phptools-docs
phptools-docs copied to clipboard
Return type wrongly detected as "bool"
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:
and even this when I use the returned value:
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".
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.