Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leverage new array functions #424

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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;
Expand Down Expand Up @@ -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),
);
}

/**
Expand Down Expand Up @@ -386,27 +386,21 @@ 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),
);
}

/**
* {@inheritDoc}
*/
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),
);
}

/**
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 @@ -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 */
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 {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is a stub needed for this function but not the others?

Copy link
Member Author

@derrabus derrabus Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Psalm does not know about the new functions yet. We need this stub, so Psalm can infer the correct return type for array_find(). The other two new functions I've used always return a boolean, so the native return types might be just good enough.