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:
parent
00822074fe
commit
a66aace523
@ -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) {
|
||||
|
@ -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
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user