在 PhpStorm 中,提示:Return value is expected to be ‘array’, ‘bool’ returned less… (Ctrl+F1) 的解决
1、在 PhpStorm 中,提示:Return value is expected to be ‘array’, ‘bool’ returned less… (Ctrl+F1),如图1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | /** * 返回用户详情 * * @param string $loginId 用户标识 * @param string $loginTid 登录标识 * * @return array * * 格式如下: * * 框架服务控制台的用户详情 * [ * 'message' => '', //说明 * 'data' => [], //数据 * ] * * 失败(将错误保存在 [[yii\base\Model::errors]] 属性中) * false * * @throws ServerErrorHttpException 如果响应状态码不等于20x * @throws HttpException 如果登录超时 */ public function getUser( $loginId , $loginTid ) { $requestParams = [ 'login_id' => $loginId , 'login_tid' => $loginTid , 'url' => 'interface/get-user-info' , ]; $apiRequestParams = self::getApiRequestParams( $requestParams ); $response = Yii:: $app ->cmcConsoleHttp->createRequest() ->setMethod( 'get' ) ->setUrl( $requestParams [ 'url' ]) ->setData( $apiRequestParams ) ->send(); // 检查响应状态码是否等于20x if ( $response ->isOk) { // 检查业务逻辑是否成功 if ( $response ->data[ 'code' ] === 10000) { $cmcConsoleData = [ 'message' => $response ->data[ 'message' ], 'data' => $response ->data[ 'data' ]]; return $cmcConsoleData ; } elseif ( $response ->data[ 'code' ] === 99998) { // 登录超时 throw new HttpException(302, Yii::t( 'error' , Yii::t( 'error' , Yii::t( 'error' , '201003' ), [ 'message' => $response ->data[ 'message' ]])), 201003); } else { $this ->addError( 'login_id' , $response ->data[ 'message' ]); return false; } } else { throw new ServerErrorHttpException(Yii::t( 'error' , Yii::t( 'error' , Yii::t( 'error' , '201001' ), [ 'status_code' => $response ->statusCode])), 201001); } } |
2、Return value is expected to be ‘array’, ‘bool’ returned less… (Ctrl+F1) Inspection info: Return value type is not compatible with declared. 返回值预计为 ‘array’,’bool’ 返回的较少…(Ctrl + F1)检验信息:返回值类型与声明不兼容。
3、在返回值类型声明处,加上或者返回布尔值的情况,提示信息已取消,如图2
1 | * @ return array |bool |
4、此类问题的最佳解决方案的建议,在方法中,仅返回某一种数据类型,而不是以或者的关系返回多种
近期评论