fix: Scope interfaces implemented by enums#1233
Open
danielmaier42 wants to merge 1 commit into
Open
Conversation
`NameStmtPrefixer` only prefixed names whose parent node was in an allow-list of supported node types. That list contained `Class_` and `Interface_` but not `Enum_`, so the interface names in an `enum X implements Y` declaration were left untouched while the matching `use` statement was prefixed, producing code that referenced a non-existent (unprefixed) interface. Add `Enum_` to the supported parent nodes so enum `implements` clauses are scoped exactly like class ones. Closes humbug#1119 Co-Authored-By: Claude Opus 4.8 <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.
Problem
Fixes #1119.
When an
enumimplements an interface, PHP-Scoper prefixes theusestatement for the interface but leaves the interface name in theimplementsclause untouched. The generated code then references an interface that no longer exists under that name.Reproduction
Input:
Output before this fix:
The
usestatement was rewritten toHumbug\App\Contracts\HasColor, butimplementsstill points at the original\App\Contracts\HasColor, which is not defined in the scoped output.Cause
NameStmtPrefixeronly prefixes a name when its parent node is in theSUPPORTED_PARENT_NODE_CLASS_NAMESallow-list. That list containedClass_andInterface_but notEnum_, so names appearing in an enum'simplementsclause were skipped entirely.Fix
Add
PhpParser\Node\Stmt\Enum_to the supported parent nodes. Enumimplementsclauses are now scoped exactly like class ones:Tests
enum with interfacespec, whose expected output (implements \HasColor) had codified the buggy behaviour — a user-defined interface in the global namespace should stay unqualified (implements HasColor), matching how classes are handled.usestatement and via a fully-qualified name.Full spec suite (768 cases) and the rest of the PHPUnit suite pass locally.
🤖 Generated with Claude Code