Skip to content

Fix crash on dynamic numeric-string symbols #3975

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

Merged
merged 2 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Rules/Classes/ClassConstantRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ public function processNode(Node $node, Scope $scope): array
}

foreach ($constantNameScopes as $constantName => $constantScope) {
$errors = array_merge($errors, $this->processSingleClassConstFetch($constantScope, $node, $constantName));
$errors = array_merge($errors, $this->processSingleClassConstFetch(
$constantScope,
$node,
(string) $constantName, // @phpstan-ignore cast.useless
));
}

return $errors;
Expand Down
6 changes: 5 additions & 1 deletion src/Rules/Methods/CallMethodsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ public function processNode(Node $node, Scope $scope): array
}

foreach ($methodNameScopes as $methodName => $methodScope) {
$errors = array_merge($errors, $this->processSingleMethodCall($methodScope, $node, $methodName));
$errors = array_merge($errors, $this->processSingleMethodCall(
$methodScope,
$node,
(string) $methodName, // @phpstan-ignore cast.useless
));
}

return $errors;
Expand Down
6 changes: 5 additions & 1 deletion src/Rules/Methods/CallStaticMethodsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public function processNode(Node $node, Scope $scope): array
}

foreach ($methodNameScopes as $methodName => $methodScope) {
$errors = array_merge($errors, $this->processSingleMethodCall($methodScope, $node, $methodName));
$errors = array_merge($errors, $this->processSingleMethodCall(
$methodScope,
$node,
(string) $methodName, // @phpstan-ignore cast.useless
));
}

return $errors;
Expand Down
6 changes: 5 additions & 1 deletion src/Rules/Variables/DefinedVariableRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public function processNode(Node $node, Scope $scope): array
}

foreach ($variableNameScopes as $name => $variableScope) {
$errors = array_merge($errors, $this->processSingleVariable($variableScope, $node, $name));
$errors = array_merge($errors, $this->processSingleVariable(
$variableScope,
$node,
(string) $name, // @phpstan-ignore cast.useless
));
}

return $errors;
Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,20 @@ public function testBug12800(): void
$this->assertNoErrors($errors);
}

public function testBug12949(): void
{
// Fetching class constants with a dynamic name is supported only on PHP 8.3 and later
if (PHP_VERSION_ID < 80300) {
$this->markTestSkipped('Test requires PHP 8.3.');
}

$errors = $this->runAnalyse(__DIR__ . '/data/bug-12949.php');
$this->assertCount(3, $errors);
$this->assertSame('Call to an undefined method object::0().', $errors[0]->getMessage());
$this->assertSame('Call to an undefined static method object::0().', $errors[1]->getMessage());
$this->assertSame('Access to undefined constant object::0.', $errors[2]->getMessage());
}

/**
* @param string[]|null $allAnalysedFiles
* @return Error[]
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Analyser/data/bug-12949.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Bug12949;

function doFoo():void {
$b = '0';
${$b} = 1;

echo "";
}

function doBar(object $o):void {
$b = '0';
$o->{$b}();
$o::{$b}();
echo $o::{$b};
Comment on lines +13 to +16
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these cases are realistic, but I fixed them to keep the implementation of the 4 rules in sync


echo "";
}
Loading