Skip to content

Fix phpstan/phpstan#9733: PHPDoc types from overridden function are not included in trait typeinfo when aliased#5195

Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-ausp0az
Open

Fix phpstan/phpstan#9733: PHPDoc types from overridden function are not included in trait typeinfo when aliased#5195
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-ausp0az

Conversation

@phpstan-bot
Copy link
Collaborator

Summary

When a trait method is aliased using use MyTrait { test as test2; }, PHPDoc types from the overridden parent method were not inherited by the aliased method. This caused false positive missingType.iterableValue errors and incorrect type inference inside the trait method body.

Changes

  • Added resolveOriginalMethodName() helper method to src/PhpDoc/PhpDocInheritanceResolver.php that resolves trait alias names back to original method names using ClassReflection::getNativeReflection()->getTraitAliases()
  • Modified resolvePhpDocForMethod() to use the original method name when looking up PHPDoc in parent classes, interfaces, and traits

Root cause

When a trait method test is aliased as test2, PHPStan's PhpDocInheritanceResolver::resolvePhpDocForMethod() was called with the alias name test2. It then tried to find test2 in the parent class (which only has test), in interfaces, and in traits — all lookups failed because none of them have a method named test2. The fix resolves the alias back to the original method name before performing these lookups.

Test

Added tests/PHPStan/Analyser/nsrt/bug-9733.php — an NSRT test with a base class declaring abstract test(array $array) with @param int[] $array, a trait implementing it, and a class using the trait with test as test2. The test asserts that $array inside the trait method body has type array<int>.

Fixes phpstan/phpstan#9733

…t methods

- PhpDocInheritanceResolver now resolves trait aliases to original method names when looking up PHPDoc in parent classes, interfaces, and traits
- Added resolveOriginalMethodName() helper using ClassReflection::getNativeReflection()->getTraitAliases()
- New regression test in tests/PHPStan/Analyser/nsrt/bug-9733.php
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.

1 participant