From 3943b2c487a4b938ce9271bb07673ddc9861e086 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 6 Jan 2025 13:26:04 -0500 Subject: [PATCH 1/5] Handle parsing anonymous classes --- src/TokenParsers/FileTokenParser.php | 31 ++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/TokenParsers/FileTokenParser.php b/src/TokenParsers/FileTokenParser.php index 8a88399..e686937 100644 --- a/src/TokenParsers/FileTokenParser.php +++ b/src/TokenParsers/FileTokenParser.php @@ -5,7 +5,6 @@ use Spatie\StructureDiscoverer\Collections\TokenCollection; use Spatie\StructureDiscoverer\Collections\UsageCollection; use Spatie\StructureDiscoverer\Data\DiscoveredStructure; -use Spatie\StructureDiscoverer\Data\Token; use Spatie\StructureDiscoverer\Enums\DiscoveredStructureType; use Spatie\StructureDiscoverer\Exceptions\CouldNotParseFile; use Throwable; @@ -20,7 +19,6 @@ public function __construct( ) { } - /** @return array */ public function execute( string $filename, string $contents, @@ -39,8 +37,7 @@ public function execute( $structureDefined = false; $index = 0; - - + try { do { if ($tokens->get($index)->is(T_NAMESPACE)) { @@ -74,6 +71,15 @@ public function execute( continue; } + if ( + $type === DiscoveredStructureType::ClassDefinition + && $this->isAnonymousClass($tokens, $index) + ) { + $index++; + + continue; + } + $discoveredStructure = $this->discoveredDataResolver->execute( $index + 1, $tokens, @@ -97,4 +103,21 @@ public function execute( return $found; } + + private function isAnonymousClass(TokenCollection $tokens, int $index): bool + { + $prevIndex = $index - 1; + + // find the previous non-whitespace token + while ($prevIndex >= 0 && $tokens->get($prevIndex)->is(T_WHITESPACE)) { + $prevIndex--; + } + + // if the token before T_CLASS is T_NEW, it's an anonymous class + if ($prevIndex >= 0 && $tokens->get($prevIndex)->is(T_NEW)) { + return true; + } + + return false; + } } From ddd9e3c797362ae5fe8e5005fbf85838b26dc998 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 6 Jan 2025 13:26:09 -0500 Subject: [PATCH 2/5] Add tests --- tests/Fakes/FakeWithAnonymousClass.php | 15 +++++++++++++++ tests/Fakes/FakeWithMultipleClasses.php | 11 +++++++++++ tests/StructureDiscovererTest.php | 23 +++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 tests/Fakes/FakeWithAnonymousClass.php create mode 100644 tests/Fakes/FakeWithMultipleClasses.php diff --git a/tests/Fakes/FakeWithAnonymousClass.php b/tests/Fakes/FakeWithAnonymousClass.php new file mode 100644 index 0000000..6b6bf43 --- /dev/null +++ b/tests/Fakes/FakeWithAnonymousClass.php @@ -0,0 +1,15 @@ +get(); + + expect($found)->not->toContain("Spatie\StructureDiscoverer\Tests\Fakes\("); +}); + +it('can parse multiple nested classes', function () { + $found = Discover::in(__DIR__ . '/Fakes')->get(); + + expect($found)->toContain("Spatie\StructureDiscoverer\Tests\Fakes\FakeWithMultipleClassesSub"); }); From 695b39572dcfcce27ccb7ca4f04431bd180ad4db Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 6 Jan 2025 13:33:22 -0500 Subject: [PATCH 3/5] Re-add return type --- src/TokenParsers/FileTokenParser.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/TokenParsers/FileTokenParser.php b/src/TokenParsers/FileTokenParser.php index e686937..2bcf1c2 100644 --- a/src/TokenParsers/FileTokenParser.php +++ b/src/TokenParsers/FileTokenParser.php @@ -19,6 +19,7 @@ public function __construct( ) { } + /** @return array */ public function execute( string $filename, string $contents, @@ -37,7 +38,7 @@ public function execute( $structureDefined = false; $index = 0; - + try { do { if ($tokens->get($index)->is(T_NAMESPACE)) { From c32caffed87bccef69aa7356bdef4c7d4117dceb Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 6 Jan 2025 13:44:00 -0500 Subject: [PATCH 4/5] Fix tests --- tests/ReflectionTest.php | 4 ++++ tests/StructureDiscovererTest.php | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/ReflectionTest.php b/tests/ReflectionTest.php index 82e103b..4cc9584 100644 --- a/tests/ReflectionTest.php +++ b/tests/ReflectionTest.php @@ -21,6 +21,8 @@ use Spatie\StructureDiscoverer\Tests\Fakes\FakeSubChildClass; use Spatie\StructureDiscoverer\Tests\Fakes\FakeSubChildInterface; use Spatie\StructureDiscoverer\Tests\Fakes\FakeTrait; +use Spatie\StructureDiscoverer\Tests\Fakes\FakeWithAnonymousClass; +use Spatie\StructureDiscoverer\Tests\Fakes\FakeWithMultipleClasses; use Spatie\StructureDiscoverer\Tests\Fakes\Nested\FakeNestedClass; use Spatie\StructureDiscoverer\Tests\Fakes\Nested\FakeNestedInterface; use Spatie\StructureDiscoverer\Tests\Fakes\OtherNested\FakeOtherNestedClass; @@ -116,7 +118,9 @@ FakeOtherNestedClass::class, FakeSubChildInterface::class, FakeSubChildClass::class, + FakeWithMultipleClasses::class, FakeClassDepender::class, FakeInterfaceDepender::class, + FakeWithAnonymousClass::class, ]); }); diff --git a/tests/StructureDiscovererTest.php b/tests/StructureDiscovererTest.php index b5fc740..5819f2e 100644 --- a/tests/StructureDiscovererTest.php +++ b/tests/StructureDiscovererTest.php @@ -83,6 +83,9 @@ FakeOtherNestedClass::class, FakeSubChildClass::class, FakeClassDepender::class, + FakeWithAnonymousClass::class, + FakeWithMultipleClasses::class, + FakeWithMultipleClassesSub::class, ], ], 'interfaces' => [ @@ -508,7 +511,7 @@ public function satisfies(DiscoveredStructure $discoveredData): bool }); it('can discover enums with interfaces', function () { - $found = Discover::in(__DIR__.'/Fakes') + $found = Discover::in(__DIR__ . '/Fakes') ->implementing(FakeChildInterface::class) ->enums() ->get(); @@ -518,6 +521,7 @@ public function satisfies(DiscoveredStructure $discoveredData): bool FakeStringEnum::class, FakeIntEnum::class, ]); +}); it('can parse anonymous classes', function () { $found = Discover::in(__DIR__ . '/Fakes')->get(); From e39b0e47c9dee40f2023f6c82b50850f242a2130 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 6 Jan 2025 13:45:59 -0500 Subject: [PATCH 5/5] CS fixes --- tests/StructureDiscovererTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/StructureDiscovererTest.php b/tests/StructureDiscovererTest.php index 5819f2e..f1cc401 100644 --- a/tests/StructureDiscovererTest.php +++ b/tests/StructureDiscovererTest.php @@ -511,7 +511,7 @@ public function satisfies(DiscoveredStructure $discoveredData): bool }); it('can discover enums with interfaces', function () { - $found = Discover::in(__DIR__ . '/Fakes') + $found = Discover::in(__DIR__.'/Fakes') ->implementing(FakeChildInterface::class) ->enums() ->get(); @@ -524,13 +524,13 @@ public function satisfies(DiscoveredStructure $discoveredData): bool }); it('can parse anonymous classes', function () { - $found = Discover::in(__DIR__ . '/Fakes')->get(); + $found = Discover::in(__DIR__.'/Fakes')->get(); expect($found)->not->toContain("Spatie\StructureDiscoverer\Tests\Fakes\("); }); it('can parse multiple nested classes', function () { - $found = Discover::in(__DIR__ . '/Fakes')->get(); + $found = Discover::in(__DIR__.'/Fakes')->get(); expect($found)->toContain("Spatie\StructureDiscoverer\Tests\Fakes\FakeWithMultipleClassesSub"); });