Skip to content

Commit c585090

Browse files
Fix interface union parsing after ::class references (#3)
1 parent 0263204 commit c585090

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

src/Commands/BaseCommand.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,14 @@ protected function findClassTokenIndex(array $tokens): ?int
182182
for ($lookahead = $index + 1; isset($tokens[$lookahead]); $lookahead++) {
183183
$nextToken = $tokens[$lookahead];
184184

185-
if (!is_array($nextToken)) {
186-
continue;
187-
}
188-
189-
if (in_array($nextToken[0], [T_WHITESPACE, T_COMMENT, T_DOC_COMMENT], true)) {
190-
continue;
191-
}
185+
if (is_array($nextToken)) {
186+
if (in_array($nextToken[0], [T_WHITESPACE, T_COMMENT, T_DOC_COMMENT], true)) {
187+
continue;
188+
}
192189

193-
if ($nextToken[0] === T_STRING) {
194-
return $index;
190+
if ($nextToken[0] === T_STRING) {
191+
return $index;
192+
}
195193
}
196194

197195
break;

tests/Commands/GenerateInterfaceUnionsCommandTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ public function testParserIgnoresClassConstantReferencesBeforeClassDeclaration()
121121
$this->assertSame(['App\\Interfaces\\AnimalInterface'], $info['implements']);
122122
}
123123

124+
public function testParserIgnoresClassConstantReferencesBeforeClassDeclarationWhenFollowedByEnumCase(): void
125+
{
126+
$info = $this->makeParserCommand()->classInfo(
127+
'./workbench/app/Models/AttributeClassAndEnumReferenceAnimal.php'
128+
);
129+
130+
$this->assertSame('AttributeClassAndEnumReferenceAnimal', $info['class']);
131+
$this->assertSame('App\\Models\\AttributeClassAndEnumReferenceAnimal', $info['fqcn']);
132+
$this->assertSame(['App\\Interfaces\\AnimalInterface'], $info['implements']);
133+
}
134+
124135
public function testCommandFailsForInvalidInputPath(): void
125136
{
126137
$this->expectException(\RuntimeException::class);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use App\Attributes\ModelInfo;
6+
use App\Enums\AnimalType;
7+
use App\Interfaces\AnimalInterface;
8+
9+
#[ModelInfo(ParentAnimal::class, AnimalType::Dog)]
10+
class AttributeClassAndEnumReferenceAnimal implements AnimalInterface
11+
{
12+
}

0 commit comments

Comments
 (0)