phptools-docs icon indicating copy to clipboard operation
phptools-docs copied to clipboard

Return type wrongly detected as "bool"

Open php4fan opened this issue 1 year ago • 1 comments

I have this function (a method of a class):

    public function getCachedUser($telegram_user_id) {
        if (!isset($this->userCache[$telegram_user_id])) {
            $user = $this->query_first("
                SELECT *, BINARY info AS info_bin FROM `{BOT_TABLE}user`
                WHERE telegram_id = $telegram_user_id
            ");
            if (!$user) $this->userCache[$telegram_user_id] = false;
            else {
                $user['info'] = json_decode($user['info_bin'], true);
                $this->userCache[$telegram_user_id] = $user;
            }
        }
        return $this->userCache[$telegram_user_id];
    }

This does NOT always return a boolean. It can return a boolean but only in some cases.

Yet when I use the function in my code, I get this:

image

and even this when I use the returned value:

image

I do not expect the extension to be able to predict that this function returns either the boolean false or an array (that would be amazing), but I do expect it to not not be over-confident and state that the return value is a boolean when it doesn't know and it is in fact not true.

If it can't know for sure, it should say "mixed".

php4fan avatar Nov 30 '23 20:11 php4fan

I see, we're too optimistic here ...

The quick workaround is to annotate the method with PHPDoc @return tag.

/** @return array|false */

I'll add the getCachedUser to our tests, and fix the analysis accordingly.

jakubmisek avatar Dec 10 '23 11:12 jakubmisek