From f3038ce6ab61d3b3ff18e86e751da59fdde5a7f7 Mon Sep 17 00:00:00 2001 From: macocci7 Date: Sun, 14 Apr 2024 17:16:01 +0900 Subject: [PATCH 1/4] Fix type error in suggest with collection --- src/SuggestPrompt.php | 3 ++- tests/Feature/SuggestPromptTest.php | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/SuggestPrompt.php b/src/SuggestPrompt.php index 85f2bf7b..d4ff08af 100644 --- a/src/SuggestPrompt.php +++ b/src/SuggestPrompt.php @@ -91,7 +91,8 @@ public function matches(): array } if ($this->options instanceof Closure) { - return $this->matches = array_values(($this->options)($this->value())); + $matches = ($this->options)($this->value()); + return $this->matches = array_values($matches instanceof Collection ? $matches->toArray() : $matches); } return $this->matches = array_values(array_filter($this->options, function ($option) { diff --git a/tests/Feature/SuggestPromptTest.php b/tests/Feature/SuggestPromptTest.php index e0eae8e1..dd23d9c7 100644 --- a/tests/Feature/SuggestPromptTest.php +++ b/tests/Feature/SuggestPromptTest.php @@ -98,6 +98,26 @@ expect($result)->toBe('Blue'); }); +it('accepts a callback returning a collection', function () { + Prompt::fake(['b', Key::TAB, Key::ENTER]); + + $result = suggest( + label: 'What is your favorite color?', + options: fn ($value) => collect([ + 'Red', + 'Green', + 'Blue', + ])->filter( + fn ($name) => str_contains( + strtoupper($name), + strtoupper($value) + ) + ) + ); + + expect($result)->toBe('Blue'); +}); + it('validates', function () { Prompt::fake([Key::ENTER, 'X', Key::ENTER]); From c22ad613e4ce594629468d0c64a46410b0b85923 Mon Sep 17 00:00:00 2001 From: macocci7 Date: Sun, 14 Apr 2024 18:06:15 +0900 Subject: [PATCH 2/4] add @phpstan-ignore-next-line --- src/SuggestPrompt.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/SuggestPrompt.php b/src/SuggestPrompt.php index d4ff08af..6002f612 100644 --- a/src/SuggestPrompt.php +++ b/src/SuggestPrompt.php @@ -92,6 +92,7 @@ public function matches(): array if ($this->options instanceof Closure) { $matches = ($this->options)($this->value()); + // @phpstan-ignore-next-line return $this->matches = array_values($matches instanceof Collection ? $matches->toArray() : $matches); } From f7ae83d6392495291694a264780bdd432f917f17 Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Tue, 16 Apr 2024 09:50:41 +1000 Subject: [PATCH 3/4] Update types --- src/SuggestPrompt.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/SuggestPrompt.php b/src/SuggestPrompt.php index 6002f612..b666011a 100644 --- a/src/SuggestPrompt.php +++ b/src/SuggestPrompt.php @@ -14,7 +14,7 @@ class SuggestPrompt extends Prompt /** * The options for the suggest prompt. * - * @var array|Closure(string): array + * @var array|Closure(string): (array|Collection) */ public array|Closure $options; @@ -28,7 +28,7 @@ class SuggestPrompt extends Prompt /** * Create a new SuggestPrompt instance. * - * @param array|Collection|Closure(string): array $options + * @param array|Collection|Closure(string): (array|Collection) $options */ public function __construct( public string $label, @@ -92,7 +92,7 @@ public function matches(): array if ($this->options instanceof Closure) { $matches = ($this->options)($this->value()); - // @phpstan-ignore-next-line + return $this->matches = array_values($matches instanceof Collection ? $matches->toArray() : $matches); } From 3c2a3f5631f0a72c3dfe4b6b169e0703a32dcd70 Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Tue, 16 Apr 2024 09:54:05 +1000 Subject: [PATCH 4/4] Replace expensive operation --- src/SuggestPrompt.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SuggestPrompt.php b/src/SuggestPrompt.php index b666011a..dd8d2553 100644 --- a/src/SuggestPrompt.php +++ b/src/SuggestPrompt.php @@ -93,7 +93,7 @@ public function matches(): array if ($this->options instanceof Closure) { $matches = ($this->options)($this->value()); - return $this->matches = array_values($matches instanceof Collection ? $matches->toArray() : $matches); + return $this->matches = array_values($matches instanceof Collection ? $matches->all() : $matches); } return $this->matches = array_values(array_filter($this->options, function ($option) {