Vscode-intelephense: Improving static code analysis for types

Created on 13 Sep 2019  路  7Comments  路  Source: bmewburn/vscode-intelephense

We know that curl_init() (for example) returns resource|false:

image

But when we returns/throws from the code by conditions, that checks for some types in such type unions, we need to exclude these types in the code after the conditions:

image

On my screenshot above $ch will not be false or boolean after the condition - it will be only resource. Some instruments for static code analysis, like phpstan, see it too. It would be nice to have such analysis in intelephense.

bug

All 7 comments

Following works even now, so I guess false and throw need to be correctly recognized.

if(is_bool($ch)){
    return;
}
$this->ch = $ch

Also extension is generalizing false keyword [:link:phpdoc] into bool, but true and false is correct type in phpdoc.

@KapitanOczywisty In my environment (VSC 1.38.1, Intelephense 1.2.3) it doesn't work even with is_bool() and return:

image

@yura3d same version:

image

@KapitanOczywisty Thanks for your reply, it prompted me to dive deeper in the problem. And right now I've found the reason that breaks this example in my code. It is a namespace. When it is declared for the file, the type of $ch is not recognized as expected after return:

image

It seems this is a bug.

Summarazing the discussion above, what was discovered and needs to be improved/fixed:

  1. false and true types are subtypes of bool in PHPDoc and should not be generalized to it;
  2. type exclusion (on condition) for type unions should work not only with return, but with other language constructions that interrupt code execution (throw, exit, die, ...); it would be nice that it will work even if throw (for example) occured in called external method.
  3. namespace declaration in file should not break type exclusion.

Different behavior depending on condition form:

image

Thanks for the analysis @yura3d and @KapitanOczywisty . Fixed in 1.3.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ilyes512 picture Ilyes512  路  3Comments

dgunay picture dgunay  路  3Comments

superadmini picture superadmini  路  4Comments

vanasis picture vanasis  路  4Comments

pseudoanime picture pseudoanime  路  3Comments