1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 09:37:59 +01:00

Analyze dynamic static property names

This commit is contained in:
Evan Shaw 2024-02-01 17:31:15 +13:00
parent 00822074fe
commit a66aace523
2 changed files with 34 additions and 6 deletions

View File

@ -134,13 +134,27 @@ 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))
} else {
$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) {
if ($fq_class_name) {

View File

@ -1012,7 +1012,7 @@ class UnusedVariableTest extends TestCase
A::$method();
}',
],
'usedAsStaticPropertyName' => [
'usedAsStaticPropertyAssign' => [
'code' => '<?php
class A {
private static bool $something = false;
@ -1026,6 +1026,20 @@ class UnusedVariableTest extends TestCase
}
}',
],
'usedAsStaticPropertyFetch' => [
'code' => '<?php
class A {
private static bool $something = false;
public function foo() : void {
$var = "something";
if (rand(0, 1)) {
static::${$var};
}
}
}',
],
'setInLoopThatsAlwaysEntered' => [
'code' => '<?php
/**