1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #3563 - add workaround for == true

This commit is contained in:
Brown 2020-06-12 10:58:44 -04:00
parent 8c2f1d7683
commit 9ca6c868b7
2 changed files with 40 additions and 0 deletions

View File

@ -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

View File

@ -2773,6 +2773,14 @@ class ConditionalTest extends \Psalm\Tests\TestCase
sayHi($hello);',
],
'equalsTrueInIf' => [
'<?php
$a = rand(0,1) ? new DateTime() : null;
if (($a !== null && $a->format("Y") === "2020") == true) {
$a->format("d-m-Y");
}',
],
];
}