Fix phpstan/phpstan#9733: PHPDoc types from overridden function are not included in trait typeinfo when aliased#5204
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- When a trait method is aliased (e.g., `use MyTrait { test as test2; }`),
the method name in the AST is changed to the alias, but PhpDocInheritanceResolver
needs the original name to find the parent class's PHPDoc types
- Use the `originalTraitMethodName` attribute (already set during AST transformation)
when calling resolvePhpDocForMethod, so the parent method lookup succeeds
- New regression test in tests/PHPStan/Analyser/nsrt/bug-9733.php
Closes phpstan/phpstan#9733
VincentLanglet
approved these changes
Mar 12, 2026
Collaborator
Author
|
Everything is already pushed. Here's the summary:
The PR is ready to merge. |
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 (
use MyTrait { test as test2; }), PHPDoc types from overridden parent class methods were not being inherited. The parameter type would show asarrayinstead ofarray<int>from the parent's@param int[] $arrayannotation.Changes
src/Analyser/NodeScopeResolver.phpto use theoriginalTraitMethodNameAST attribute (which stores the pre-alias method name) when callingPhpDocInheritanceResolver::resolvePhpDocForMethod(), so the parent class method PHPDoc can be correctly foundtests/PHPStan/Analyser/nsrt/bug-9733.phpRoot cause
When a trait method is aliased,
NodeScopeResolverrenames the method's AST node name from the original (e.g.,test) to the alias (e.g.,test2) and saves the original as theoriginalTraitMethodNameattribute. However,resolvePhpDocForMethodwas called with the aliased name. Since the parent classBaseonly has a method calledtest(nottest2), the lookup inPhpDocInheritanceResolverfailed silently, and no PHPDoc types were inherited.The fix uses the already-available
originalTraitMethodNameattribute to pass the original method name to the inheritance resolver.Test
Added
tests/PHPStan/Analyser/nsrt/bug-9733.phpwhich verifies that a trait method aliased viatest as test2correctly inherits the@param int[] $arraytype from the abstract parent class method, resulting inarray<int>instead of plainarray.Fixes phpstan/phpstan#9733