mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 04:45:20 +01:00
Fix #4061 - allow indirect null comparison check
This commit is contained in:
parent
936a7e01b6
commit
e64d45b644
@ -511,7 +511,7 @@ class AssertionFinder
|
||||
) {
|
||||
$if_types = [];
|
||||
|
||||
$null_position = self::hasNullVariable($conditional);
|
||||
$null_position = self::hasNullVariable($conditional, $source);
|
||||
$false_position = self::hasFalseVariable($conditional);
|
||||
$true_position = self::hasTrueVariable($conditional);
|
||||
$empty_array_position = self::hasEmptyArrayVariable($conditional);
|
||||
@ -1147,7 +1147,7 @@ class AssertionFinder
|
||||
) {
|
||||
$if_types = [];
|
||||
|
||||
$null_position = self::hasNullVariable($conditional);
|
||||
$null_position = self::hasNullVariable($conditional, $sourcec);
|
||||
$false_position = self::hasFalseVariable($conditional);
|
||||
$true_position = self::hasTrueVariable($conditional);
|
||||
$empty_array_position = self::hasEmptyArrayVariable($conditional);
|
||||
@ -2439,8 +2439,10 @@ class AssertionFinder
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
protected static function hasNullVariable(PhpParser\Node\Expr\BinaryOp $conditional)
|
||||
{
|
||||
protected static function hasNullVariable(
|
||||
PhpParser\Node\Expr\BinaryOp $conditional,
|
||||
FileSource $source
|
||||
) {
|
||||
if ($conditional->right instanceof PhpParser\Node\Expr\ConstFetch
|
||||
&& strtolower($conditional->right->name->parts[0]) === 'null'
|
||||
) {
|
||||
@ -2453,6 +2455,13 @@ class AssertionFinder
|
||||
return self::ASSIGNMENT_TO_LEFT;
|
||||
}
|
||||
|
||||
if ($source instanceof StatementsAnalyzer
|
||||
&& ($right_type = $source->node_data->getType($conditional->right))
|
||||
&& $right_type->isNull()
|
||||
) {
|
||||
return self::ASSIGNMENT_TO_RIGHT;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -736,6 +736,26 @@ class ValueTest extends \Psalm\Tests\TestCase
|
||||
}
|
||||
if ($data["e"] > 0) {}'
|
||||
],
|
||||
'compareToNullImplicitly' => [
|
||||
'<?php
|
||||
final class Foo {
|
||||
public const VALUE_ANY = null;
|
||||
public const VALUE_ONE = "one";
|
||||
|
||||
/** @return self::VALUE_* */
|
||||
public static function getValues() {
|
||||
return rand(0, 1) ? null : self::VALUE_ONE;
|
||||
}
|
||||
}
|
||||
|
||||
$data = Foo::getValues();
|
||||
|
||||
if ($data === Foo::VALUE_ANY) {
|
||||
$data = "default";
|
||||
}
|
||||
|
||||
echo strlen($data);'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user