Skip to content

[12.x] Make built-in functions be callable with collection callbacks whose parameter count doesn't fit #56525

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

Closed
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
39 changes: 35 additions & 4 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
use ArrayAccess;
use ArrayIterator;
use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString;
use Closure;
use Illuminate\Support\Traits\EnumeratesValues;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Traits\TransformsToResourceCollection;
use InvalidArgumentException;
use ReflectionFunction;
use stdClass;
use Traversable;

Expand Down Expand Up @@ -414,7 +416,7 @@ public function except($keys)
public function filter(?callable $callback = null)
{
if ($callback) {
return new static(Arr::where($this->items, $callback));
return new static(Arr::where($this->items, fn ($value, $key) => $this->callPotentialBuiltinCallback($callback, $value, $key)));
}

return new static(array_filter($this->items));
Expand All @@ -431,7 +433,11 @@ public function filter(?callable $callback = null)
*/
public function first(?callable $callback = null, $default = null)
{
return Arr::first($this->items, $callback, $default);
if ($callback) {
return Arr::first($this->items, fn ($value, $key) => $this->callPotentialBuiltinCallback($callback, $value, $key), $default);
}

return Arr::first($this->items, default: $default);
}

/**
Expand Down Expand Up @@ -794,7 +800,11 @@ public function keys()
*/
public function last(?callable $callback = null, $default = null)
{
return Arr::last($this->items, $callback, $default);
if ($callback) {
return Arr::last($this->items, fn ($value, $key) => $this->callPotentialBuiltinCallback($callback, $value, $key), $default);
}

return Arr::last($this->items, default: $default);
}

/**
Expand All @@ -819,7 +829,7 @@ public function pluck($value, $key = null)
*/
public function map(callable $callback)
{
return new static(Arr::map($this->items, $callback));
return new static(Arr::map($this->items, fn ($value, $key) => $this->callPotentialBuiltinCallback($callback, $value, $key)));
}

/**
Expand Down Expand Up @@ -1948,4 +1958,25 @@ public function offsetUnset($key): void
{
unset($this->items[$key]);
}

/**
* Built-in or internal functions, unlike userland functions, must be called with the proper
* number of arguments. Userland functions can discard additional arguments on their own
* To align built-in function behavior with their userland counterparts, callback function calls
* with callables have to be wrapped in this function.
*
* @template TReturn
*
* @param (callable(): TReturn)|string|array{object|class-string, string} $fn
* @return TReturn
*/
private function callPotentialBuiltinCallback(callable|string|array $fn, ...$arguments): mixed
{
$reflectionFunction = new ReflectionFunction(Closure::fromCallable($fn));
if ($reflectionFunction->isInternal() && ($paramCount = $reflectionFunction->getNumberOfParameters()) < count($arguments)) {
return call_user_func($fn, ...array_slice($arguments, 0, $paramCount));
}

return call_user_func($fn, ...$arguments);
}
}
6 changes: 6 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,12 @@ public function testFilter($collection)
$this->assertEquals([1, 2, 3], $c->filter()->all());
}

public function testCallPotentialBuiltinCallback()
{
$this->assertEquals(['foo'], collect(['a' => 1, 'b' => 'foo'])->filter(is_string(...))->all());
$this->assertEquals(['Foo', 'Bar'], collect(['a' => 'foo', 'b' => 'bar'])->map(ucfirst(...))->all());
}

#[DataProvider('collectionClassProvider')]
public function testHigherOrderKeyBy($collection)
{
Expand Down
Loading