Skip to content

Commit a97c88f

Browse files
Daniel Maierclaude
andcommitted
fix: Scope interfaces implemented by enums
`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 #1119 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 7806c70 commit a97c88f

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

specs/enum/declaration.php

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,60 @@ enum Status implements HasColor {
9696
9797
namespace Humbug;
9898
99-
enum Status implements \HasColor
99+
enum Status implements HasColor
100+
{
101+
case DRAFT = 'draft';
102+
case PUBLISHED = 'published';
103+
case ARCHIVED = 'archived';
104+
}
105+
106+
PHP,
107+
108+
'enum implementing an interface imported via a use statement' => <<<'PHP'
109+
<?php
110+
111+
namespace Acme;
112+
113+
use App\Contracts\HasColor;
114+
115+
enum Status: string implements HasColor {
116+
case DRAFT = 'draft';
117+
case PUBLISHED = 'published';
118+
case ARCHIVED = 'archived';
119+
}
120+
121+
----
122+
<?php
123+
124+
namespace Humbug\Acme;
125+
126+
use Humbug\App\Contracts\HasColor;
127+
enum Status : string implements HasColor
128+
{
129+
case DRAFT = 'draft';
130+
case PUBLISHED = 'published';
131+
case ARCHIVED = 'archived';
132+
}
133+
134+
PHP,
135+
136+
'enum implementing an interface referenced via a FQ name' => <<<'PHP'
137+
<?php
138+
139+
namespace Acme;
140+
141+
enum Status: string implements \App\Contracts\HasColor {
142+
case DRAFT = 'draft';
143+
case PUBLISHED = 'published';
144+
case ARCHIVED = 'archived';
145+
}
146+
147+
----
148+
<?php
149+
150+
namespace Humbug\Acme;
151+
152+
enum Status : string implements \Humbug\App\Contracts\HasColor
100153
{
101154
case DRAFT = 'draft';
102155
case PUBLISHED = 'published';

src/PhpParser/NodeVisitor/NameStmtPrefixer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use PhpParser\Node\Stmt\Catch_;
4141
use PhpParser\Node\Stmt\Class_;
4242
use PhpParser\Node\Stmt\ClassMethod;
43+
use PhpParser\Node\Stmt\Enum_;
4344
use PhpParser\Node\Stmt\Function_;
4445
use PhpParser\Node\Stmt\Interface_;
4546
use PhpParser\Node\Stmt\Property;
@@ -79,6 +80,7 @@ final class NameStmtPrefixer extends NodeVisitorAbstract
7980
ClassConstFetch::class,
8081
ClassMethod::class,
8182
Closure::class,
83+
Enum_::class,
8284
FuncCall::class,
8385
Function_::class,
8486
Instanceof_::class,

0 commit comments

Comments
 (0)