diff --git a/composer.json b/composer.json index 6f1cb73..9281f45 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,8 @@ "laravel/pint": "^1.18", "orchestra/testbench": "^9.5", "pestphp/pest": "^3.2", - "pestphp/pest-plugin-laravel": "^3.0" + "pestphp/pest-plugin-laravel": "^3.0", + "pestphp/pest-plugin-type-coverage": "^3.1" }, "autoload": { "psr-4": { diff --git a/src/Commands/GenerateEnumsCommand.php b/src/Commands/GenerateEnumsCommand.php index 6efd219..e3b5d7f 100644 --- a/src/Commands/GenerateEnumsCommand.php +++ b/src/Commands/GenerateEnumsCommand.php @@ -17,6 +17,7 @@ use ReflectionEnum; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; +use UnitEnum; #[AsCommand(name: 'paragon:generate-enums', description: 'Generate Typescript versions of existing PHP enums')] class GenerateEnumsCommand extends Command @@ -29,7 +30,7 @@ public function handle(): int $builder = $this->builder(); $generatedEnums = $this->enums() - ->map(fn ($enum) => app(EnumGenerator::class, ['enum' => $enum, 'builder' => $builder])()) + ->map(fn (string $enum) => app(EnumGenerator::class, ['enum' => $enum, 'builder' => $builder])()) ->filter(); $this->components->info("{$generatedEnums->count()} enums have been (re)generated."); @@ -44,12 +45,12 @@ public function handle(): int /** * Gather all enum namespaces for searching. * - * @return Collection> + * @return Collection> */ protected function enums(): Collection { return DiscoverEnums::within(app_path(config()->string('paragon.enums.paths.php'))) - ->reject(function ($enum) { + ->reject(function (string $enum) { if (! enum_exists($enum)) { return true; } @@ -72,7 +73,7 @@ protected function builder(): EnumBuilder $builder = match (true) { $this->option('javascript') => EnumJsBuilder::class, $this->option('typescript') => EnumTsBuilder::class, - default => GenerateAs::from(config('paragon.generate-as'))?->builder() + default => GenerateAs::from(config()->string('paragon.generate-as'))->builder() }; return app($builder); diff --git a/src/Commands/MakeEnumMethodCommand.php b/src/Commands/MakeEnumMethodCommand.php index d8a8819..55f27d7 100644 --- a/src/Commands/MakeEnumMethodCommand.php +++ b/src/Commands/MakeEnumMethodCommand.php @@ -21,8 +21,8 @@ class MakeEnumMethodCommand extends GeneratorCommand /** * Execute the console command. * - * @throws FileNotFoundException * @throws Exception + * @throws FileNotFoundException */ public function handle(): ?bool { diff --git a/src/Concerns/Builders/EnumJsBuilder.php b/src/Concerns/Builders/EnumJsBuilder.php index 53d98bf..11faa06 100644 --- a/src/Concerns/Builders/EnumJsBuilder.php +++ b/src/Concerns/Builders/EnumJsBuilder.php @@ -3,6 +3,7 @@ namespace Kirschbaum\Paragon\Concerns\Builders; use BackedEnum; +use ReflectionEnumBackedCase; use ReflectionEnumUnitCase; use ReflectionMethod; @@ -35,7 +36,7 @@ public function fileExtension(): string /** * Prepare the method and its respective values so it can get injected into the case object. */ - public function caseMethod(ReflectionMethod $method, ReflectionEnumUnitCase $case): string + public function caseMethod(ReflectionMethod $method, ReflectionEnumUnitCase|ReflectionEnumBackedCase $case): string { $value = $case->getValue()->{$method->getName()}(); $class = class_basename($method->getDeclaringClass()->getName()); @@ -53,7 +54,7 @@ public function caseMethod(ReflectionMethod $method, ReflectionEnumUnitCase $cas /** * Assemble the static getter method code for the enum case object. */ - public function assembleCaseGetter(ReflectionEnumUnitCase $case): string + public function assembleCaseGetter(ReflectionEnumUnitCase|ReflectionEnumBackedCase $case): string { return <<name}() { diff --git a/src/Concerns/Builders/EnumTsBuilder.php b/src/Concerns/Builders/EnumTsBuilder.php index e3dc99f..9354de1 100644 --- a/src/Concerns/Builders/EnumTsBuilder.php +++ b/src/Concerns/Builders/EnumTsBuilder.php @@ -3,6 +3,7 @@ namespace Kirschbaum\Paragon\Concerns\Builders; use BackedEnum; +use ReflectionEnumBackedCase; use ReflectionEnumUnitCase; use ReflectionMethod; @@ -35,7 +36,7 @@ public function fileExtension(): string /** * Prepare the method and its respective values so it can get injected into the case object. */ - public function caseMethod(ReflectionMethod $method, ReflectionEnumUnitCase $case): string + public function caseMethod(ReflectionMethod $method, ReflectionEnumUnitCase|ReflectionEnumBackedCase $case): string { $value = $case->getValue()->{$method->getName()}(); $class = class_basename($method->getDeclaringClass()->getName()); @@ -53,7 +54,7 @@ public function caseMethod(ReflectionMethod $method, ReflectionEnumUnitCase $cas /** * Assemble the static getter method code for the enum case object. */ - public function assembleCaseGetter(ReflectionEnumUnitCase $case): string + public function assembleCaseGetter(ReflectionEnumUnitCase|ReflectionEnumBackedCase $case): string { $class = class_basename($case->getDeclaringClass()->name); diff --git a/src/Concerns/DiscoverEnums.php b/src/Concerns/DiscoverEnums.php index cc1b980..1ccf98d 100644 --- a/src/Concerns/DiscoverEnums.php +++ b/src/Concerns/DiscoverEnums.php @@ -7,15 +7,16 @@ use ReflectionException; use SplFileInfo; use Symfony\Component\Finder\Finder; +use UnitEnum; class DiscoverEnums { /** * Get all the enums by searching the given directory. * - * @param array|string $path + * @param array|string $path * - * @return Collection,class-string<\UnitEnum>> + * @return Collection, class-string> */ public static function within(array|string $path): Collection { @@ -25,13 +26,15 @@ public static function within(array|string $path): Collection /** * Filter the files down to only enums. * - * @param Finder $files + * @param Finder $files * - * @return Collection,class-string<\UnitEnum>> + * @return Collection, class-string> */ protected static function getEnums(Finder $files): Collection { - /** @var Collection $fileCollection */ + /** + * @var Collection $fileCollection + */ $fileCollection = collect($files); return $fileCollection @@ -56,11 +59,13 @@ protected static function getEnums(Finder $files): Collection /** * Extract the class name from the given file path. * - * @return class-string<\UnitEnum> + * @return class-string */ protected static function classFromFile(SplFileInfo $file): string { - /** @var class-string<\UnitEnum> */ + /** + * @var class-string + */ return str($file->getRealPath()) ->replaceFirst(base_path(), '') ->trim(DIRECTORY_SEPARATOR) diff --git a/src/Generators/AbstractEnumGenerator.php b/src/Generators/AbstractEnumGenerator.php index 6241dc1..6e2ff0b 100644 --- a/src/Generators/AbstractEnumGenerator.php +++ b/src/Generators/AbstractEnumGenerator.php @@ -56,7 +56,9 @@ protected function imports(): Collection return collect(); } - /** @var Collection $fileCollection */ + /** + * @var Collection $fileCollection + */ $fileCollection = collect($files); return $fileCollection diff --git a/src/Generators/EnumGenerator.php b/src/Generators/EnumGenerator.php index 2e33f7d..8e8aaa9 100644 --- a/src/Generators/EnumGenerator.php +++ b/src/Generators/EnumGenerator.php @@ -7,12 +7,14 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Fluent; +use Illuminate\Support\Stringable; use Kirschbaum\Paragon\Concerns\Builders\EnumBuilder; use Kirschbaum\Paragon\Concerns\IgnoreParagon; use ReflectionEnum; use ReflectionEnumBackedCase; use ReflectionEnumUnitCase; use ReflectionMethod; +use UnitEnum; class EnumGenerator { @@ -25,7 +27,7 @@ class EnumGenerator /** * Create new EnumGenerator instance. * - * @param class-string<\UnitEnum> $enum + * @param class-string $enum */ public function __construct(protected string $enum, protected EnumBuilder $builder) { @@ -107,11 +109,11 @@ protected function relativePath(): string protected function buildTypeDefinition(): string { return $this->methods() - ->map(fn ($method) => PHP_EOL . " {$method->getName()}();") + ->map(fn (ReflectionMethod $method) => PHP_EOL . " {$method->getName()}();") ->sortDesc() ->when( $this->reflector->isBacked(), - fn ($collection) => $collection->push(PHP_EOL . " value: {$this->valueReturnType()};") + fn (Collection $collection) => $collection->push(PHP_EOL . " value: {$this->valueReturnType()};") ) ->reverse() ->join(''); @@ -142,12 +144,12 @@ protected function valueReturnType(): string /** * Build all the case objects. * - * @param Collection $cases + * @param Collection $cases */ protected function buildCases(Collection $cases): string { return $cases - ->map(function (ReflectionEnumUnitCase $case) { + ->map(function (ReflectionEnumUnitCase|ReflectionEnumBackedCase $case) { $value = $this->caseValueProperty($case); $methodValues = $this->methods() @@ -168,10 +170,10 @@ protected function caseValueProperty(ReflectionEnumUnitCase|ReflectionEnumBacked ->prepend(PHP_EOL . ' ') ->when( $this->reflector->getBackingType()->getName() === 'int', - fn ($string) => $case->getValue() instanceof BackedEnum + fn (Stringable $string) => $case->getValue() instanceof BackedEnum ? $string->append("{$case->getValue()->value}") : $string, - fn ($string) => $case->getValue() instanceof BackedEnum + fn (Stringable $string) => $case->getValue() instanceof BackedEnum ? $string->append("'{$case->getValue()->value}'") : $string, ) @@ -184,7 +186,7 @@ protected function caseValueProperty(ReflectionEnumUnitCase|ReflectionEnumBacked /** * Assemble the actual enum case object code including the name, value if needed, and any public methods. * - * @param Collection $methodValues + * @param Collection $methodValues */ protected function assembleCaseObject( ReflectionEnumUnitCase|ReflectionEnumBackedCase $case, @@ -203,12 +205,12 @@ protected function assembleCaseObject( /** * Build all case object getter methods. * - * @param Collection $cases + * @param Collection $cases */ protected function buildGetters(Collection $cases): string { return $cases - ->map(fn ($case) => $this->builder->assembleCaseGetter($case)) + ->map(fn (ReflectionEnumUnitCase|ReflectionEnumBackedCase $case) => $this->builder->assembleCaseGetter($case)) ->join(PHP_EOL . PHP_EOL); }