From a66aace523c9e39a6129c404789ecabe247cdad1 Mon Sep 17 00:00:00 2001 From: Evan Shaw Date: Thu, 1 Feb 2024 17:31:15 +1300 Subject: [PATCH] Analyze dynamic static property names --- .../Fetch/StaticPropertyFetchAnalyzer.php | 24 +++++++++++++++---- tests/UnusedVariableTest.php | 16 ++++++++++++- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/StaticPropertyFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/StaticPropertyFetchAnalyzer.php index 94771ed2e..790b36b30 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/StaticPropertyFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/StaticPropertyFetchAnalyzer.php @@ -134,12 +134,26 @@ final class StaticPropertyFetchAnalyzer if ($stmt->name instanceof PhpParser\Node\VarLikeIdentifier) { $prop_name = $stmt->name->name; - } elseif (($stmt_name_type = $statements_analyzer->node_data->getType($stmt->name)) - && $stmt_name_type->isSingleStringLiteral() - ) { - $prop_name = $stmt_name_type->getSingleStringLiteral()->value; } else { - $prop_name = null; + $was_inside_general_use = $context->inside_general_use; + + $context->inside_general_use = true; + + if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->name, $context) === false) { + $context->inside_general_use = $was_inside_general_use; + + return false; + } + + $context->inside_general_use = $was_inside_general_use; + + if (($stmt_name_type = $statements_analyzer->node_data->getType($stmt->name)) + && $stmt_name_type->isSingleStringLiteral() + ) { + $prop_name = $stmt_name_type->getSingleStringLiteral()->value; + } else { + $prop_name = null; + } } if (!$prop_name) { diff --git a/tests/UnusedVariableTest.php b/tests/UnusedVariableTest.php index 8518dd510..d58c210c0 100644 --- a/tests/UnusedVariableTest.php +++ b/tests/UnusedVariableTest.php @@ -1012,7 +1012,7 @@ class UnusedVariableTest extends TestCase A::$method(); }', ], - 'usedAsStaticPropertyName' => [ + 'usedAsStaticPropertyAssign' => [ 'code' => ' [ + 'code' => ' [ 'code' => '