Skip to content

Commit

Permalink
Ability to set preferred language
Browse files Browse the repository at this point in the history
Also some type coverage enhancements and cleanup
  • Loading branch information
brandonferens committed Oct 23, 2024
1 parent 354d87e commit 1e8f80f
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 28 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
9 changes: 5 additions & 4 deletions src/Commands/GenerateEnumsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.");
Expand All @@ -44,12 +45,12 @@ public function handle(): int
/**
* Gather all enum namespaces for searching.
*
* @return Collection<int,class-string<\UnitEnum>>
* @return Collection<int,class-string<UnitEnum>>
*/
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;
}
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/MakeEnumMethodCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class MakeEnumMethodCommand extends GeneratorCommand
/**
* Execute the console command.
*
* @throws FileNotFoundException
* @throws Exception
* @throws FileNotFoundException
*/
public function handle(): ?bool
{
Expand Down
5 changes: 3 additions & 2 deletions src/Concerns/Builders/EnumJsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kirschbaum\Paragon\Concerns\Builders;

use BackedEnum;
use ReflectionEnumBackedCase;
use ReflectionEnumUnitCase;
use ReflectionMethod;

Expand Down Expand Up @@ -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());
Expand All @@ -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 <<<JS
static get {$case->name}() {
Expand Down
5 changes: 3 additions & 2 deletions src/Concerns/Builders/EnumTsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kirschbaum\Paragon\Concerns\Builders;

use BackedEnum;
use ReflectionEnumBackedCase;
use ReflectionEnumUnitCase;
use ReflectionMethod;

Expand Down Expand Up @@ -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());
Expand All @@ -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);

Expand Down
19 changes: 12 additions & 7 deletions src/Concerns/DiscoverEnums.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int,string>|string $path
* @param array<int, string>|string $path
*
* @return Collection<class-string<\UnitEnum>,class-string<\UnitEnum>>
* @return Collection<class-string<UnitEnum>, class-string<UnitEnum>>
*/
public static function within(array|string $path): Collection
{
Expand All @@ -25,13 +26,15 @@ public static function within(array|string $path): Collection
/**
* Filter the files down to only enums.
*
* @param Finder<string,SplFileInfo> $files
* @param Finder<string, SplFileInfo> $files
*
* @return Collection<class-string<\UnitEnum>,class-string<\UnitEnum>>
* @return Collection<class-string<UnitEnum>, class-string<UnitEnum>>
*/
protected static function getEnums(Finder $files): Collection
{
/** @var Collection<int, SplFileInfo> $fileCollection */
/**
* @var Collection<int, SplFileInfo> $fileCollection
*/
$fileCollection = collect($files);

return $fileCollection
Expand All @@ -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<UnitEnum>
*/
protected static function classFromFile(SplFileInfo $file): string
{
/** @var class-string<\UnitEnum> */
/**
* @var class-string<UnitEnum>
*/
return str($file->getRealPath())
->replaceFirst(base_path(), '')
->trim(DIRECTORY_SEPARATOR)
Expand Down
4 changes: 3 additions & 1 deletion src/Generators/AbstractEnumGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ protected function imports(): Collection
return collect();
}

/** @var Collection<int, SplFileInfo> $fileCollection */
/**
* @var Collection<int, SplFileInfo> $fileCollection
*/
$fileCollection = collect($files);

return $fileCollection
Expand Down
22 changes: 12 additions & 10 deletions src/Generators/EnumGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -25,7 +27,7 @@ class EnumGenerator
/**
* Create new EnumGenerator instance.
*
* @param class-string<\UnitEnum> $enum
* @param class-string<UnitEnum> $enum
*/
public function __construct(protected string $enum, protected EnumBuilder $builder)
{
Expand Down Expand Up @@ -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('');
Expand Down Expand Up @@ -142,12 +144,12 @@ protected function valueReturnType(): string
/**
* Build all the case objects.
*
* @param Collection<int,ReflectionEnumUnitCase|ReflectionEnumBackedCase> $cases
* @param Collection<int, ReflectionEnumUnitCase|ReflectionEnumBackedCase> $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()
Expand All @@ -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,
)
Expand All @@ -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<int,string> $methodValues
* @param Collection<int, string> $methodValues
*/
protected function assembleCaseObject(
ReflectionEnumUnitCase|ReflectionEnumBackedCase $case,
Expand All @@ -203,12 +205,12 @@ protected function assembleCaseObject(
/**
* Build all case object getter methods.
*
* @param Collection<int,ReflectionEnumUnitCase|ReflectionEnumBackedCase> $cases
* @param Collection<int, ReflectionEnumUnitCase|ReflectionEnumBackedCase> $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);
}

Expand Down

0 comments on commit 1e8f80f

Please sign in to comment.