没有不值得去解决的问题,也没有不值得去学习的技术!

Fixing the PhpStorm Warning: Return Value Is Expected to Be ‘array’, ‘bool’ Returned

In PhpStorm, the prompt appears: Return value is expected to be 'array', 'bool' returned less... (Ctrl+F1)

1. In PhpStorm, the prompt appears: Return value is expected to be ‘array’, ‘bool’ returned less… (Ctrl+F1), as shown in Figure 1.

In PhpStorm, the prompt appears: Return value is expected to be 'array', 'bool' returned less... (Ctrl+F1)
Figure 1
PHP

    /**
     * 返回用户详情
     *
     * @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. The return value is expected to be ‘array’, but ‘bool’ is returned… (Ctrl+F1) Inspection info: The return value type is not compatible with the declaration.

3. At the return value type declaration, adding the case of returning a boolean value cancels the prompt, as shown in Figure 2.

At the return value type declaration, adding the case of returning a boolean value cancels the prompt
Figure 2
PHP


     * @return array|bool

4. The recommended best solution for this type of issue is to return only a single data type in a method, rather than returning multiple types in an either-or relationship.

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系