From 1a882ec96b02eecff790042e2fe6790eb62b57c0 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Wed, 28 Aug 2024 21:44:12 +0200 Subject: [PATCH] Leverage new array functions (#424) --- .gitattributes | 1 + composer.json | 3 ++- psalm.xml.dist | 10 ++++++++ src/ArrayCollection.php | 36 +++++++++++---------------- src/Expr/ClosureExpressionVisitor.php | 28 ++++++++------------- stubs/array_find.phpstub | 12 +++++++++ 6 files changed, 50 insertions(+), 40 deletions(-) create mode 100644 stubs/array_find.phpstub diff --git a/.gitattributes b/.gitattributes index e5239788..1674a083 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,4 @@ +/stubs export-ignore /tests export-ignore /.github export-ignore .doctrine-project.json export-ignore diff --git a/composer.json b/composer.json index 4c36229a..960064c6 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,8 @@ "homepage": "https://www.doctrine-project.org/projects/collections.html", "require": { "php": "^8.1", - "doctrine/deprecations": "^1" + "doctrine/deprecations": "^1", + "symfony/polyfill-php84": "^1.30" }, "require-dev": { "ext-json": "*", diff --git a/psalm.xml.dist b/psalm.xml.dist index aaa96c85..da4259fb 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -16,6 +16,10 @@ + + + + @@ -40,6 +44,12 @@ + + + + + + diff --git a/src/ArrayCollection.php b/src/ArrayCollection.php index 2e7d0e3b..8c3efb2f 100644 --- a/src/ArrayCollection.php +++ b/src/ArrayCollection.php @@ -11,7 +11,10 @@ use Stringable; use Traversable; +use function array_all; +use function array_any; use function array_filter; +use function array_find; use function array_key_exists; use function array_keys; use function array_map; @@ -245,13 +248,10 @@ public function contains(mixed $element) */ public function exists(Closure $p) { - foreach ($this->elements as $key => $element) { - if ($p($key, $element)) { - return true; - } - } - - return false; + return array_any( + $this->elements, + static fn (mixed $element, mixed $key): bool => (bool) $p($key, $element), + ); } /** @@ -386,13 +386,10 @@ public function filter(Closure $p) */ public function findFirst(Closure $p) { - foreach ($this->elements as $key => $element) { - if ($p($key, $element)) { - return $element; - } - } - - return null; + return array_find( + $this->elements, + static fn (mixed $element, mixed $key): bool => (bool) $p($key, $element), + ); } /** @@ -400,13 +397,10 @@ public function findFirst(Closure $p) */ public function forAll(Closure $p) { - foreach ($this->elements as $key => $element) { - if (! $p($key, $element)) { - return false; - } - } - - return true; + return array_all( + $this->elements, + static fn (mixed $element, mixed $key): bool => (bool) $p($key, $element), + ); } /** diff --git a/src/Expr/ClosureExpressionVisitor.php b/src/Expr/ClosureExpressionVisitor.php index 4319b9b5..5240e306 100644 --- a/src/Expr/ClosureExpressionVisitor.php +++ b/src/Expr/ClosureExpressionVisitor.php @@ -8,6 +8,8 @@ use Closure; use RuntimeException; +use function array_all; +use function array_any; use function explode; use function in_array; use function is_array; @@ -189,29 +191,19 @@ public function walkCompositeExpression(CompositeExpression $expr) /** @param callable[] $expressions */ private function andExpressions(array $expressions): Closure { - return static function ($object) use ($expressions): bool { - foreach ($expressions as $expression) { - if (! $expression($object)) { - return false; - } - } - - return true; - }; + return static fn ($object): bool => array_all( + $expressions, + static fn (callable $expression): bool => (bool) $expression($object), + ); } /** @param callable[] $expressions */ private function orExpressions(array $expressions): Closure { - return static function ($object) use ($expressions): bool { - foreach ($expressions as $expression) { - if ($expression($object)) { - return true; - } - } - - return false; - }; + return static fn ($object): bool => array_any( + $expressions, + static fn (callable $expression): bool => (bool) $expression($object), + ); } /** @param callable[] $expressions */ diff --git a/stubs/array_find.phpstub b/stubs/array_find.phpstub new file mode 100644 index 00000000..bbe9341c --- /dev/null +++ b/stubs/array_find.phpstub @@ -0,0 +1,12 @@ + $array + * @param callable(TValue $value, TKey $key): bool $callback + * + * @return TValue|null + * + * @template TKey of array-key + * @template TValue + */ +function array_find(array $array, callable $callback): mixed {}