From 33406574ca903c41db4592312dddeaeca75fe3f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Kr=C3=B6g?= Date: Sat, 9 Nov 2024 19:53:38 +0100 Subject: [PATCH 1/2] Add test for property-get with null-coalesce --- tests/MagicPropertyTest.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/MagicPropertyTest.php b/tests/MagicPropertyTest.php index bc1e89faffe..0ac6dba8d52 100644 --- a/tests/MagicPropertyTest.php +++ b/tests/MagicPropertyTest.php @@ -765,6 +765,20 @@ public function updateStatus(): array { } }', ], + 'propertyFetchWithNullCoalesce' => [ + 'code' => 'p ?? \'\'; + } + }', + ], ]; } @@ -1189,6 +1203,21 @@ class A { }', 'error_message' => 'InvalidDocblock', ], + 'invalidPropertyFetchWithNullCoalesce' => [ + 'code' => 'q ?? \'\'; + } + }', + 'error_message' => 'MixedReturnStatement', + ], ]; } From c0672fe6f27dbb0ca668d3b854ffe26d7e3ac76b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Kr=C3=B6g?= Date: Sat, 9 Nov 2024 19:54:18 +0100 Subject: [PATCH 2/2] Fix type for property-get with null-coalesce --- .../Analyzer/Statements/Expression/IssetAnalyzer.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/IssetAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/IssetAnalyzer.php index 6303733642b..373ccdc7c8d 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/IssetAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/IssetAnalyzer.php @@ -5,6 +5,7 @@ use PhpParser; use Psalm\CodeLocation; use Psalm\Context; +use Psalm\Internal\Analyzer\Statements\Expression\Fetch\InstancePropertyFetchAnalyzer; use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer; use Psalm\Internal\Analyzer\StatementsAnalyzer; use Psalm\Issue\InvalidArgument; @@ -30,7 +31,16 @@ public static function analyze( $var_id = '$this->' . $isset_var->name->name; if (!isset($context->vars_in_scope[$var_id])) { - $context->vars_in_scope[$var_id] = Type::getMixed(); + if (isset($context->vars_in_scope['$this'])) { + InstancePropertyFetchAnalyzer::analyze( + $statements_analyzer, + $isset_var, + $context, + ); + } + if (!isset($context->vars_in_scope[$var_id])) { + $context->vars_in_scope[$var_id] = Type::getMixed(); + } $context->vars_possibly_in_scope[$var_id] = true; } } elseif (!self::isValidStatement($isset_var)) {