Fix #14205: Incorrect: Class referenced with incorrect case,#5064
Open
phpstan-bot wants to merge 2 commits intophpstan:2.1.xfrom
Open
Fix #14205: Incorrect: Class referenced with incorrect case,#5064phpstan-bot wants to merge 2 commits intophpstan:2.1.xfrom
phpstan-bot wants to merge 2 commits intophpstan:2.1.xfrom
Conversation
- In getOriginalClassNamePairsFromTypeNode(), detect when original name parts are a use alias (not just a case variant) by comparing them case-insensitively with the resolved name's suffix parts - When parts differ (not just in case), skip creating a pair since the alias name is unrelated to the actual class name - New regression test in tests/PHPStan/Rules/Methods/data/bug-14205.php Closes phpstan/phpstan#14205
staabm
approved these changes
Feb 27, 2026
Contributor
|
I pushed on the branch, please take a new look @staabm Also, I discovered that alias are currently not reported when mismatch casing |
VincentLanglet
approved these changes
Mar 4, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When a
usealias happened to case-insensitively match a different class in the same namespace, PHPStan incorrectly reported aclass.nameCaseerror. For example, withuse App\Team\SamenstellingService as TeamSamenstellingService;, PHPStan would report thatTeamsamenstellingService(a separate class) was referenced with incorrect case, even though the alias was never meant to refer to that class.Changes
getOriginalClassNamePairsFromTypeNode()insrc/Rules/FunctionDefinitionCheck.phpto detect use aliases vs case variantstests/PHPStan/Rules/Methods/data/bug-14205.phpRoot cause
getOriginalClassNamePairsFromTypeNode()reconstructs the "original case" class name by combining the namespace prefix from the resolved name with the original name parts (from PHP-Parser'soriginalNameattribute). When ause ... asalias is involved, the original name is the alias itself, not the target class. The reconstructed name (App\Team\TeamSamenstellingService) was then checked against the reflection provider, which case-insensitively matched it to the unrelated classTeamsamenstellingService, triggering a false positive.The fix detects that the original parts don't case-insensitively match the corresponding resolved parts, identifying it as a use alias rather than a case variant, and skips creating the pair.
Test
Added
tests/PHPStan/Rules/Methods/data/bug-14205.phpwith two classes (TeamsamenstellingServiceandSamenstellingService) and a use alias (SamenstellingService as TeamSamenstellingService). The test expects no errors, confirming the false positive is eliminated.Fixes phpstan/phpstan#14205