Skip to content

Commit 2ac87fc

Browse files
authored
Fix crash on dynamic numeric-string symbols
1 parent f615b1a commit 2ac87fc

File tree

6 files changed

+53
-4
lines changed

6 files changed

+53
-4
lines changed

src/Rules/Classes/ClassConstantRule.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ public function processNode(Node $node, Scope $scope): array
6464
}
6565

6666
foreach ($constantNameScopes as $constantName => $constantScope) {
67-
$errors = array_merge($errors, $this->processSingleClassConstFetch($constantScope, $node, $constantName));
67+
$errors = array_merge($errors, $this->processSingleClassConstFetch(
68+
$constantScope,
69+
$node,
70+
(string) $constantName, // @phpstan-ignore cast.useless
71+
));
6872
}
6973

7074
return $errors;

src/Rules/Methods/CallMethodsRule.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ public function processNode(Node $node, Scope $scope): array
4747
}
4848

4949
foreach ($methodNameScopes as $methodName => $methodScope) {
50-
$errors = array_merge($errors, $this->processSingleMethodCall($methodScope, $node, $methodName));
50+
$errors = array_merge($errors, $this->processSingleMethodCall(
51+
$methodScope,
52+
$node,
53+
(string) $methodName, // @phpstan-ignore cast.useless
54+
));
5155
}
5256

5357
return $errors;

src/Rules/Methods/CallStaticMethodsRule.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ public function processNode(Node $node, Scope $scope): array
4848
}
4949

5050
foreach ($methodNameScopes as $methodName => $methodScope) {
51-
$errors = array_merge($errors, $this->processSingleMethodCall($methodScope, $node, $methodName));
51+
$errors = array_merge($errors, $this->processSingleMethodCall(
52+
$methodScope,
53+
$node,
54+
(string) $methodName, // @phpstan-ignore cast.useless
55+
));
5256
}
5357

5458
return $errors;

src/Rules/Variables/DefinedVariableRule.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ public function processNode(Node $node, Scope $scope): array
4848
}
4949

5050
foreach ($variableNameScopes as $name => $variableScope) {
51-
$errors = array_merge($errors, $this->processSingleVariable($variableScope, $node, $name));
51+
$errors = array_merge($errors, $this->processSingleVariable(
52+
$variableScope,
53+
$node,
54+
(string) $name, // @phpstan-ignore cast.useless
55+
));
5256
}
5357

5458
return $errors;

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,20 @@ public function testBug12800(): void
15701570
$this->assertNoErrors($errors);
15711571
}
15721572

1573+
public function testBug12949(): void
1574+
{
1575+
// Fetching class constants with a dynamic name is supported only on PHP 8.3 and later
1576+
if (PHP_VERSION_ID < 80300) {
1577+
$this->markTestSkipped('Test requires PHP 8.3.');
1578+
}
1579+
1580+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-12949.php');
1581+
$this->assertCount(3, $errors);
1582+
$this->assertSame('Call to an undefined method object::0().', $errors[0]->getMessage());
1583+
$this->assertSame('Call to an undefined static method object::0().', $errors[1]->getMessage());
1584+
$this->assertSame('Access to undefined constant object::0.', $errors[2]->getMessage());
1585+
}
1586+
15731587
/**
15741588
* @param string[]|null $allAnalysedFiles
15751589
* @return Error[]
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Bug12949;
4+
5+
function doFoo():void {
6+
$b = '0';
7+
${$b} = 1;
8+
9+
echo "";
10+
}
11+
12+
function doBar(object $o):void {
13+
$b = '0';
14+
$o->{$b}();
15+
$o::{$b}();
16+
echo $o::{$b};
17+
18+
echo "";
19+
}

0 commit comments

Comments
 (0)