-
Notifications
You must be signed in to change notification settings - Fork 590
Report non-stringable names in variable variables, dynamic method calls and dynamic static property/method access #5871
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
ondrejmirtes
merged 6 commits into
phpstan:2.2.x
from
phpstan-bot:create-pull-request/patch-8kop9k3
Sep 20, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd8aa7f
Report non-stringable names in variable variables, dynamic method cal…
VincentLanglet 65beed9
Build errors inside NonStringableDynamicAccessCheck and return them t…
phpstan-bot d25be4f
Derive error line and class placeholder inside NonStringableDynamicAc…
phpstan-bot f472eda
Fix
VincentLanglet ffac986
Add dynamic static property name tests for nullable and union name types
phpstan-bot a3d250c
List valid error identifiers in NonStringableDynamicAccessCheck PHPDocs
phpstan-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules; | ||
|
|
||
| use PhpParser\Node\Expr; | ||
| use PhpParser\Node\Name; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\AutowiredParameter; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use PHPStan\Type\ErrorType; | ||
| use PHPStan\Type\Type; | ||
| use PHPStan\Type\VerbosityLevel; | ||
| use function sprintf; | ||
|
|
||
| /** | ||
| * Checks whether the name of a dynamically accessed variable or member | ||
| * (`$$name`, `$obj->{$name}`, `$obj->{$name}()`, `Foo::{$name}()`, | ||
| * `Foo::${$name}`, `Foo::{$name}`) can actually be used as a name at runtime. | ||
| * | ||
| * Gated behind the `checkNonStringableDynamicAccess` feature toggle. | ||
| */ | ||
| #[AutowiredService] | ||
| final class NonStringableDynamicAccessCheck | ||
| { | ||
|
|
||
| public function __construct( | ||
| private RuleLevelHelper $ruleLevelHelper, | ||
| #[AutowiredParameter(ref: '%featureToggles.checkNonStringableDynamicAccess%')] | ||
| private bool $checkNonStringableDynamicAccess, | ||
| ) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * For names that PHP casts to string at runtime (variable variables, | ||
| * property and static property names) objects implementing __toString are | ||
| * accepted. | ||
| * | ||
| * @param Name|Expr|null $classNode the node whose name/type fills the leading `%s` placeholder, or null when the message has none | ||
| * @param 'property.nameNotString'|'staticProperty.nameNotString'|'variable.nameNotString' $identifier | ||
| * @return list<IdentifierRuleError> | ||
| */ | ||
| public function checkStringCastableName(Scope $scope, Expr $name, string $messageFormat, $classNode, string $identifier): array | ||
| { | ||
| if (!$this->checkNonStringableDynamicAccess) { | ||
| return []; | ||
| } | ||
|
|
||
| $nameType = $this->ruleLevelHelper->findTypeToCheck( | ||
| $scope, | ||
| $name, | ||
| '', | ||
| static fn (Type $type) => !$type->toString() instanceof ErrorType && $type->toString()->isString()->yes(), | ||
| )->getType(); | ||
|
|
||
| if ( | ||
| !$nameType instanceof ErrorType | ||
| && ($nameType->toString() instanceof ErrorType || !$nameType->toString()->isString()->yes()) | ||
| ) { | ||
| return [$this->buildError($scope, $name, $scope->getType($name), $messageFormat, $classNode, $identifier)]; | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| /** | ||
| * For names that must be actual strings (method, static method and class | ||
| * constant names) objects implementing __toString are not accepted. | ||
| * | ||
| * @param Name|Expr|null $classNode the node whose name/type fills the leading `%s` placeholder, or null when the message has none | ||
| * @param 'classConstant.nameNotString'|'method.nameNotString'|'staticMethod.nameNotString' $identifier | ||
| * @return list<IdentifierRuleError> | ||
| */ | ||
| public function checkStringName(Scope $scope, Expr $name, string $messageFormat, $classNode, string $identifier): array | ||
| { | ||
| if (!$this->checkNonStringableDynamicAccess) { | ||
| return []; | ||
| } | ||
|
|
||
| $nameType = $this->ruleLevelHelper->findTypeToCheck( | ||
| $scope, | ||
| $name, | ||
| '', | ||
| static fn (Type $type) => $type->isString()->yes(), | ||
| )->getType(); | ||
|
|
||
| if (!$nameType instanceof ErrorType && !$nameType->isString()->yes()) { | ||
| return [$this->buildError($scope, $name, $nameType, $messageFormat, $classNode, $identifier)]; | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| /** | ||
| * @param Name|Expr|null $classNode | ||
| * @param 'classConstant.nameNotString'|'method.nameNotString'|'property.nameNotString'|'staticMethod.nameNotString'|'staticProperty.nameNotString'|'variable.nameNotString' $identifier | ||
| */ | ||
| private function buildError(Scope $scope, Expr $name, Type $nameType, string $messageFormat, $classNode, string $identifier): IdentifierRuleError | ||
| { | ||
| $messageArgs = []; | ||
| if ($classNode !== null) { | ||
| $messageArgs[] = $classNode instanceof Name | ||
| ? $scope->resolveName($classNode) | ||
| : $scope->getType($classNode)->describe(VerbosityLevel::typeOnly()); | ||
| } | ||
| $messageArgs[] = $nameType->describe(VerbosityLevel::precise()); | ||
|
|
||
| return RuleErrorBuilder::message(sprintf($messageFormat, ...$messageArgs)) | ||
| ->line($name->getStartLine()) | ||
| ->identifier($identifier) | ||
| ->build(); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$identifierneeds to list all valid identifiers as a union of constant strings in@paramso that https://github.com/phpstan/phpstan/tree/2.2.x/identifier-extractor can extract them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed ondrejmirtes' review:
$identifieris now typed as a union of constant strings in the PHPDocs ofsrc/Rules/NonStringableDynamicAccessCheck.php, so the identifier-extractor can pick the identifiers up.checkStringCastableName()—'property.nameNotString'|'staticProperty.nameNotString'|'variable.nameNotString'checkStringName()—'classConstant.nameNotString'|'method.nameNotString'|'staticMethod.nameNotString'buildError()— union of all six, since that's where->identifier($identifier)is actually called and the extractor needs a constant-string type at that call site.Verification:
make phpstanreports no errors (which also confirms all six call sites pass identifiers matching the declared unions), andmake testspasses (21499 tests, 95 skipped as usual).Committed as
a3d250c20and pushed to the PR branch.