From 0107afb2fb3c91a9fb72cbefc912df6e172f4b3e Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Sun, 17 Jan 2021 12:08:19 -0500 Subject: [PATCH] Fix #5025 - prevent crash in assert function during reflection --- .../Reflector/FunctionLikeNodeScanner.php | 7 ++++++- tests/AssertAnnotationTest.php | 18 ++++++++++++++++++ tests/PropertyTypeTest.php | 10 ++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php b/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php index fe53f5027..5107fd654 100644 --- a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php +++ b/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php @@ -297,7 +297,12 @@ class FunctionLikeNodeScanner null ); - $negated_formula = \Psalm\Internal\Algebra::negateFormula($if_clauses); + try { + $negated_formula = \Psalm\Internal\Algebra::negateFormula($if_clauses); + } catch (\Psalm\Exception\ComplicatedExpressionException $e) { + $var_assertions = []; + break; + } $rules = \Psalm\Internal\Algebra::getTruthsFromFormula($negated_formula); diff --git a/tests/AssertAnnotationTest.php b/tests/AssertAnnotationTest.php index 9e5b92e7c..a5055c3ad 100644 --- a/tests/AssertAnnotationTest.php +++ b/tests/AssertAnnotationTest.php @@ -1414,6 +1414,24 @@ class AssertAnnotationTest extends TestCase } ?>' ], + 'implicitComplexAssertionNoCrash' => [ + 'status && "complete" === $status) + || ("canceled" === $this->status && "pending" === $status) + || ("complete" === $this->status && "canceled" === $status) + || ("complete" === $this->status && "pending" === $status) + ) { + throw new \LogicException(); + } + } + }' + ], ]; } diff --git a/tests/PropertyTypeTest.php b/tests/PropertyTypeTest.php index cdf5a1369..c443ef473 100644 --- a/tests/PropertyTypeTest.php +++ b/tests/PropertyTypeTest.php @@ -2385,6 +2385,16 @@ class PropertyTypeTest extends TestCase [], '8.0' ], + 'dynamicPropertyFetch' => [ + '{$b} ?? null; + }' + ], ]; }