From d91aab15be353b1edabec92e3447c70b78fedf85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tinjo=20Sch=C3=B6ni?= <32767367+tscni@users.noreply.github.com> Date: Wed, 29 Nov 2023 02:06:20 +0100 Subject: [PATCH] Restore support for null coalesce on match expressions https://github.com/vimeo/psalm/pull/10068 added isset restrictions that didn't consider null coalesces on match expressions. This restores that support by converting the match expression to a virtual variable for the isset analysis, similar to other incompatible expressions. --- .../Expression/BinaryOp/CoalesceAnalyzer.php | 1 + tests/MatchTest.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/CoalesceAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/CoalesceAnalyzer.php index 0a52166699a..72194f55224 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/CoalesceAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/CoalesceAnalyzer.php @@ -39,6 +39,7 @@ public static function analyze( || $root_expr instanceof PhpParser\Node\Expr\MethodCall || $root_expr instanceof PhpParser\Node\Expr\StaticCall || $root_expr instanceof PhpParser\Node\Expr\Cast + || $root_expr instanceof PhpParser\Node\Expr\Match_ || $root_expr instanceof PhpParser\Node\Expr\NullsafePropertyFetch || $root_expr instanceof PhpParser\Node\Expr\NullsafeMethodCall || $root_expr instanceof PhpParser\Node\Expr\Ternary diff --git a/tests/MatchTest.php b/tests/MatchTest.php index e281a200e97..64f2c4f5e0d 100644 --- a/tests/MatchTest.php +++ b/tests/MatchTest.php @@ -167,6 +167,21 @@ function process(Obj1|Obj2 $obj): int|string 'ignored_issues' => [], 'php_version' => '8.0', ], + 'nullCoalesce' => [ + 'code' => <<<'PHP' + null, + true => 1, + } ?? 2; + PHP, + 'assertions' => [ + '$match' => 'int', + ], + 'ignored_issues' => [], + 'php_version' => '8.0', + ], ]; }