Repository navigation
Look up object class names in nullable and mixed unions and class-strings in restricted-usage rules #7006
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
VincentLanglet
merged 4 commits into
phpstan:2.3.x
from
phpstan-bot:create-pull-request/patch-19je90i
Nov 8, 2026
Merged
Look up object class names in nullable and mixed unions and class-strings in restricted-usage rules #7006
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3c35705
Look up object class names in nullable and mixed unions and class-str…
phpstan-bot 8232547
Inline object class name lookup in restricted-usage rules
phpstan-bot 2e262ac
Simplify class name lookup in static restricted-usage rules
phpstan-bot 4914ca9
Test generic types in static restricted-usage rules
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
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
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
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.
can this be simplified to
$referencedClasses = TypeCombinator::removeNull($classTypeResult->getType())->getReferencedClasses();?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.
no, I was wrong.
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.
I didn't change any code, and I didn't make a new commit or rerun the tests for this round. In the comment on
RestrictedStaticMethodCallableUsageRule.php, staabm took back the suggestion to useTypeCombinator::removeNull(...)->getReferencedClasses()("no, I was wrong"), and has since approved the PR. The code is the same as at commit 2e262ac, which the last round'smake tests,make phpstanandmake cspassed on.The suggestion wouldn't have worked anyway.
getReferencedClasses()returns every class mentioned anywhere in a type, including generic type arguments, so it's not the same as the class you access a member on. It also doesn't resolveclass-string<Foo>toFoothe waygetObjectTypeOrClassStringObjectType()does, so$className::doFoo()with aclass-string<StaticRequest>would stop being checked.All four static-access rules still use:
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.
I kept
getObjectTypeOrClassStringObjectType()->getObjectClassNames()and didn't switch togetReferencedClasses(). As you suggested earlier, I added a test case that shows why the current code needs to stay.Why
getReferencedClasses()doesn't work here: it doesn't only return the class whose member is being accessed. It returns every class mentioned anywhere in the type, including generic type arguments.GenericObjectType::getReferencedClasses()adds the referenced classes of each type argument, andGenericClassStringTypepasses the call through to its inner type. ForGenericStaticRequest<StaticRequest>|null, it returns bothGenericStaticRequestandStaticRequest, so the rules would also checkStaticRequest::doFoo(), a class that isn't being called.New test case: I added
testGeneric()totests/PHPStan/Rules/RestrictedUsage/data/bug-15681.php. It does static method calls, static callables, static property reads and class constant reads on:GenericStaticRequest<StaticRequest>|nullclass-string<GenericStaticRequest<StaticRequest>>StaticRequestalso has adoFoo()method, a$fooproperty and aFOOconstant, which makes the difference visible. I added the expected errors to the four static-rule tests.I ran it both ways:
GenericStaticRequestand once forStaticRequest. InRestrictedStaticMethodUsageRuleTest, lines 115 and 120 each showed twoCannot call doFooerrors.make testspasses (23,546 tests, 108 skipped), andmake phpstanandmake csare clean. The test is committed as 4914ca9 and not pushed.