Skip to content

Commit

Permalink
Merge pull request #10629 from edsrzf/analyze-dynamic-fetches
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Feb 5, 2024
2 parents ddb02b9 + 4cec31e commit c40f232
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 7 deletions.
Expand Up @@ -206,7 +206,25 @@ public static function analyzeFetch(
}

if (!$stmt->name instanceof PhpParser\Node\Identifier) {
return true;
if ($codebase->analysis_php_version_id < 8_03_00) {
IssueBuffer::maybeAdd(
new ParseError(
'Dynamically fetching class constants and enums requires PHP 8.3',
new CodeLocation($statements_analyzer->getSource(), $stmt),
),
$statements_analyzer->getSuppressedIssues(),
);
}

$was_inside_general_use = $context->inside_general_use;

$context->inside_general_use = true;

$ret = ExpressionAnalyzer::analyze($statements_analyzer, $stmt->name, $context);

$context->inside_general_use = $was_inside_general_use;

return $ret;
}

$const_id = $fq_class_name . '::' . $stmt->name;
Expand Down
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
12 changes: 12 additions & 0 deletions tests/ConstantTest.php
Expand Up @@ -2476,6 +2476,18 @@ class A {
PHP,
'error_message' => 'InvalidArrayOffset',
],
'unsupportedDynamicFetch' => [
'code' => '<?php
class C {
const A = 0;
}
$a = C::{"A"};
',
'error_message' => 'ParseError',
'error_levels' => [],
'php_version' => '8.2',
],
];
}
}
52 changes: 51 additions & 1 deletion tests/UnusedVariableTest.php
Expand Up @@ -1012,7 +1012,43 @@ 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',
],
'usedAsEnumFetch' => [
'code' => '<?php
enum E {
case C;
}
class A {
public function foo() : void {
$var = "C";
if (rand(0, 1)) {
E::{$var};
}
}
}',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.3',
],
'usedAsStaticPropertyAssign' => [
'code' => '<?php
class A {
private static bool $something = false;
Expand All @@ -1026,6 +1062,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

0 comments on commit c40f232

Please sign in to comment.