diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/IfAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/IfAnalyzer.php index a644bd666..b3d64cabe 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/IfAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/IfAnalyzer.php @@ -1822,6 +1822,22 @@ class IfAnalyzer */ private static function getDefinitelyEvaluatedExpressionAfterIf(PhpParser\Node\Expr $stmt) { + if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Equal + || $stmt instanceof PhpParser\Node\Expr\BinaryOp\Identical + ) { + if ($stmt->left instanceof PhpParser\Node\Expr\ConstFetch + && $stmt->left->name->parts === ['true'] + ) { + return self::getDefinitelyEvaluatedExpressionAfterIf($stmt->right); + } + + if ($stmt->right instanceof PhpParser\Node\Expr\ConstFetch + && $stmt->right->name->parts === ['true'] + ) { + return self::getDefinitelyEvaluatedExpressionAfterIf($stmt->left); + } + } + if ($stmt instanceof PhpParser\Node\Expr\BinaryOp) { if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd || $stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalAnd @@ -1854,6 +1870,22 @@ class IfAnalyzer */ private static function getDefinitelyEvaluatedExpressionInsideIf(PhpParser\Node\Expr $stmt) { + if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Equal + || $stmt instanceof PhpParser\Node\Expr\BinaryOp\Identical + ) { + if ($stmt->left instanceof PhpParser\Node\Expr\ConstFetch + && $stmt->left->name->parts === ['true'] + ) { + return self::getDefinitelyEvaluatedExpressionInsideIf($stmt->right); + } + + if ($stmt->right instanceof PhpParser\Node\Expr\ConstFetch + && $stmt->right->name->parts === ['true'] + ) { + return self::getDefinitelyEvaluatedExpressionInsideIf($stmt->left); + } + } + if ($stmt instanceof PhpParser\Node\Expr\BinaryOp) { if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr || $stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalOr diff --git a/tests/TypeReconciliation/ConditionalTest.php b/tests/TypeReconciliation/ConditionalTest.php index 03554f39b..9067d7f92 100644 --- a/tests/TypeReconciliation/ConditionalTest.php +++ b/tests/TypeReconciliation/ConditionalTest.php @@ -2773,6 +2773,14 @@ class ConditionalTest extends \Psalm\Tests\TestCase sayHi($hello);', ], + 'equalsTrueInIf' => [ + 'format("Y") === "2020") == true) { + $a->format("d-m-Y"); + }', + ], ]; }