Skip to content

Commit

Permalink
Merge pull request #432 from greg0ire/3.0.x
Browse files Browse the repository at this point in the history
Merge 2.3.x up into 3.0.x
  • Loading branch information
greg0ire authored Oct 14, 2024
2 parents 7278c35 + dd4cb41 commit ab5a3cc
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/stubs export-ignore
/tests export-ignore
/.github export-ignore
.doctrine-project.json export-ignore
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand Down
10 changes: 10 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
</ignoreFiles>
</projectFiles>

<stubs>
<file name="stubs/array_find.phpstub" />
</stubs>

<issueHandlers>
<MixedArgument errorLevel="info" />
<MixedArgumentTypeCoercion errorLevel="info" />
Expand All @@ -40,6 +44,12 @@
</errorLevel>
</PossiblyNullArgument>

<RedundantCastGivenDocblockType>
<errorLevel type="suppress">
<file name="src/ArrayCollection.php"/>
</errorLevel>
</RedundantCastGivenDocblockType>

<UnsafeGenericInstantiation>
<errorLevel type="suppress">
<file name="src/ArrayCollection.php"/>
Expand Down
36 changes: 15 additions & 21 deletions src/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,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;
Expand Down Expand Up @@ -201,13 +204,10 @@ public function contains(mixed $element): bool

public function exists(Closure $p): bool
{
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),
);
}

/**
Expand Down Expand Up @@ -326,24 +326,18 @@ public function filter(Closure $p): Collection

public function findFirst(Closure $p): mixed
{
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),
);
}

public function forAll(Closure $p): bool
{
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),
);
}

/**
Expand Down
28 changes: 10 additions & 18 deletions src/Expr/ClosureExpressionVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -176,29 +178,19 @@ public function walkCompositeExpression(CompositeExpression $expr): Closure
/** @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 */
Expand Down
12 changes: 12 additions & 0 deletions stubs/array_find.phpstub
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/**
* @param array<TKey, TValue> $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 {}

0 comments on commit ab5a3cc

Please sign in to comment.