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
Conversation
…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
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 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 positivemissingType.iterableValueerrors and incorrect type inference inside the trait method body.Changes
resolveOriginalMethodName()helper method tosrc/PhpDoc/PhpDocInheritanceResolver.phpthat resolves trait alias names back to original method names usingClassReflection::getNativeReflection()->getTraitAliases()resolvePhpDocForMethod()to use the original method name when looking up PHPDoc in parent classes, interfaces, and traitsRoot cause
When a trait method
testis aliased astest2, PHPStan'sPhpDocInheritanceResolver::resolvePhpDocForMethod()was called with the alias nametest2. It then tried to findtest2in the parent class (which only hastest), in interfaces, and in traits — all lookups failed because none of them have a method namedtest2. 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 declaringabstract test(array $array)with@param int[] $array, a trait implementing it, and a class using the trait withtest as test2. The test asserts that$arrayinside the trait method body has typearray<int>.Fixes phpstan/phpstan#9733