Fix inherited PHPDoc / annotation type resolution#136
Open
ragulka wants to merge 2 commits intospatie:mainfrom
Open
Fix inherited PHPDoc / annotation type resolution#136ragulka wants to merge 2 commits intospatie:mainfrom
ragulka wants to merge 2 commits intospatie:mainfrom
Conversation
When a child class inherits a property with a PHPDoc @var annotation that references a class (e.g. SomeGenericClass<int, string>), the transpiler resolves the class name using the child class's file imports and namespace — not the parent class's where the annotation is defined. This causes the type to resolve as TypeScriptUnknown when the child class is in a different namespace and doesn't import the referenced class, even though the parent's namespace contains it. The bug is in FindClassNameFqcnAction::execute() which uses $node->getFileName() to load use statements, where $node is the class being transformed (the child), not the class that declares the property and its docblock.
When a child class inherits a property with a @var docblock from a parent class, resolve type references using the declaring (parent) class's namespace context instead of the child's. This fixes cases where the child is in a different namespace and doesn't import the referenced class. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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 child class inherits a property with a
@vardocblock annotation from a parent class in a different namespace, the type transpiler incorrectly resolves class references using the child class's namespace context. This causes types likeSimpleGenericClass<int, string>to resolve asTypeScriptUnknownwhen the child doesn't import the referenced class.Example
Transforming
Childwould produceunknownfor the$itemstype instead of correctly resolvingSimpleGenericClass.Fix
In
ClassTransformer::getTypeScriptNode(), when the annotation comes from the property's own@vardocblock (rather than a class-level@propertyor constructor@paramon the current class), use the declaring class's namespace context for type resolution.This distinction matters because:
@propertyand constructor@paramannotations are written in the current class's context and should resolve against its namespace@varannotations belong to the declaring class and should resolve against that class's namespace