Skip to content

[12.x] multiline chaining on Collections #55061

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

Merged
merged 2 commits into from
Mar 18, 2025
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
10 changes: 5 additions & 5 deletions src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ protected function extractAuthParameters($pattern, $channel, $callback)
{
$callbackParameters = $this->extractParameters($callback);

return (new Collection($this->extractChannelKeys($pattern, $channel)))->reject(function ($value, $key) {
return is_numeric($key);
})->map(function ($value, $key) use ($callbackParameters) {
return $this->resolveBinding($key, $value, $callbackParameters);
})->values()->all();
return (new Collection($this->extractChannelKeys($pattern, $channel)))
->reject(fn ($value, $key) => is_numeric($key))
->map(fn ($value, $key) => $this->resolveBinding($key, $value, $callbackParameters))
->values()
->all();
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ public function many(array $keys)
{
$this->event(new RetrievingManyKeys($this->getName(), $keys));

$values = $this->store->many((new Collection($keys))->map(function ($value, $key) {
return is_string($key) ? $key : $value;
})->values()->all());
$values = $this->store->many((new Collection($keys))
->map(fn ($value, $key) => is_string($key) ? $key : $value)
->values()
->all()
);

return (new Collection($values))
->map(fn ($value, $key) => $this->handleManyResult($keys, $key, $value))
Expand Down
14 changes: 5 additions & 9 deletions src/Illuminate/Database/Console/PruneCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,11 @@ protected function models()
['\\', ''],
Str::after($model->getRealPath(), realpath(app_path()).DIRECTORY_SEPARATOR)
);
})->when(! empty($except), function ($models) use ($except) {
return $models->reject(function ($model) use ($except) {
return in_array($model, $except);
});
})->filter(function ($model) {
return class_exists($model);
})->filter(function ($model) {
return $this->isPrunable($model);
})->values();
})
->when(! empty($except), fn ($models) => $models->reject(fn ($model) => in_array($model, $except)))
->filter(fn ($model) => class_exists($model))
->filter(fn ($model) => $this->isPrunable($model))
->values();
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2850,11 +2850,9 @@ public function reorder($column = null, $direction = 'asc')
protected function removeExistingOrdersFor($column)
{
return (new Collection($this->orders))
->reject(function ($order) use ($column) {
return isset($order['column'])
? $order['column'] === $column
: false;
})->values()->all();
->reject(fn ($order) => isset($order['column']) && $order['column'] === $column)
->values()
->all();
}

