Skip to content

Commit

Permalink
fix issue where string and int enum interfaces could not be found
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Aug 13, 2024
1 parent 131d7a6 commit 5936dec
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/TokenParsers/StructureHeadTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ public function execute(
}

if (! $tokens->get($index)->is([
T_EXTENDS,
T_IMPLEMENTS,
T_STRING,
T_NAME_FULLY_QUALIFIED,
T_NAME_FULLY_QUALIFIED,
T_NAME_RELATIVE,
T_NAME_QUALIFIED,
])) {
T_EXTENDS,
T_IMPLEMENTS,
T_STRING,
T_NAME_FULLY_QUALIFIED,
T_NAME_FULLY_QUALIFIED,
T_NAME_RELATIVE,
T_NAME_QUALIFIED,
]) && $tokens->get($index)->text !== ':') {
break;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Fakes/FakeIntEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Spatie\StructureDiscoverer\Tests\Fakes;

enum FakeIntEnum: int implements FakeChildInterface
{
case A = 'a';

Check failure on line 7 in tests/Fakes/FakeIntEnum.php

View workflow job for this annotation

GitHub Actions / P8.1 - L10.* - prefer-lowest - ubuntu-latest

Enum case type string does not match enum backing type int

Check failure on line 7 in tests/Fakes/FakeIntEnum.php

View workflow job for this annotation

GitHub Actions / P8.1 - L10.* - prefer-stable - ubuntu-latest

Enum case type string does not match enum backing type int
case B = 'b';
case C = 'c';
}
10 changes: 10 additions & 0 deletions tests/Fakes/FakeStringEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Spatie\StructureDiscoverer\Tests\Fakes;

enum FakeStringEnum: string implements FakeChildInterface
{
case A = 'a';
case B = 'b';
case C = 'c';
}
4 changes: 4 additions & 0 deletions tests/ReflectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
use Spatie\StructureDiscoverer\Tests\Fakes\FakeChildClass;
use Spatie\StructureDiscoverer\Tests\Fakes\FakeChildInterface;
use Spatie\StructureDiscoverer\Tests\Fakes\FakeEnum;
use Spatie\StructureDiscoverer\Tests\Fakes\FakeIntEnum;
use Spatie\StructureDiscoverer\Tests\Fakes\FakeRootClass;
use Spatie\StructureDiscoverer\Tests\Fakes\FakeRootInterface;
use Spatie\StructureDiscoverer\Tests\Fakes\FakeStringEnum;
use Spatie\StructureDiscoverer\Tests\Fakes\FakeSubChildClass;
use Spatie\StructureDiscoverer\Tests\Fakes\FakeSubChildInterface;
use Spatie\StructureDiscoverer\Tests\Fakes\FakeTrait;
Expand Down Expand Up @@ -103,8 +105,10 @@
FakeAttribute::class,
FakeChildClass::class,
FakeEnum::class,
FakeIntEnum::class,
FakeChildInterface::class,
FakeRootClass::class,
FakeStringEnum::class,
FakeRootInterface::class,
FakeTrait::class,
FakeNestedClass::class,
Expand Down
37 changes: 33 additions & 4 deletions tests/StructureDiscovererTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Spatie\StructureDiscoverer\Tests\Fakes\FakeIntEnum;
use Spatie\StructureDiscoverer\Tests\Fakes\FakeStringEnum;
use function Pest\Laravel\artisan;

use Spatie\StructureDiscoverer\Cache\FileDiscoverCacheDriver;
Expand Down Expand Up @@ -42,9 +44,11 @@
FakeEnum::class,
FakeChildInterface::class,
FakeRootClass::class,
FakeIntEnum::class,
FakeRootInterface::class,
FakeTrait::class,
FakeNestedClass::class,
FakeStringEnum::class,
FakeNestedInterface::class,
FakeOtherNestedClass::class,
FakeSubChildInterface::class,
Expand Down Expand Up @@ -89,6 +93,8 @@
Discover::in()->enums(),
[
FakeEnum::class,
FakeIntEnum::class,
FakeStringEnum::class,
],
],
'traits' => [
Expand All @@ -104,6 +110,8 @@
),
[
FakeEnum::class,
FakeIntEnum::class,
FakeStringEnum::class,
FakeTrait::class,
],
],
Expand All @@ -126,6 +134,8 @@
FakeSubChildClass::class,
FakeChildClass::class,
FakeEnum::class,
FakeIntEnum::class,
FakeStringEnum::class,
FakeSubChildInterface::class,
]);
});
Expand All @@ -136,6 +146,8 @@
expect($found)->toEqualCanonicalizing([
FakeChildClass::class,
FakeEnum::class,
FakeIntEnum::class,
FakeStringEnum::class,
FakeSubChildInterface::class,
]);
});
Expand Down Expand Up @@ -211,6 +223,8 @@
expect($found)->toEqualCanonicalizing([
FakeChildClass::class,
FakeEnum::class,
FakeIntEnum::class,
FakeStringEnum::class,
FakeSubChildClass::class,
]);
});
Expand Down Expand Up @@ -407,6 +421,8 @@ public function satisfies(DiscoveredStructure $discoveredData): bool
->toEqualCanonicalizing([
FakeChildClass::class,
FakeEnum::class,
FakeIntEnum::class,
FakeStringEnum::class,
FakeSubChildInterface::class,
])
->toEqualCanonicalizing($foundInterfacesB);
Expand All @@ -415,7 +431,7 @@ public function satisfies(DiscoveredStructure $discoveredData): bool
it('can use a discoverer with cache', function () {
$found = StubStructureScout::create()->get();

expect($found)->toBe([FakeEnum::class]);
expect($found)->toBe([FakeEnum::class,FakeIntEnum::class, FakeStringEnum::class, ]);

// Replace cache
StaticDiscoverCacheDriver::$entries['stub'] = [FakeRootClass::class];
Expand All @@ -429,7 +445,7 @@ public function satisfies(DiscoveredStructure $discoveredData): bool
StructureScoutManager::cache([__DIR__.'/Stubs']);

expect(StaticDiscoverCacheDriver::$entries)->toHaveKey('stub');
expect(StaticDiscoverCacheDriver::$entries['stub'])->toBe([FakeEnum::class]);
expect(StaticDiscoverCacheDriver::$entries['stub'])->toBe([FakeEnum::class, FakeIntEnum::class, FakeStringEnum::class, ]);

StructureScoutManager::clear([__DIR__.'/Stubs']);

Expand All @@ -445,7 +461,7 @@ public function satisfies(DiscoveredStructure $discoveredData): bool
StructureScoutManager::cache([]);

expect(StaticDiscoverCacheDriver::$entries)->toHaveKey('stub');
expect(StaticDiscoverCacheDriver::$entries['stub'])->toBe([FakeEnum::class]);
expect(StaticDiscoverCacheDriver::$entries['stub'])->toBe([FakeEnum::class, FakeIntEnum::class, FakeStringEnum::class, ]);

StructureScoutManager::clear([__DIR__.'/Stubs']);

Expand All @@ -458,7 +474,7 @@ public function satisfies(DiscoveredStructure $discoveredData): bool
artisan('structure-scouts:cache');

expect(StaticDiscoverCacheDriver::$entries)->toHaveKey('stub');
expect(StaticDiscoverCacheDriver::$entries['stub'])->toBe([FakeEnum::class]);
expect(StaticDiscoverCacheDriver::$entries['stub'])->toBe([FakeEnum::class, FakeIntEnum::class, FakeStringEnum::class, ]);

artisan('structure-scouts:clear');

Expand All @@ -478,3 +494,16 @@ public function satisfies(DiscoveredStructure $discoveredData): bool
FakeChildInterface::class,
]);
});

