From f68c5ee8565b42bdb4c4fbf055f3cf63a0644ab5 Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Mon, 21 Aug 2023 16:38:50 +0200 Subject: [PATCH] Fix disambiguations --- phpstan-baseline.neon | 16 ++++++++-------- src/Commands/Concerns/AsksClass.php | 2 +- src/Commands/Concerns/AsksData.php | 2 +- src/Commands/Concerns/AsksEvent.php | 8 ++++---- src/Commands/Concerns/AsksModel.php | 2 +- .../Controllers/InvokableVideoController.php | 10 ++++++++++ tests/Feature/TestCommandTest.php | 2 ++ 7 files changed, 27 insertions(+), 15 deletions(-) create mode 100644 test-laravel/app/App/Admin/Controllers/InvokableVideoController.php diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 19a6880..7742339 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -31,7 +31,7 @@ parameters: path: src/Commands/BuilderCommand.php - - message: "#^Parameter \\$options of function Laravel\\\\Prompts\\\\select expects array\\\\|Illuminate\\\\Support\\\\Collection\\, Illuminate\\\\Support\\\\Collection\\\\> given\\.$#" + message: "#^Parameter \\$options of function Laravel\\\\Prompts\\\\select expects array\\\\|Illuminate\\\\Support\\\\Collection\\, Illuminate\\\\Support\\\\Collection\\<\\(int\\|string\\), class\\-string\\\\> given\\.$#" count: 1 path: src/Commands/BuilderCommand.php @@ -56,7 +56,7 @@ parameters: path: src/Commands/FactoryCommand.php - - message: "#^Parameter \\$options of function Laravel\\\\Prompts\\\\select expects array\\\\|Illuminate\\\\Support\\\\Collection\\, Illuminate\\\\Support\\\\Collection\\\\> given\\.$#" + message: "#^Parameter \\$options of function Laravel\\\\Prompts\\\\select expects array\\\\|Illuminate\\\\Support\\\\Collection\\, Illuminate\\\\Support\\\\Collection\\<\\(int\\|string\\), class\\-string\\\\> given\\.$#" count: 1 path: src/Commands/FactoryCommand.php @@ -76,7 +76,7 @@ parameters: path: src/Commands/ListenerCommand.php - - message: "#^Method Soyhuce\\\\Somake\\\\Commands\\\\ListenerCommand\\:\\:disambiguateClass\\(\\) should return string but returns int\\|string\\.$#" + message: "#^Method Soyhuce\\\\Somake\\\\Commands\\\\ListenerCommand\\:\\:disambiguateEvent\\(\\) should return string but returns int\\|string\\.$#" count: 1 path: src/Commands/ListenerCommand.php @@ -86,7 +86,7 @@ parameters: path: src/Commands/ListenerCommand.php - - message: "#^Parameter \\$options of function Laravel\\\\Prompts\\\\select expects array\\\\|Illuminate\\\\Support\\\\Collection\\, Illuminate\\\\Support\\\\Collection\\ given\\.$#" + message: "#^Parameter \\$options of function Laravel\\\\Prompts\\\\select expects array\\\\|Illuminate\\\\Support\\\\Collection\\, Illuminate\\\\Support\\\\Collection\\<\\(int\\|string\\), class\\-string\\> given\\.$#" count: 1 path: src/Commands/ListenerCommand.php @@ -126,7 +126,7 @@ parameters: path: src/Commands/PolicyCommand.php - - message: "#^Parameter \\$options of function Laravel\\\\Prompts\\\\select expects array\\\\|Illuminate\\\\Support\\\\Collection\\, Illuminate\\\\Support\\\\Collection\\\\> given\\.$#" + message: "#^Parameter \\$options of function Laravel\\\\Prompts\\\\select expects array\\\\|Illuminate\\\\Support\\\\Collection\\, Illuminate\\\\Support\\\\Collection\\<\\(int\\|string\\), class\\-string\\\\> given\\.$#" count: 1 path: src/Commands/PolicyCommand.php @@ -156,7 +156,7 @@ parameters: path: src/Commands/RequestCommand.php - - message: "#^Parameter \\$options of function Laravel\\\\Prompts\\\\select expects array\\\\|Illuminate\\\\Support\\\\Collection\\, Illuminate\\\\Support\\\\Collection\\\\> given\\.$#" + message: "#^Parameter \\$options of function Laravel\\\\Prompts\\\\select expects array\\\\|Illuminate\\\\Support\\\\Collection\\, Illuminate\\\\Support\\\\Collection\\<\\(int\\|string\\), class\\-string\\\\> given\\.$#" count: 1 path: src/Commands/RequestCommand.php @@ -191,7 +191,7 @@ parameters: path: src/Commands/ResourceCommand.php - - message: "#^Parameter \\$options of function Laravel\\\\Prompts\\\\select expects array\\\\|Illuminate\\\\Support\\\\Collection\\, Illuminate\\\\Support\\\\Collection\\\\> given\\.$#" + message: "#^Parameter \\$options of function Laravel\\\\Prompts\\\\select expects array\\\\|Illuminate\\\\Support\\\\Collection\\, Illuminate\\\\Support\\\\Collection\\<\\(int\\|string\\), class\\-string\\\\> given\\.$#" count: 1 path: src/Commands/ResourceCommand.php @@ -226,7 +226,7 @@ parameters: path: src/Commands/TestCommand.php - - message: "#^Parameter \\$options of function Laravel\\\\Prompts\\\\select expects array\\\\|Illuminate\\\\Support\\\\Collection\\, Illuminate\\\\Support\\\\Collection\\ given\\.$#" + message: "#^Parameter \\$options of function Laravel\\\\Prompts\\\\select expects array\\\\|Illuminate\\\\Support\\\\Collection\\, Illuminate\\\\Support\\\\Collection\\<\\(int\\|string\\), class\\-string\\> given\\.$#" count: 1 path: src/Commands/TestCommand.php diff --git a/src/Commands/Concerns/AsksClass.php b/src/Commands/Concerns/AsksClass.php index fda0692..3d9f6d7 100644 --- a/src/Commands/Concerns/AsksClass.php +++ b/src/Commands/Concerns/AsksClass.php @@ -82,7 +82,7 @@ private function disambiguateClass(Collection $guessedClasses): string return select( label: 'Which one should I choose ?', - options: $guessedClasses + options: $guessedClasses->keyBy(fn (string $fqcn) => $fqcn) ); } } diff --git a/src/Commands/Concerns/AsksData.php b/src/Commands/Concerns/AsksData.php index 12f1125..a560f40 100644 --- a/src/Commands/Concerns/AsksData.php +++ b/src/Commands/Concerns/AsksData.php @@ -76,7 +76,7 @@ private function disambiguateDatas(Collection $datas): string return select( label: 'Which one should I choose ?', - options: $datas + options: $datas->keyBy(fn (string $fqcn) => $fqcn) ); } } diff --git a/src/Commands/Concerns/AsksEvent.php b/src/Commands/Concerns/AsksEvent.php index d84de99..e00755a 100644 --- a/src/Commands/Concerns/AsksEvent.php +++ b/src/Commands/Concerns/AsksEvent.php @@ -50,7 +50,7 @@ protected function askEvent(string $question, Collection $events): string return match ($guessedEvents->count()) { 0 => $this->qualifyEvent($question, $event), 1 => $guessedEvents->first(), - default => $this->disambiguateClass($guessedEvents) + default => $this->disambiguateEvent($guessedEvents) }; } @@ -71,15 +71,15 @@ private function qualifyEvent(string $question, string $event): string } /** - * @param \Illuminate\Support\Collection $guessedClasses + * @param \Illuminate\Support\Collection $guessedEvents */ - private function disambiguateClass(Collection $guessedClasses): string + private function disambiguateEvent(Collection $guessedEvents): string { warning('I\'m not sure which class you choose...'); return select( label: 'Which one should I choose ?', - options: $guessedClasses + options: $guessedEvents->keyBy(fn (string $fqcn) => $fqcn) ); } } diff --git a/src/Commands/Concerns/AsksModel.php b/src/Commands/Concerns/AsksModel.php index 3c8b7ba..97801dd 100644 --- a/src/Commands/Concerns/AsksModel.php +++ b/src/Commands/Concerns/AsksModel.php @@ -77,7 +77,7 @@ private function disambiguateModel(Collection $models): string return select( label: 'Which one should I choose ?', - options: $models, + options: $models->keyBy(fn (string $fqcn) => $fqcn), ); } } diff --git a/test-laravel/app/App/Admin/Controllers/InvokableVideoController.php b/test-laravel/app/App/Admin/Controllers/InvokableVideoController.php new file mode 100644 index 0000000..3090c53 --- /dev/null +++ b/test-laravel/app/App/Admin/Controllers/InvokableVideoController.php @@ -0,0 +1,10 @@ +artisan('somake:test') ->expectsQuestion('Which kind of test do you want to create ?', 'Feature') ->expectsQuestion('Which controller do you want to cover ?', 'InvokableVideoController') + ->expectsOutputToContain('I\'m not sure which class you choose...') + ->expectsQuestion('Which one should I choose ?', 'App\\Website\\Videos\\Controllers\\InvokableVideoController') ->expectsQuestion('Which method do you want to cover ?', '__invoke') ->expectsQuestion('What is the Test name ?', 'IndexVideosTest') ->expectsOutputToContain('The Tests\\Feature\\Website\\Videos\\InvokableVideoController\\IndexVideosTest class was successfully created !')