/**
Expand Down
13 changes: 7 additions & 6 deletions src/Illuminate/Foundation/Console/RouteListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ public function handle()
*/
protected function getRoutes()
{
$routes = (new Collection($this->router->getRoutes()))->map(function ($route) {
return $this->getRouteInformation($route);
})->filter()->all();
$routes = (new Collection($this->router->getRoutes()))
->map(fn ($route) => $this->getRouteInformation($route))
->filter()
->all();

if (($sort = $this->option('sort')) !== null) {
$routes = $this->sortRoutes($sort, $routes);
Expand Down Expand Up @@ -208,9 +209,9 @@ protected function displayRoutes(array $routes)
*/
protected function getMiddleware($route)
{
return (new Collection($this->router->gatherRouteMiddleware($route)))->map(function ($middleware) {
return $middleware instanceof Closure ? 'Closure' : $middleware;
})->implode("\n");
return (new Collection($this->router->gatherRouteMiddleware($route)))
->map(fn ($middleware) => $middleware instanceof Closure ? 'Closure' : $middleware)
->implode("\n");
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,14 @@ public function stopIgnoring(array|string $exceptions)
$exceptions = Arr::wrap($exceptions);

$this->dontReport = (new Collection($this->dontReport))
->reject(fn ($ignored) => in_array($ignored, $exceptions))->values()->all();
->reject(fn ($ignored) => in_array($ignored, $exceptions))
->values()
->all();

$this->internalDontReport = (new Collection($this->internalDontReport))
->reject(fn ($ignored) => in_array($ignored, $exceptions))->values()->all();
->reject(fn ($ignored) => in_array($ignored, $exceptions))
->values()
->all();

return $this;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Illuminate/Foundation/PackageManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ public function aliases()
*/
public function config($key)
{
return (new Collection($this->getManifest()))->flatMap(function ($configuration) use ($key) {
return (array) ($configuration[$key] ?? []);
})->filter()->all();
return (new Collection($this->getManifest()))
->flatMap(fn ($configuration) => (array) ($configuration[$key] ?? []))
->filter()
->all();
}

/**
Expand Down
14 changes: 8 additions & 6 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -991,13 +991,15 @@ protected function parseHttpOptions(array $options)
$options[$this->bodyFormat] = $this->pendingBody;
}

return (new Collection($options))->map(function ($value, $key) {
if ($key === 'json' && $value instanceof JsonSerializable) {
return $value;
}
return (new Collection($options))
->map(function ($value, $key) {
if ($key === 'json' && $value instanceof JsonSerializable) {
return $value;
}

return $value instanceof Arrayable ? $value->toArray() : $value;
})->all();
return $value instanceof Arrayable ? $value->toArray() : $value;
})
->all();
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/Illuminate/Notifications/Channels/MailChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,13 @@ protected function getRecipients($notifiable, $notification, $message)
$recipients = [$recipients];
}

return (new Collection($recipients))->mapWithKeys(function ($recipient, $email) {
return is_numeric($email)
? [$email => (is_string($recipient) ? $recipient : $recipient->email)]
: [$email => $recipient];
})->all();
return (new Collection($recipients))
->mapWithKeys(function ($recipient, $email) {
return is_numeric($email)
? [$email => (is_string($recipient) ? $recipient : $recipient->email)]
: [$email => $recipient];
})
->all();
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Illuminate/Notifications/Messages/MailMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,10 @@ public function data()
*/
protected function parseAddresses($value)
{
return (new Collection($value))->map(function ($address, $name) {
return [$address, is_numeric($name) ? null : $name];
})->values()->all();
return (new Collection($value))
->map(fn ($address, $name) => [$address, is_numeric($name) ? null : $name])
->values()
->all();
}

/**
Expand Down
14 changes: 8 additions & 6 deletions src/Illuminate/Process/FakeProcessDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ public function errorOutput(array|string $output)
*/
public function replaceOutput(string $output)
{
$this->output = (new Collection($this->output))->reject(function ($output) {
return $output['type'] === 'out';
})->values()->all();
$this->output = (new Collection($this->output))
->reject(fn ($output) => $output['type'] === 'out')
->values()
->all();

if (strlen($output) > 0) {
$this->output[] = [
Expand All @@ -116,9 +117,10 @@ public function replaceOutput(string $output)
*/
public function replaceErrorOutput(string $output)
{
$this->output = (new Collection($this->output))->reject(function ($output) {
return $output['type'] === 'err';
})->values()->all();
$this->output = (new Collection($this->output))
->reject(fn ($output) => $output['type'] === 'err')
->values()
->all();

if (strlen($output) > 0) {
$this->output[] = [
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Process/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public function start(?callable $output = null)
if (! $pendingProcess instanceof PendingProcess) {
throw new InvalidArgumentException('Process pool must only contain pending processes.');
}
})->mapWithKeys(function ($pendingProcess, $key) use ($output) {
})
->mapWithKeys(function ($pendingProcess, $key) use ($output) {
return [$key => $pendingProcess->start(output: $output ? function ($type, $buffer) use ($key, $output) {
$output($type, $buffer, $key);
} : null)];
Expand Down
8 changes: 5 additions & 3 deletions src/Illuminate/Queue/Failed/FileFailedJobProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,11 @@ public function prune(DateTimeInterface $before)
return $this->lock(function () use ($before) {
$jobs = $this->read();

$this->write($prunedJobs = (new Collection($jobs))->reject(function ($job) use ($before) {
return $job->failed_at_timestamp <= $before->getTimestamp();
})->values()->all());
$this->write($prunedJobs = (new Collection($jobs))
->reject(fn ($job) => $job->failed_at_timestamp <= $before->getTimestamp())
->values()
->all()
);

return count($jobs) - count($prunedJobs);
});
Expand Down
25 changes: 16 additions & 9 deletions src/Illuminate/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -1139,15 +1139,22 @@ public function controllerMiddleware()
*/
protected function staticallyProvidedControllerMiddleware(string $class, string $method)
{
return (new Collection($class::middleware()))->map(function ($middleware) {
return $middleware instanceof Middleware
? $middleware
: new Middleware($middleware);
})->reject(function ($middleware) use ($method) {
return static::methodExcludedByOptions(
$method, ['only' => $middleware->only, 'except' => $middleware->except]
);
})->map->middleware->flatten()->values()->all();
return (new Collection($class::middleware()))
->map(function ($middleware) {
return $middleware instanceof Middleware
? $middleware
: new Middleware($middleware);
})
->reject(function ($middleware) use ($method) {
return static::methodExcludedByOptions(
$method, ['only' => $middleware->only, 'except' => $middleware->except],
);
})
->map
->middleware
->flatten()
->values()
->all();
}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -830,9 +830,13 @@ public function gatherRouteMiddleware(Route $route)
*/
public function resolveMiddleware(array $middleware, array $excluded = [])
{
$excluded = $excluded === [] ? $excluded : (new Collection($excluded))->map(function ($name) {
return (array) MiddlewareNameResolver::resolve($name, $this->middleware, $this->middlewareGroups);
})->flatten()->values()->all();
$excluded = $excluded === []
? $excluded
: (new Collection($excluded))
->map(fn ($name) => (array) MiddlewareNameResolver::resolve($name, $this->middleware, $this->middlewareGroups))
->flatten()
->values()
->all();

$middleware = (new Collection($middleware))->map(function ($name) {
return (array) MiddlewareNameResolver::resolve($name, $this->middleware, $this->middlewareGroups);
Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -1051,8 +1051,10 @@ public static function password($length = 32, $letters = true, $numbers = true,
']', '|', ':', ';',
] : null,
'spaces' => $spaces === true ? [' '] : null,
]))->filter()->each(fn ($c) => $password->push($c[random_int(0, count($c) - 1)])
)->flatten();
]))
->filter()
->each(fn ($c) => $password->push($c[random_int(0, count($c) - 1)]))
->flatten();

$length = $length - $password->count();

Expand Down
7 changes: 4 additions & 3 deletions src/Illuminate/Support/Testing/Fakes/QueueFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,10 @@ public function connection($value = null)
*/
public function size($queue = null)
{
return (new Collection($this->jobs))->flatten(1)->filter(
fn ($job) => $job['queue'] === $queue
)->count();
return (new Collection($this->jobs))
->flatten(1)
->filter(fn ($job) => $job['queue'] === $queue)
->count();
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Illuminate/Support/Traits/InteractsWithData.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,10 @@ public function enums($key, $enumClass)
return [];
}

return $this->collect($key)->map(function ($value) use ($enumClass) {
return $enumClass::tryFrom($value);
})->filter()->all();
return $this->collect($key)
->map(fn ($value) => $enumClass::tryFrom($value))
->filter()
->all();
}

/**
Expand Down
30 changes: 18 additions & 12 deletions src/Illuminate/Support/Traits/ReflectsClosures.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ protected function firstClosureParameterTypes(Closure $closure)
{
$reflection = new ReflectionFunction($closure);

$types = (new Collection($reflection->getParameters()))->mapWithKeys(function ($parameter) {
if ($parameter->isVariadic()) {
return [$parameter->getName() => null];
}
$types = (new Collection($reflection->getParameters()))
->mapWithKeys(function ($parameter) {
if ($parameter->isVariadic()) {
return [$parameter->getName() => null];
}

return [$parameter->getName() => Reflector::getParameterClassNames($parameter)];
})->filter()->values()->all();
return [$parameter->getName() => Reflector::getParameterClassNames($parameter)];
})
->filter()
->values()
->all();

if (empty($types)) {
throw new RuntimeException('The given Closure has no parameters.');
Expand All @@ -78,12 +82,14 @@ protected function closureParameterTypes(Closure $closure)
{
$reflection = new ReflectionFunction($closure);

return (new Collection($reflection->getParameters()))->mapWithKeys(function ($parameter) {
if ($parameter->isVariadic()) {
return [$parameter->getName() => null];
}
return (new Collection($reflection->getParameters()))
->mapWithKeys(function ($parameter) {
if ($parameter->isVariadic()) {
return [$parameter->getName() => null];
}

return [$parameter->getName() => Reflector::getParameterClassName($parameter)];
})->all();
return [$parameter->getName() => Reflector::getParameterClassName($parameter)];
})
->all();
}
}
16 changes: 10 additions & 6 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1042,9 +1042,11 @@ public function invalid()
*/
protected function attributesThatHaveMessages()
{
return (new Collection($this->messages()->toArray()))->map(function ($message, $key) {
return explode('.', $key)[0];
})->unique()->flip()->all();
return (new Collection($this->messages()->toArray()))
->map(fn ($message, $key) => explode('.', $key)[0])
->unique()
->flip()
->all();
}

/**
Expand Down Expand Up @@ -1217,9 +1219,11 @@ public function getRulesWithoutPlaceholders()
*/
public function setRules(array $rules)
{
$rules = (new Collection($rules))->mapWithKeys(function ($value, $key) {
return [str_replace('\.', '__dot__'.static::$placeholderHash, $key) => $value];
})->toArray();
$rules = (new Collection($rules))
->mapWithKeys(function ($value, $key) {
return [str_replace('\.', '__dot__'.static::$placeholderHash, $key) => $value];
})
->toArray();

$this->initialRules = $rules;

Expand Down
Loading