it('can discover enums with interfaces', function () {
$found = Discover::in(__DIR__.'/Fakes')
->implementing(FakeChildInterface::class)
->enums()
->get();

expect($found)->toEqualCanonicalizing([
FakeEnum::class,
FakeStringEnum::class,
FakeIntEnum::class,
]);
});
16 changes: 15 additions & 1 deletion tests/TokenParsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ enum IntEnum: int{}
->name->toEqual('IntEnum');
});

it('can resolve implemented interfaces for an enum', function () {
it('can resolve implemented interfaces for a unit enum', function () {
$definition = <<<'PHP'
use Illuminate\Contracts\Support\Arrayable;
Expand All @@ -249,6 +249,20 @@ enum UnitEnum implements EnumInterface, Arrayable {}
->implements->toContain('EnumInterface', Arrayable::class);
});

it('can resolve implemented interfaces for a string enum', function () {
$definition = <<<'PHP'
use Illuminate\Contracts\Support\Arrayable;
enum UnitEnum: string implements EnumInterface, Arrayable {}
PHP;

$discovered = getDiscoveredStructure($definition, structure: 'UnitEnum');

expect($discovered)
->toBeInstanceOf(DiscoveredEnum::class)
->implements->toContain('EnumInterface', Arrayable::class);
});

/**
* Traits
*/
Expand Down

0 comments on commit 5936dec

Please sign in to comment.