From 2399643472a936b03dcf62bd981a75cdc8d32261 Mon Sep 17 00:00:00 2001 From: Brown Date: Mon, 13 Jul 2020 21:31:58 -0400 Subject: [PATCH] Fix #3811 - allow more complex negations inside boolean expressions --- .../Statements/Expression/AssertionFinder.php | 5 ++++ src/Psalm/Type/Algebra.php | 11 ++++++++ tests/AssertAnnotationTest.php | 27 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php index 98ceabaa6..679a541eb 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php @@ -197,7 +197,12 @@ class AssertionFinder throw new \UnexpectedValueException('Assertions should be set'); } + if (count($expr_assertions) !== 1) { + return []; + } + $if_types = \Psalm\Type\Algebra::negateTypes($expr_assertions); + return $if_types; } diff --git a/src/Psalm/Type/Algebra.php b/src/Psalm/Type/Algebra.php index efaeb3ca4..2d05fef89 100644 --- a/src/Psalm/Type/Algebra.php +++ b/src/Psalm/Type/Algebra.php @@ -256,6 +256,17 @@ class Algebra false ); } + + return self::negateFormula( + self::getFormula( + $object_id, + $conditional->expr, + $this_class_name, + $source, + $codebase, + !$inside_negation + ) + ); } if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical diff --git a/tests/AssertAnnotationTest.php b/tests/AssertAnnotationTest.php index 1c5465583..0f1cf71c7 100644 --- a/tests/AssertAnnotationTest.php +++ b/tests/AssertAnnotationTest.php @@ -1162,6 +1162,33 @@ class AssertAnnotationTest extends TestCase allString($keys); }', ], + 'multipleAssertIfTrue' => [ + '