psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Greater > and Less Than < comparisons with non-numeric, non-string, non-resource types should report error

Open kkmuffme opened this issue 11 months ago • 1 comments

e.g. https://psalm.dev/r/e63cef6890 this is a redundant condition but isn't reported, or https://psalm.dev/r/29eb392672 which causes a PHP notice.

However there are tons of things one can do wrong with those operators: https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.types and it would take an immense amount of time to figure out all possible cases and all report them as separate errors.

As a first, I suggest to report all > and < operations that contain an array, object, probably even null and bool as possible error - and refine this if e.g. there are too many false positives for some cases (e.g. int|false > 100)

kkmuffme avatar Mar 15 '24 19:03 kkmuffme

I found these snippets:

https://psalm.dev/r/e63cef6890
<?php

$a = array();

if ( $a > PHP_INT_MAX ) {
    echo "yes";
}
Psalm output (using commit ef3b018):

No issues!
https://psalm.dev/r/29eb392672
<?php

$a = new stdClass();

if ( $a > PHP_INT_MAX ) {
    echo "yes";
}
Psalm output (using commit ef3b018):

No issues!

psalm-github-bot[bot] avatar Mar 15 '24 19:03 psalm-github-bot[bot]