Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analyze dynamic names for static property and const fetches #10629

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,15 @@ public static function analyzeFetch(
}

if (!$stmt->name instanceof PhpParser\Node\Identifier) {
return true;
$was_inside_general_use = $context->inside_general_use;

$context->inside_general_use = true;

$ret = ExpressionAnalyzer::analyze($statements_analyzer, $stmt->name, $context);
weirdan marked this conversation as resolved.
Show resolved Hide resolved

$context->inside_general_use = $was_inside_general_use;

return $ret;
}

$const_id = $fq_class_name . '::' . $stmt->name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,26 @@ public static function analyze(

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) {
Expand Down
33 changes: 32 additions & 1 deletion tests/UnusedVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,24 @@ function foo() : void {
A::$method();
}',
],
'usedAsStaticPropertyName' => [
'usedAsClassConstFetch' => [
'code' => '<?php
class A {
const bool something = false;

public function foo() : void {
$var = "something";

if (rand(0, 1)) {
static::{$var};
}
}
}',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.3',
],
'usedAsStaticPropertyAssign' => [
'code' => '<?php
class A {
private static bool $something = false;
Expand All @@ -1026,6 +1043,20 @@ public function foo() : void {
}
}',
],
'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
/**
Expand Down