diff --git a/src/Commands/EnumAnnotateCommand.php b/src/Commands/EnumAnnotateCommand.php index b39cb93..82dd825 100644 --- a/src/Commands/EnumAnnotateCommand.php +++ b/src/Commands/EnumAnnotateCommand.php @@ -5,6 +5,7 @@ namespace Datomatic\LaravelEnumHelper\Commands; use Composer\ClassMapGenerator\ClassMapGenerator; +use Datomatic\LaravelEnumHelper\LaravelEnumHelper; use Illuminate\Console\Command; use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Filesystem\Filesystem; @@ -52,13 +53,13 @@ protected function annotateFolder(): int if (count($searchDirectoryMap) > 0) { foreach ($searchDirectoryMap as $class => $_) { - if(!enum_exists($class)){ + if (! enum_exists($class)) { continue; } $reflection = new ReflectionEnum($class); - if ($reflection->isSubclassOf(UnitEnum::class)) { + if ($reflection->isSubclassOf(UnitEnum::class) && $this->usesEnumHelperTrait($class)) { $this->annotate($reflection); } } @@ -71,6 +72,11 @@ protected function annotateFolder(): int return self::FAILURE; } + protected function usesEnumHelperTrait(string $className): bool + { + return in_array(LaravelEnumHelper::class, class_uses_recursive($className)); + } + /** * @throws ReflectionException|FileNotFoundException */ @@ -84,9 +90,9 @@ protected function annotateClass(string $className): int } $reflection = new ReflectionEnum($className); - + if ($reflection->isSubclassOf(UnitEnum::class)) { - if (class_uses_recursive($class)['Datomatic\LaravelEnumHelper\LaravelEnumHelper'] ?? false) { + if ($this->usesEnumHelperTrait($className)) { $this->annotate($reflection); } } diff --git a/tests/Feature/EnumAnnotateCommandTest.php b/tests/Feature/EnumAnnotateCommandTest.php index baf9adb..d06bfaa 100644 --- a/tests/Feature/EnumAnnotateCommandTest.php +++ b/tests/Feature/EnumAnnotateCommandTest.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use Datomatic\LaravelEnumHelper\Tests\Support\Enums\DoesntUseEnumHelperTrait; + beforeEach(function () { if (! file_exists(app_path('Enums'))) { mkdir(app_path('Enums'), 0755, true); @@ -43,7 +45,6 @@ $this->assertEquals(1, substr_count($contents, '@method static string noResponse()')); }); - it('can be success single file int backed enum', function () { $this->artisan("enum:annotate --folder={$this->withoutDocBlockEnumsFolder} Datomatic\\\\LaravelEnumHelper\\\\Tests\\\\Support\\\\WithoutDocBlockEnums\\\\StatusIntWithoutDocBlock") ->assertSuccessful(); @@ -96,6 +97,13 @@ $this->assertEquals(1, substr_count($contents, '@method static string noResponse()')); }); +it('doesnt annotate enums that dont use LaravelEnumTrait', function () { + $this->artisan('enum:annotate Datomatic\\\\LaravelEnumHelper\\\\Tests\\\\Support\\\\Enums\\\\DoesntUseEnumHelperTrait') + ->assertSuccessful(); + $e = new ReflectionEnum(DoesntUseEnumHelperTrait::class); + $this->assertEquals(false, $e->getDocComment()); +}); + it('can be failed with class', function () { $this->artisan('enum:annotate Datomatic\\\\LaravelEnumHelper\\\\Tests\\\\Support\\\\NotEnums\\\\TestClass') ->assertFailed(); diff --git a/tests/Feature/LaravelEnumHelperTest.php b/tests/Feature/LaravelEnumHelperTest.php index ae36f70..0538c9b 100644 --- a/tests/Feature/LaravelEnumHelperTest.php +++ b/tests/Feature/LaravelEnumHelperTest.php @@ -139,13 +139,13 @@ 'translations with lang and cases param' => [Status::class, [Status::ACCEPTED, Status::NO_RESPONSE], 'it', ['ITA news ACCEPTED', 'ITA news NO_RESPONSE']], ]); -//it('can\'t return an array of method results with method name both singular and plural', function ($enumClass, $cases, $lang) { +// it('can\'t return an array of method results with method name both singular and plural', function ($enumClass, $cases, $lang) { // expect(fn() => $enumClass::news($cases, $lang))->toRuntimeError(); -//})->with([ +// })->with([ // 'enum with method' => [StatusString::class, null, null], // 'enum with method with cases param' => [StatusString::class, [StatusString::DISCARDED, StatusString::ACCEPTED], null], // 'enum with method with lang and cases param' => [StatusString::class, [StatusString::NO_RESPONSE, StatusString::ACCEPTED], 'it'], -//]); +// ]); it('can return an array of translations with magic method', function ($enumClass, $cases, $lang, $result) { expect($enumClass::excerpts($cases, $lang))->toBe($result); diff --git a/tests/Support/Enums/DoesntUseEnumHelperTrait.php b/tests/Support/Enums/DoesntUseEnumHelperTrait.php new file mode 100644 index 0000000..677bcc9 --- /dev/null +++ b/tests/Support/Enums/DoesntUseEnumHelperTrait.php @@ -0,0 +1,11 @@ +