Skip to content

Commit

Permalink
[11.x] convert collect() helper to new Collection() (#53726)
Browse files Browse the repository at this point in the history
* wip

* more

* more

* remove unused imports

i screwed up when I did the merge

* wip

* remove extra parentheses

* wip

* minor formatting

* wip

* wip

* wip

* wip

* wip

* minor formatting
  • Loading branch information
browner12 authored Dec 3, 2024
1 parent 507f325 commit 7db7ea9
Show file tree
Hide file tree
Showing 149 changed files with 524 additions and 399 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Contracts\Routing\BindingRegistrar;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Reflector;
use ReflectionClass;
use ReflectionFunction;
Expand Down Expand Up @@ -140,7 +141,7 @@ protected function extractAuthParameters($pattern, $channel, $callback)
{
$callbackParameters = $this->extractParameters($callback);

return collect($this->extractChannelKeys($pattern, $channel))->reject(function ($value, $key) {
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);
Expand Down Expand Up @@ -381,6 +382,6 @@ protected function channelNameMatchesPattern($channel, $pattern)
*/
public function getChannels()
{
return collect($this->channels);
return new Collection($this->channels);
}
}
14 changes: 7 additions & 7 deletions src/Illuminate/Bus/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function add($jobs)
*/
protected function prepareBatchedChain(array $chain)
{
return collect($chain)->map(function ($job) {
return (new Collection($chain))->map(function ($job) {
$job = $job instanceof Closure ? CallQueuedClosure::create($job) : $job;

return $job->withBatchId($this->id);
Expand Down Expand Up @@ -245,7 +245,7 @@ public function recordSuccessfulJob(string $jobId)
if ($this->hasProgressCallbacks()) {
$batch = $this->fresh();

collect($this->options['progress'])->each(function ($handler) use ($batch) {
(new Collection($this->options['progress']))->each(function ($handler) use ($batch) {
$this->invokeHandlerCallback($handler, $batch);
});
}
Expand All @@ -257,15 +257,15 @@ public function recordSuccessfulJob(string $jobId)
if ($counts->pendingJobs === 0 && $this->hasThenCallbacks()) {
$batch = $this->fresh();

collect($this->options['then'])->each(function ($handler) use ($batch) {
(new Collection($this->options['then']))->each(function ($handler) use ($batch) {
$this->invokeHandlerCallback($handler, $batch);
});
}

if ($counts->allJobsHaveRanExactlyOnce() && $this->hasFinallyCallbacks()) {
$batch = $this->fresh();

collect($this->options['finally'])->each(function ($handler) use ($batch) {
(new Collection($this->options['finally']))->each(function ($handler) use ($batch) {
$this->invokeHandlerCallback($handler, $batch);
});
}
Expand Down Expand Up @@ -350,23 +350,23 @@ public function recordFailedJob(string $jobId, $e)
if ($this->hasProgressCallbacks() && $this->allowsFailures()) {
$batch = $this->fresh();

collect($this->options['progress'])->each(function ($handler) use ($batch, $e) {
(new Collection($this->options['progress']))->each(function ($handler) use ($batch, $e) {
$this->invokeHandlerCallback($handler, $batch, $e);
});
}

if ($counts->failedJobs === 1 && $this->hasCatchCallbacks()) {
$batch = $this->fresh();

collect($this->options['catch'])->each(function ($handler) use ($batch, $e) {
(new Collection($this->options['catch']))->each(function ($handler) use ($batch, $e) {
$this->invokeHandlerCallback($handler, $batch, $e);
});
}

if ($counts->allJobsHaveRanExactlyOnce() && $this->hasFinallyCallbacks()) {
$batch = $this->fresh();

collect($this->options['finally'])->each(function ($handler) use ($batch, $e) {
(new Collection($this->options['finally']))->each(function ($handler) use ($batch, $e) {
$this->invokeHandlerCallback($handler, $batch, $e);
});
}
Expand Down
17 changes: 9 additions & 8 deletions src/Illuminate/Bus/Queueable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Queue\CallQueuedClosure;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Assert as PHPUnit;
use RuntimeException;

Expand Down Expand Up @@ -203,7 +204,7 @@ public function through($middleware)
*/
public function chain($chain)
{
$jobs = ChainedBatch::prepareNestedBatches(collect($chain));
$jobs = ChainedBatch::prepareNestedBatches(new Collection($chain));

$this->chained = $jobs->map(function ($job) {
return $this->serializeJob($job);
Expand All @@ -220,7 +221,7 @@ public function chain($chain)
*/
public function prependToChain($job)
{
$jobs = ChainedBatch::prepareNestedBatches(collect([$job]));
$jobs = ChainedBatch::prepareNestedBatches(new Collection([$job]));

$this->chained = Arr::prepend($this->chained, $this->serializeJob($jobs->first()));

Expand All @@ -235,7 +236,7 @@ public function prependToChain($job)
*/
public function appendToChain($job)
{
$jobs = ChainedBatch::prepareNestedBatches(collect([$job]));
$jobs = ChainedBatch::prepareNestedBatches(new Collection([$job]));

$this->chained = array_merge($this->chained, [$this->serializeJob($jobs->first())]);

Expand Down Expand Up @@ -294,7 +295,7 @@ public function dispatchNextJobInChain()
*/
public function invokeChainCatchCallbacks($e)
{
collect($this->chainCatchCallbacks)->each(function ($callback) use ($e) {
(new Collection($this->chainCatchCallbacks))->each(function ($callback) use ($e) {
$callback($e);
});
}
Expand All @@ -308,14 +309,14 @@ public function invokeChainCatchCallbacks($e)
public function assertHasChain($expectedChain)
{
PHPUnit::assertTrue(
collect($expectedChain)->isNotEmpty(),
(new Collection($expectedChain))->isNotEmpty(),
'The expected chain can not be empty.'
);

if (collect($expectedChain)->contains(fn ($job) => is_object($job))) {
$expectedChain = collect($expectedChain)->map(fn ($job) => serialize($job))->all();
if ((new Collection($expectedChain))->contains(fn ($job) => is_object($job))) {
$expectedChain = (new Collection($expectedChain))->map(fn ($job) => serialize($job))->all();
} else {
$chain = collect($this->chained)->map(fn ($job) => get_class(unserialize($job)))->all();
$chain = (new Collection($this->chained))->map(fn ($job) => get_class(unserialize($job)))->all();
}

PHPUnit::assertTrue(
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Illuminate\Contracts\Cache\Store;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\InteractsWithTime;
use Illuminate\Support\Traits\Macroable;

Expand Down Expand Up @@ -141,11 +142,11 @@ public function many(array $keys)
{
$this->event(new RetrievingManyKeys($this->getName(), $keys));

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

return collect($values)->map(function ($value, $key) use ($keys) {
return (new Collection($values))->map(function ($value, $key) use ($keys) {
return $this->handleManyResult($keys, $key, $value);
})->all();
}
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Cache/RetrievesMultipleKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Cache;

use Illuminate\Support\Collection;

trait RetrievesMultipleKeys
{
/**
Expand All @@ -16,7 +18,7 @@ public function many(array $keys)
{
$return = [];

$keys = collect($keys)->mapWithKeys(function ($value, $key) {
$keys = (new Collection($keys))->mapWithKeys(function ($value, $key) {
return [is_string($key) ? $key : $value => is_string($key) ? $value : null];
})->all();

Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Collections/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ function data_get($target, $key, $default = null)
$segment = match ($segment) {
'\*' => '*',
'\{first}' => '{first}',
'{first}' => array_key_first(is_array($target) ? $target : collect($target)->all()),
'{first}' => array_key_first(is_array($target) ? $target : (new Collection($target))->all()),
'\{last}' => '{last}',
'{last}' => array_key_last(is_array($target) ? $target : collect($target)->all()),
'{last}' => array_key_last(is_array($target) ? $target : (new Collection($target))->all()),
default => $segment,
};

Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Concurrency/SyncDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Contracts\Concurrency\Driver;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Defer\DeferredCallback;

use function Illuminate\Support\defer;
Expand All @@ -16,7 +17,7 @@ class SyncDriver implements Driver
*/
public function run(Closure|array $tasks): array
{
return collect(Arr::wrap($tasks))->map(
return (new Collection(Arr::wrap($tasks)))->map(
fn ($task) => $task()
)->all();
}
Expand All @@ -26,6 +27,6 @@ public function run(Closure|array $tasks): array
*/
public function defer(Closure|array $tasks): DeferredCallback
{
return defer(fn () => collect(Arr::wrap($tasks))->each(fn ($task) => $task()));
return defer(fn () => (new Collection(Arr::wrap($tasks)))->each(fn ($task) => $task()));
}
}
3 changes: 2 additions & 1 deletion src/Illuminate/Console/Concerns/CallsCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Console\Concerns;

use Illuminate\Support\Collection;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -95,7 +96,7 @@ protected function createInputFromArguments(array $arguments)
*/
protected function context()
{
return collect($this->option())->only([
return (new Collection($this->option()))->only([
'ansi',
'no-ansi',
'no-interaction',
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Console/Concerns/InteractsWithSignals.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Signals;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

trait InteractsWithSignals
{
Expand All @@ -30,7 +31,7 @@ public function trap($signals, $callback)
$this->getApplication()->getSignalRegistry(),
);

collect(Arr::wrap(value($signals)))
(new Collection(Arr::wrap(value($signals))))
->each(fn ($signal) => $this->signals->register($signal, $callback));
});
}
Expand Down
7 changes: 4 additions & 3 deletions src/Illuminate/Console/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Concerns\CreatesMatchingTest;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Finder\Finder;
Expand Down Expand Up @@ -248,7 +249,7 @@ protected function possibleModels()
{
$modelPath = is_dir(app_path('Models')) ? app_path('Models') : app_path();

return collect(Finder::create()->files()->depth(0)->in($modelPath))
return (new Collection(Finder::create()->files()->depth(0)->in($modelPath)))
->map(fn ($file) => $file->getBasename('.php'))
->sort()
->values()
Expand All @@ -268,7 +269,7 @@ protected function possibleEvents()
return [];
}

return collect(Finder::create()->files()->depth(0)->in($eventPath))
return (new Collection(Finder::create()->files()->depth(0)->in($eventPath)))
->map(fn ($file) => $file->getBasename('.php'))
->sort()
->values()
Expand Down Expand Up @@ -460,7 +461,7 @@ protected function isReservedName($name)
{
return in_array(
strtolower($name),
collect($this->reservedNames)
(new Collection($this->reservedNames))
->transform(fn ($name) => strtolower($name))
->all()
);
Expand Down
7 changes: 4 additions & 3 deletions src/Illuminate/Console/Scheduling/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\CallQueuedClosure;
use Illuminate\Support\Collection;
use Illuminate\Support\ProcessUtils;
use Illuminate\Support\Traits\Macroable;
use RuntimeException;
Expand Down Expand Up @@ -332,7 +333,7 @@ protected function mergePendingAttributes(Event $event)
*/
protected function compileParameters(array $parameters)
{
return collect($parameters)->map(function ($value, $key) {
return (new Collection($parameters))->map(function ($value, $key) {
if (is_array($value)) {
return $this->compileArrayInput($key, $value);
}
Expand All @@ -354,7 +355,7 @@ protected function compileParameters(array $parameters)
*/
public function compileArrayInput($key, $value)
{
$value = collect($value)->map(function ($value) {
$value = (new Collection($value))->map(function ($value) {
return ProcessUtils::escapeArgument($value);
});

Expand Down Expand Up @@ -391,7 +392,7 @@ public function serverShouldRun(Event $event, DateTimeInterface $time)
*/
public function dueEvents($app)
{
return collect($this->events)->filter->isDue($app);
return (new Collection($this->events))->filter->isDue($app);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Console/Scheduling/ScheduleFinishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Console\Events\ScheduledBackgroundTaskFinished;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Collection;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'schedule:finish')]
Expand Down Expand Up @@ -39,7 +40,7 @@ class ScheduleFinishCommand extends Command
*/
public function handle(Schedule $schedule)
{
collect($schedule->events())->filter(function ($value) {
(new Collection($schedule->events()))->filter(function ($value) {
return $value->mutexName() == $this->argument('id');
})->each(function ($event) {
$event->finish($this->laravel, $this->argument('code'));
Expand Down
7 changes: 4 additions & 3 deletions src/Illuminate/Console/Scheduling/ScheduleListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Console\Application;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use ReflectionClass;
use ReflectionFunction;
use Symfony\Component\Console\Attribute\AsCommand;
Expand Down Expand Up @@ -50,7 +51,7 @@ class ScheduleListCommand extends Command
*/
public function handle(Schedule $schedule)
{
$events = collect($schedule->events());
$events = new Collection($schedule->events());

if ($events->isEmpty()) {
$this->components->info('No scheduled tasks have been defined.');
Expand Down Expand Up @@ -87,7 +88,7 @@ private function getCronExpressionSpacing($events)
{
$rows = $events->map(fn ($event) => array_map('mb_strlen', preg_split("/\s+/", $event->expression)));

return collect($rows[0] ?? [])->keys()->map(fn ($key) => $rows->max($key))->all();
return (new Collection($rows[0] ?? []))->keys()->map(fn ($key) => $rows->max($key))->all();
}

/**
Expand Down Expand Up @@ -244,7 +245,7 @@ private function formatCronExpression($expression, $spacing)
{
$expressions = preg_split("/\s+/", $expression);

return collect($spacing)
return (new Collection($spacing))
->map(fn ($length, $index) => str_pad($expressions[$index], $length))
->implode(' ');
}
Expand Down
Loading

0 comments on commit 7db7ea9

Please # to comment.