From fc9e6c43a00ab11e9a222a900d9f128ab9394eb5 Mon Sep 17 00:00:00 2001 From: "M. D." Date: Fri, 14 Jun 2024 11:47:33 +0200 Subject: [PATCH 1/2] Add little internal helper command to output todos This PR adds an internal command that outputs a list of `todos` (`$isTodo = true`) --- src/Commands/ListTodosCommand.php | 78 ++++++++++++++++++++++++++++ src/FilamentTestsServiceProvider.php | 6 ++- 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/Commands/ListTodosCommand.php diff --git a/src/Commands/ListTodosCommand.php b/src/Commands/ListTodosCommand.php new file mode 100644 index 00000000..02eb0338 --- /dev/null +++ b/src/Commands/ListTodosCommand.php @@ -0,0 +1,78 @@ +files = $files; + } + + public function handle(): int + { + $files = $this->files->allFiles(__DIR__ . '/../Stubs'); + $todos = collect($files) + ->map(fn($file) => $this->getClassFromFile($file->getPathname())) + ->filter(fn($class) => $class && $this->hasTodoProperty($class)) + ->values() + ->all(); + + $this->outputTodos($todos); + + return self::SUCCESS; + } + + protected function getClassFromFile(string $filePath): ?string + { + $contents = $this->files->get($filePath); + $namespace = $this->getNamespaceFromFile($contents); + $className = $this->getClassNameFromFile($contents); + + return $namespace && $className ? $namespace . '\\' . $className : null; + } + + protected function getNamespaceFromFile(string $fileContents): ?string + { + return str($fileContents)->match('/namespace\s+(.+?);/') ?: null; + } + + protected function getClassNameFromFile(string $fileContents): ?string + { + return str($fileContents)->match('/class\s+(\w+)/') ?: null; + } + + protected function hasTodoProperty(string $class): bool + { + if (!class_exists($class)) { + return false; + } + + $reflection = new ReflectionClass($class); + if ($reflection->hasProperty('isTodo')) { + $property = $reflection->getProperty('isTodo'); + $instance = $reflection->newInstanceWithoutConstructor(); + return $property->getValue($instance) === true; + } + + return false; + } + + protected function outputTodos(array $todos): void + { + $this->table(['Todos ' . count($todos)], collect($todos)->map(function ($todo) { + $todo = str($todo)->trim('CodeWithDennis\FilamentTests\Stubs\\'); + return [$todo]; + })->all()); + } + } diff --git a/src/FilamentTestsServiceProvider.php b/src/FilamentTestsServiceProvider.php index 126b1655..6d93dad1 100644 --- a/src/FilamentTestsServiceProvider.php +++ b/src/FilamentTestsServiceProvider.php @@ -3,6 +3,7 @@ namespace CodeWithDennis\FilamentTests; use CodeWithDennis\FilamentTests\Commands\FilamentTestsCommand; +use CodeWithDennis\FilamentTests\Commands\ListTodosCommand; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; @@ -13,7 +14,10 @@ public function configurePackage(Package $package): void $package ->name('filament-tests') ->hasConfigFile() - ->hasCommand(FilamentTestsCommand::class); + ->hasCommands([ + FilamentTestsCommand::class, + ListTodosCommand::class, + ]); } public function boot(): void From 780c6b94895fefc326933b5d784aedb3ff7c00b9 Mon Sep 17 00:00:00 2001 From: dissto Date: Fri, 14 Jun 2024 09:47:54 +0000 Subject: [PATCH 2/2] Pint: Fix code style issues --- src/Commands/ListTodosCommand.php | 119 +++++++++++++++--------------- 1 file changed, 61 insertions(+), 58 deletions(-) diff --git a/src/Commands/ListTodosCommand.php b/src/Commands/ListTodosCommand.php index 02eb0338..6bcd9a63 100644 --- a/src/Commands/ListTodosCommand.php +++ b/src/Commands/ListTodosCommand.php @@ -1,78 +1,81 @@ files = $files; - } + protected $description = '(internal) Output a list of todos for Filament tests'; - public function handle(): int - { - $files = $this->files->allFiles(__DIR__ . '/../Stubs'); - $todos = collect($files) - ->map(fn($file) => $this->getClassFromFile($file->getPathname())) - ->filter(fn($class) => $class && $this->hasTodoProperty($class)) - ->values() - ->all(); + protected Filesystem $files; - $this->outputTodos($todos); + public function __construct(Filesystem $files) + { + parent::__construct(); + $this->files = $files; + } - return self::SUCCESS; - } + public function handle(): int + { + $files = $this->files->allFiles(__DIR__.'/../Stubs'); + $todos = collect($files) + ->map(fn ($file) => $this->getClassFromFile($file->getPathname())) + ->filter(fn ($class) => $class && $this->hasTodoProperty($class)) + ->values() + ->all(); - protected function getClassFromFile(string $filePath): ?string - { - $contents = $this->files->get($filePath); - $namespace = $this->getNamespaceFromFile($contents); - $className = $this->getClassNameFromFile($contents); + $this->outputTodos($todos); - return $namespace && $className ? $namespace . '\\' . $className : null; - } + return self::SUCCESS; + } - protected function getNamespaceFromFile(string $fileContents): ?string - { - return str($fileContents)->match('/namespace\s+(.+?);/') ?: null; - } + protected function getClassFromFile(string $filePath): ?string + { + $contents = $this->files->get($filePath); + $namespace = $this->getNamespaceFromFile($contents); + $className = $this->getClassNameFromFile($contents); - protected function getClassNameFromFile(string $fileContents): ?string - { - return str($fileContents)->match('/class\s+(\w+)/') ?: null; - } + return $namespace && $className ? $namespace.'\\'.$className : null; + } - protected function hasTodoProperty(string $class): bool - { - if (!class_exists($class)) { - return false; - } + protected function getNamespaceFromFile(string $fileContents): ?string + { + return str($fileContents)->match('/namespace\s+(.+?);/') ?: null; + } - $reflection = new ReflectionClass($class); - if ($reflection->hasProperty('isTodo')) { - $property = $reflection->getProperty('isTodo'); - $instance = $reflection->newInstanceWithoutConstructor(); - return $property->getValue($instance) === true; - } + protected function getClassNameFromFile(string $fileContents): ?string + { + return str($fileContents)->match('/class\s+(\w+)/') ?: null; + } + protected function hasTodoProperty(string $class): bool + { + if (! class_exists($class)) { return false; } - protected function outputTodos(array $todos): void - { - $this->table(['Todos ' . count($todos)], collect($todos)->map(function ($todo) { - $todo = str($todo)->trim('CodeWithDennis\FilamentTests\Stubs\\'); - return [$todo]; - })->all()); + $reflection = new ReflectionClass($class); + if ($reflection->hasProperty('isTodo')) { + $property = $reflection->getProperty('isTodo'); + $instance = $reflection->newInstanceWithoutConstructor(); + + return $property->getValue($instance) === true; } + + return false; + } + + protected function outputTodos(array $todos): void + { + $this->table(['Todos '.count($todos)], collect($todos)->map(function ($todo) { + $todo = str($todo)->trim('CodeWithDennis\FilamentTests\Stubs\\'); + + return [$todo]; + })->all()); } +}