From 45ea5d0bfeae4b3cb28fe60ad80c180f2151149d Mon Sep 17 00:00:00 2001 From: Brown Date: Fri, 12 Jun 2020 11:18:34 -0400 Subject: [PATCH] Add a couple more shortcuts for common pattern Fixes #3563 --- .../Statements/Expression/AssertionFinder.php | 2 +- src/Psalm/Type/Algebra.php | 39 ++++++++++++++++++- tests/SwitchTypeTest.php | 9 +++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php index 3223a9891..9d21caba1 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php @@ -2367,7 +2367,7 @@ class AssertionFinder * * @return int|null */ - protected static function hasTrueVariable(PhpParser\Node\Expr\BinaryOp $conditional) + public static function hasTrueVariable(PhpParser\Node\Expr\BinaryOp $conditional) { if ($conditional->right instanceof PhpParser\Node\Expr\ConstFetch && strtolower($conditional->right->name->parts[0]) === 'true' diff --git a/src/Psalm/Type/Algebra.php b/src/Psalm/Type/Algebra.php index 12c85fe6a..efaeb3ca4 100644 --- a/src/Psalm/Type/Algebra.php +++ b/src/Psalm/Type/Algebra.php @@ -258,8 +258,11 @@ class Algebra } } - if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical) { + if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical + || $conditional instanceof PhpParser\Node\Expr\BinaryOp\Equal + ) { $false_pos = AssertionFinder::hasFalseVariable($conditional); + $true_pos = AssertionFinder::hasTrueVariable($conditional); if ($false_pos === AssertionFinder::ASSIGNMENT_TO_RIGHT && ($conditional->left instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd @@ -276,7 +279,9 @@ class Algebra $inside_negation, $cache ); - } elseif ($false_pos === AssertionFinder::ASSIGNMENT_TO_LEFT + } + + if ($false_pos === AssertionFinder::ASSIGNMENT_TO_LEFT && ($conditional->right instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd || $conditional->right instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr) ) { @@ -292,6 +297,36 @@ class Algebra $cache ); } + + if ($true_pos === AssertionFinder::ASSIGNMENT_TO_RIGHT + && ($conditional->left instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd + || $conditional->left instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr) + ) { + return self::getFormula( + $object_id, + $conditional->left, + $this_class_name, + $source, + $codebase, + $inside_negation, + $cache + ); + } + + if ($true_pos === AssertionFinder::ASSIGNMENT_TO_LEFT + && ($conditional->right instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd + || $conditional->right instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr) + ) { + return self::getFormula( + $object_id, + $conditional->right, + $this_class_name, + $source, + $codebase, + $inside_negation, + $cache + ); + } } $assertions = null; diff --git a/tests/SwitchTypeTest.php b/tests/SwitchTypeTest.php index dcda1dcc9..7933f7684 100644 --- a/tests/SwitchTypeTest.php +++ b/tests/SwitchTypeTest.php @@ -861,6 +861,15 @@ class SwitchTypeTest extends TestCase } }' ], + 'switchTruthyWithBoolean' => [ + 'format("Y") === "2020": + $a->format("d-m-Y"); + }' + ], ]; }