Skip to content

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
phpstan-bot:create-pull-request/patch-hinn89z
Open

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

Conversation

@phpstan-bot
Copy link
Collaborator

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 as array instead of array<int> from the parent's @param int[] $array annotation.

Changes

  • Modified src/Analyser/NodeScopeResolver.php to use the originalTraitMethodName AST attribute (which stores the pre-alias method name) when calling PhpDocInheritanceResolver::resolvePhpDocForMethod(), so the parent class method PHPDoc can be correctly found
  • Added regression test tests/PHPStan/Analyser/nsrt/bug-9733.php

Root cause

When a trait method is aliased, NodeScopeResolver renames the method's AST node name from the original (e.g., test) to the alias (e.g., test2) and saves the original as the originalTraitMethodName attribute. However, resolvePhpDocForMethod was called with the aliased name. Since the parent class Base only has a method called test (not test2), the lookup in PhpDocInheritanceResolver failed silently, and no PHPDoc types were inherited.

The fix uses the already-available originalTraitMethodName attribute to pass the original method name to the inheritance resolver.

Test

Added tests/PHPStan/Analyser/nsrt/bug-9733.php which verifies that a trait method aliased via test as test2 correctly inherits the @param int[] $array type from the abstract parent class method, resulting in array<int> instead of plain array.

Fixes phpstan/phpstan#9733

- 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
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