fix(attribute): process Attribute nodes in leaveNode for cross-file enum resolution - #13
Closed
yuan-dian wants to merge 1 commit into
Closed
Conversation
Move RuntimeAttributeFactoryLowering's Attribute argument processing from enterNode to leaveNode. In enterNode, NameResolver has not yet resolved Name nodes in attribute argument expressions, causing resolveClassConstFetchClass() to fall back to incorrect namespace resolution (e.g. 'App\BenchModels\IdType' instead of 'App\Enums\IdType'). This breaks enum case detection for cross-file enum attributes, resulting in the factory being created but not marked as lazy, which causes the PHPX bridge to store raw string values instead of calling the factory function. In leaveNode, all child nodes have been traversed and NameResolver has replaced Name nodes with Name\FullyQualified nodes containing correct resolvedName attributes, allowing isEnumCaseFetch() to correctly identify enum cases across files.
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.
Bug
When an enum from a different file is used as an attribute argument (e.g.
#[TableId(IdType::AUTO)]), theRuntimeAttributeFactoryLoweringpass fails to detect the enum case and does not mark the factory as lazy. The PHPX bridge then stores the raw string backing value instead of calling the factory function, causing aTypeErrorat runtime.Root Cause
RuntimeAttributeFactoryLoweringprocessedAttributenodes inenterNode(). At that point,NodeTraverserhas not yet descended into the Attribute's argument expressions, soNameResolverhasn't resolvedName('IdType')→Name\FullyQualified('App\Enums\IdType').The fallback in
resolveClassConstFetchClass()uses$this->namespace . '\\' . $name, producing the wrong namespace (e.g.App\BenchModels\IdType), soisDeclaredEnumCase()returnsfalseand the factory is not marked as lazy.Traversal Order Issue
Fix
Move Attribute argument processing from
enterNode()toleaveNode(). InleaveNode(), all child nodes have been traversed andNameResolverhas replacedNamenodes withName\FullyQualifiednodes containing correctresolvedNameattributes, allowingisEnumCaseFetch()to correctly identify enum cases across files.Files Changed
src/Transform/RuntimeAttributeFactoryLowering.php— 29 lines added, 27 removed (net +2)Before (broken)
After (fixed)
Reproduction
TypeError: must be 'object', got 'string'