From e97a9c86eb4b3cf9ab804b6f05b995931f84b4bc Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Mon, 9 Nov 2020 10:00:53 -0500 Subject: [PATCH] Fix #4517 - track type contradiction issues in match expressions --- .../Statements/Expression/MatchAnalyzer.php | 31 ++++++++++++++----- stubs/Php80.php | 2 ++ tests/MatchTest.php | 12 +++++++ 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/MatchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/MatchAnalyzer.php index a348fd138..46041df9f 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/MatchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/MatchAnalyzer.php @@ -106,10 +106,14 @@ class MatchAnalyzer new PhpParser\Node\Expr\Throw_( new PhpParser\Node\Expr\New_( new PhpParser\Node\Name\FullyQualified( - 'UnhandledMatchError' - ) + 'UnhandledMatchError', + $stmt->getAttributes() + ), + [], + $stmt->getAttributes() ) - ) + ), + $stmt->getAttributes() ); } @@ -255,17 +259,28 @@ class MatchAnalyzer new PhpParser\Node\Name\FullyQualified(['in_array']), [ new PhpParser\Node\Arg( - $match_condition + $match_condition, + false, + false, + $attributes ), new PhpParser\Node\Arg( new PhpParser\Node\Expr\Array_( - $array_items - ) + $array_items, + $attributes + ), + false, + false, + $attributes ), new PhpParser\Node\Arg( new PhpParser\Node\Expr\ConstFetch( - new PhpParser\Node\Name\FullyQualified(['true']) - ) + new PhpParser\Node\Name\FullyQualified(['true']), + $attributes + ), + false, + false, + $attributes ), ], $attributes diff --git a/stubs/Php80.php b/stubs/Php80.php index b0f43c1c6..f6c453c0d 100644 --- a/stubs/Php80.php +++ b/stubs/Php80.php @@ -72,3 +72,5 @@ class ReflectionUnionType extends ReflectionType { */ public function getTypes() {} } + +class UnhandledMatchError extends Error {} diff --git a/tests/MatchTest.php b/tests/MatchTest.php index 20332e79f..d8e69bee6 100644 --- a/tests/MatchTest.php +++ b/tests/MatchTest.php @@ -205,6 +205,18 @@ class MatchTest extends TestCase false, '8.0', ], + 'matchTrueImpossible' => [ + ' 1, + $foo instanceof \Exception => 1, + };', + 'error_message' => 'TypeDoesNotContainType', + [], + false, + '8.0', + ], ]; } }