Skip to content

Fix #14205: Incorrect: Class referenced with incorrect case,#5064

Open
phpstan-bot wants to merge 2 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-satlznk
Open

Fix #14205: Incorrect: Class referenced with incorrect case,#5064
phpstan-bot wants to merge 2 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-satlznk

Conversation

@phpstan-bot
Copy link
Collaborator

Summary

When a use alias happened to case-insensitively match a different class in the same namespace, PHPStan incorrectly reported a class.nameCase error. For example, with use App\Team\SamenstellingService as TeamSamenstellingService;, PHPStan would report that TeamsamenstellingService (a separate class) was referenced with incorrect case, even though the alias was never meant to refer to that class.

Changes

  • Modified getOriginalClassNamePairsFromTypeNode() in src/Rules/FunctionDefinitionCheck.php to detect use aliases vs case variants
  • Added a check comparing original name parts with the resolved name's suffix parts case-insensitively; if any part differs (not just in case), the original name is a use alias and no "original case" pair is created
  • Added regression test in tests/PHPStan/Rules/Methods/data/bug-14205.php

Root 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's originalName attribute). When a use ... as alias 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 class TeamsamenstellingService, 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.php with two classes (TeamsamenstellingService and SamenstellingService) and a use alias (SamenstellingService as TeamSamenstellingService). The test expects no errors, confirming the false positive is eliminated.

Fixes phpstan/phpstan#14205

- 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
@VincentLanglet
Copy link
Contributor

I pushed on the branch, please take a new look @staabm

Also, I discovered that alias are currently not reported when mismatch casing
https://phpstan.org/r/14ef80f7-a916-4f4b-a1c3-675c2492f383 but that's another fix.

@VincentLanglet VincentLanglet requested a review from staabm March 4, 2026 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants