From b431a9f7e6ec5bbc3017baa066a5b994673d9870 Mon Sep 17 00:00:00 2001 From: George Steel Date: Mon, 1 Apr 2024 15:07:29 +0100 Subject: [PATCH] Refine date-selection template types + other minor type inference improvements Signed-off-by: George Steel --- psalm-baseline.xml | 76 ++---------------------------------- src/AbstractDateDropdown.php | 16 ++++---- src/DateSelect.php | 6 +-- src/DateTimeSelect.php | 12 +++--- src/MonthSelect.php | 4 +- 5 files changed, 23 insertions(+), 91 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 88aa3368..92c5d898 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,16 +1,10 @@ - - - - - - @@ -325,9 +319,6 @@ - - - @@ -549,32 +540,6 @@ - - - - - - - - - - - - - - - - - - creationOptions]]> - - - - - - - - @@ -872,23 +837,10 @@ - - - - - - - - - - - - - - - - - + + + + @@ -1108,26 +1060,6 @@ - - - - - - - - - - - $value]]> - $value]]> - - - - - - - - diff --git a/src/AbstractDateDropdown.php b/src/AbstractDateDropdown.php index e122e80c..ff43f611 100644 --- a/src/AbstractDateDropdown.php +++ b/src/AbstractDateDropdown.php @@ -5,7 +5,6 @@ namespace Laminas\Filter; use function array_reduce; -use function assert; use function count; use function is_array; use function is_iterable; @@ -22,7 +21,7 @@ * @psalm-type InputArray = array * @template TOptions of Options * @template-extends AbstractFilter - * @template TInput of array + * @template TInput of array */ abstract class AbstractDateDropdown extends AbstractFilter { @@ -99,22 +98,23 @@ public function filter(mixed $value): mixed // Convert the date to a specific format if ( $this->isNullOnEmpty() - && array_reduce($value, [self::class, 'reduce'], false) + && array_reduce($value, self::reduce(...), false) ) { return null; } if ( $this->isNullOnAllEmpty() - && array_reduce($value, [self::class, 'reduce'], true) + && array_reduce($value, self::reduce(...), true) ) { return null; } + ksort($value); $this->filterable($value); - assert(is_array($value)); - ksort($value); + /** @psalm-var array $value Forcing the type here because it has already been asserted */ + return vsprintf($this->format, $value); } @@ -141,8 +141,8 @@ protected function filterable(array $value): void /** * Reduce to a single value */ - private static function reduce(string $soFar, string|null $value): bool + private static function reduce(bool $soFar, string|null $value): bool { - return $soFar || empty($value); + return $soFar || ($value === null || $value === ''); } } diff --git a/src/DateSelect.php b/src/DateSelect.php index 8d565eeb..d312f753 100644 --- a/src/DateSelect.php +++ b/src/DateSelect.php @@ -11,9 +11,9 @@ * ... * } * @psalm-type InputArray = array{ - * year: numeric, - * month: numeric, - * day: numeric, + * year: numeric-string, + * month: numeric-string, + * day: numeric-string, * } * @template TOptions of Options * @template-extends AbstractDateDropdown diff --git a/src/DateTimeSelect.php b/src/DateTimeSelect.php index ac9f0b3b..8f3657a9 100644 --- a/src/DateTimeSelect.php +++ b/src/DateTimeSelect.php @@ -15,12 +15,12 @@ * ... * } * @psalm-type InputArray = array{ - * year: numeric, - * month: numeric, - * day: numeric, - * hour: numeric, - * minute: numeric, - * second: numeric + * year: numeric-string, + * month: numeric-string, + * day: numeric-string, + * hour: numeric-string, + * minute: numeric-string, + * second: numeric-string, * } * @template TOptions of Options * @template-extends AbstractDateDropdown diff --git a/src/MonthSelect.php b/src/MonthSelect.php index d90ca512..152c6c1d 100644 --- a/src/MonthSelect.php +++ b/src/MonthSelect.php @@ -11,8 +11,8 @@ * ... * } * @psalm-type InputArray = array{ - * year: numeric, - * month: numeric, + * year: numeric-string, + * month: numeric-string, * } * @template TOptions of Options * @template-extends AbstractDateDropdown