Skip to content

Commit 7db7ea9

Browse files
authored
[11.x] convert collect() helper to new Collection() (#53726)
* 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
1 parent 507f325 commit 7db7ea9

File tree

149 files changed

+524
-399
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

149 files changed

+524
-399
lines changed

src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Contracts\Routing\BindingRegistrar;
1111
use Illuminate\Contracts\Routing\UrlRoutable;
1212
use Illuminate\Support\Arr;
13+
use Illuminate\Support\Collection;
1314
use Illuminate\Support\Reflector;
1415
use ReflectionClass;
1516
use ReflectionFunction;
@@ -140,7 +141,7 @@ protected function extractAuthParameters($pattern, $channel, $callback)
140141
{
141142
$callbackParameters = $this->extractParameters($callback);
142143

143-
return collect($this->extractChannelKeys($pattern, $channel))->reject(function ($value, $key) {
144+
return (new Collection($this->extractChannelKeys($pattern, $channel)))->reject(function ($value, $key) {
144145
return is_numeric($key);
145146
})->map(function ($value, $key) use ($callbackParameters) {
146147
return $this->resolveBinding($key, $value, $callbackParameters);
@@ -381,6 +382,6 @@ protected function channelNameMatchesPattern($channel, $pattern)
381382
*/
382383
public function getChannels()
383384
{
384-
return collect($this->channels);
385+
return new Collection($this->channels);
385386
}
386387
}

src/Illuminate/Bus/Batch.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public function add($jobs)
205205
*/
206206
protected function prepareBatchedChain(array $chain)
207207
{
208-
return collect($chain)->map(function ($job) {
208+
return (new Collection($chain))->map(function ($job) {
209209
$job = $job instanceof Closure ? CallQueuedClosure::create($job) : $job;
210210

211211
return $job->withBatchId($this->id);
@@ -245,7 +245,7 @@ public function recordSuccessfulJob(string $jobId)
245245
if ($this->hasProgressCallbacks()) {
246246
$batch = $this->fresh();
247247

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

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

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

268-
collect($this->options['finally'])->each(function ($handler) use ($batch) {
268+
(new Collection($this->options['finally']))->each(function ($handler) use ($batch) {
269269
$this->invokeHandlerCallback($handler, $batch);
270270
});
271271
}
@@ -350,23 +350,23 @@ public function recordFailedJob(string $jobId, $e)
350350
if ($this->hasProgressCallbacks() && $this->allowsFailures()) {
351351
$batch = $this->fresh();
352352

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

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

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

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

369-
collect($this->options['finally'])->each(function ($handler) use ($batch, $e) {
369+
(new Collection($this->options['finally']))->each(function ($handler) use ($batch, $e) {
370370
$this->invokeHandlerCallback($handler, $batch, $e);
371371
});
372372
}

src/Illuminate/Bus/Queueable.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use Illuminate\Queue\CallQueuedClosure;
77
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Collection;
89
use PHPUnit\Framework\Assert as PHPUnit;
910
use RuntimeException;
1011

@@ -203,7 +204,7 @@ public function through($middleware)
203204
*/
204205
public function chain($chain)
205206
{
206-
$jobs = ChainedBatch::prepareNestedBatches(collect($chain));
207+
$jobs = ChainedBatch::prepareNestedBatches(new Collection($chain));
207208

208209
$this->chained = $jobs->map(function ($job) {
209210
return $this->serializeJob($job);
@@ -220,7 +221,7 @@ public function chain($chain)
220221
*/
221222
public function prependToChain($job)
222223
{
223-
$jobs = ChainedBatch::prepareNestedBatches(collect([$job]));
224+
$jobs = ChainedBatch::prepareNestedBatches(new Collection([$job]));
224225

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

@@ -235,7 +236,7 @@ public function prependToChain($job)
235236
*/
236237
public function appendToChain($job)
237238
{
238-
$jobs = ChainedBatch::prepareNestedBatches(collect([$job]));
239+
$jobs = ChainedBatch::prepareNestedBatches(new Collection([$job]));
239240

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

@@ -294,7 +295,7 @@ public function dispatchNextJobInChain()
294295
*/
295296
public function invokeChainCatchCallbacks($e)
296297
{
297-
collect($this->chainCatchCallbacks)->each(function ($callback) use ($e) {
298+
(new Collection($this->chainCatchCallbacks))->each(function ($callback) use ($e) {
298299
$callback($e);
299300
});
300301
}
@@ -308,14 +309,14 @@ public function invokeChainCatchCallbacks($e)
308309
public function assertHasChain($expectedChain)
309310
{
310311
PHPUnit::assertTrue(
311-
collect($expectedChain)->isNotEmpty(),
312+
(new Collection($expectedChain))->isNotEmpty(),
312313
'The expected chain can not be empty.'
313314
);
314315

315-
if (collect($expectedChain)->contains(fn ($job) => is_object($job))) {
316-
$expectedChain = collect($expectedChain)->map(fn ($job) => serialize($job))->all();
316+
if ((new Collection($expectedChain))->contains(fn ($job) => is_object($job))) {
317+
$expectedChain = (new Collection($expectedChain))->map(fn ($job) => serialize($job))->all();
317318
} else {
318-
$chain = collect($this->chained)->map(fn ($job) => get_class(unserialize($job)))->all();
319+
$chain = (new Collection($this->chained))->map(fn ($job) => get_class(unserialize($job)))->all();
319320
}
320321

321322
PHPUnit::assertTrue(

src/Illuminate/Cache/Repository.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Illuminate\Contracts\Cache\Store;
2222
use Illuminate\Contracts\Events\Dispatcher;
2323
use Illuminate\Support\Carbon;
24+
use Illuminate\Support\Collection;
2425
use Illuminate\Support\InteractsWithTime;
2526
use Illuminate\Support\Traits\Macroable;
2627

@@ -141,11 +142,11 @@ public function many(array $keys)
141142
{
142143
$this->event(new RetrievingManyKeys($this->getName(), $keys));
143144

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

148-
return collect($values)->map(function ($value, $key) use ($keys) {
149+
return (new Collection($values))->map(function ($value, $key) use ($keys) {
149150
return $this->handleManyResult($keys, $key, $value);
150151
})->all();
151152
}

src/Illuminate/Cache/RetrievesMultipleKeys.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Illuminate\Cache;
44

5+
use Illuminate\Support\Collection;
6+
57
trait RetrievesMultipleKeys
68
{
79
/**
@@ -16,7 +18,7 @@ public function many(array $keys)
1618
{
1719
$return = [];
1820

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

src/Illuminate/Collections/helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ function data_get($target, $key, $default = null)
7777
$segment = match ($segment) {
7878
'\*' => '*',
7979
'\{first}' => '{first}',
80-
'{first}' => array_key_first(is_array($target) ? $target : collect($target)->all()),
80+
'{first}' => array_key_first(is_array($target) ? $target : (new Collection($target))->all()),
8181
'\{last}' => '{last}',
82-
'{last}' => array_key_last(is_array($target) ? $target : collect($target)->all()),
82+
'{last}' => array_key_last(is_array($target) ? $target : (new Collection($target))->all()),
8383
default => $segment,
8484
};
8585

src/Illuminate/Concurrency/SyncDriver.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use Illuminate\Contracts\Concurrency\Driver;
77
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Collection;
89
use Illuminate\Support\Defer\DeferredCallback;
910

1011
use function Illuminate\Support\defer;
@@ -16,7 +17,7 @@ class SyncDriver implements Driver
1617
*/
1718
public function run(Closure|array $tasks): array
1819
{
19-
return collect(Arr::wrap($tasks))->map(
20+
return (new Collection(Arr::wrap($tasks)))->map(
2021
fn ($task) => $task()
2122
)->all();
2223
}
@@ -26,6 +27,6 @@ public function run(Closure|array $tasks): array
2627
*/
2728
public function defer(Closure|array $tasks): DeferredCallback
2829
{
29-
return defer(fn () => collect(Arr::wrap($tasks))->each(fn ($task) => $task()));
30+
return defer(fn () => (new Collection(Arr::wrap($tasks)))->each(fn ($task) => $task()));
3031
}
3132
}

src/Illuminate/Console/Concerns/CallsCommands.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Console\Concerns;
44

5+
use Illuminate\Support\Collection;
56
use Symfony\Component\Console\Input\ArrayInput;
67
use Symfony\Component\Console\Output\NullOutput;
78
use Symfony\Component\Console\Output\OutputInterface;
@@ -95,7 +96,7 @@ protected function createInputFromArguments(array $arguments)
9596
*/
9697
protected function context()
9798
{
98-
return collect($this->option())->only([
99+
return (new Collection($this->option()))->only([
99100
'ansi',
100101
'no-ansi',
101102
'no-interaction',

src/Illuminate/Console/Concerns/InteractsWithSignals.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Signals;
66
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Collection;
78

89
trait InteractsWithSignals
910
{
@@ -30,7 +31,7 @@ public function trap($signals, $callback)
3031
$this->getApplication()->getSignalRegistry(),
3132
);
3233

33-
collect(Arr::wrap(value($signals)))
34+
(new Collection(Arr::wrap(value($signals))))
3435
->each(fn ($signal) => $this->signals->register($signal, $callback));
3536
});
3637
}

src/Illuminate/Console/GeneratorCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Console\Concerns\CreatesMatchingTest;
66
use Illuminate\Contracts\Console\PromptsForMissingInput;
77
use Illuminate\Filesystem\Filesystem;
8+
use Illuminate\Support\Collection;
89
use Illuminate\Support\Str;
910
use Symfony\Component\Console\Input\InputArgument;
1011
use Symfony\Component\Finder\Finder;
@@ -248,7 +249,7 @@ protected function possibleModels()
248249
{
249250
$modelPath = is_dir(app_path('Models')) ? app_path('Models') : app_path();
250251

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

271-
return collect(Finder::create()->files()->depth(0)->in($eventPath))
272+
return (new Collection(Finder::create()->files()->depth(0)->in($eventPath)))
272273
->map(fn ($file) => $file->getBasename('.php'))
273274
->sort()
274275
->values()
@@ -460,7 +461,7 @@ protected function isReservedName($name)
460461
{
461462
return in_array(
462463
strtolower($name),
463-
collect($this->reservedNames)
464+
(new Collection($this->reservedNames))
464465
->transform(fn ($name) => strtolower($name))
465466
->all()
466467
);

src/Illuminate/Console/Scheduling/Schedule.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Contracts\Queue\ShouldBeUnique;
1515
use Illuminate\Contracts\Queue\ShouldQueue;
1616
use Illuminate\Queue\CallQueuedClosure;
17+
use Illuminate\Support\Collection;
1718
use Illuminate\Support\ProcessUtils;
1819
use Illuminate\Support\Traits\Macroable;
1920
use RuntimeException;
@@ -332,7 +333,7 @@ protected function mergePendingAttributes(Event $event)
332333
*/
333334
protected function compileParameters(array $parameters)
334335
{
335-
return collect($parameters)->map(function ($value, $key) {
336+
return (new Collection($parameters))->map(function ($value, $key) {
336337
if (is_array($value)) {
337338
return $this->compileArrayInput($key, $value);
338339
}
@@ -354,7 +355,7 @@ protected function compileParameters(array $parameters)
354355
*/
355356
public function compileArrayInput($key, $value)
356357
{
357-
$value = collect($value)->map(function ($value) {
358+
$value = (new Collection($value))->map(function ($value) {
358359
return ProcessUtils::escapeArgument($value);
359360
});
360361

@@ -391,7 +392,7 @@ public function serverShouldRun(Event $event, DateTimeInterface $time)
391392
*/
392393
public function dueEvents($app)
393394
{
394-
return collect($this->events)->filter->isDue($app);
395+
return (new Collection($this->events))->filter->isDue($app);
395396
}
396397

397398
/**

src/Illuminate/Console/Scheduling/ScheduleFinishCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Console\Command;
66
use Illuminate\Console\Events\ScheduledBackgroundTaskFinished;
77
use Illuminate\Contracts\Events\Dispatcher;
8+
use Illuminate\Support\Collection;
89
use Symfony\Component\Console\Attribute\AsCommand;
910

1011
#[AsCommand(name: 'schedule:finish')]
@@ -39,7 +40,7 @@ class ScheduleFinishCommand extends Command
3940
*/
4041
public function handle(Schedule $schedule)
4142
{
42-
collect($schedule->events())->filter(function ($value) {
43+
(new Collection($schedule->events()))->filter(function ($value) {
4344
return $value->mutexName() == $this->argument('id');
4445
})->each(function ($event) {
4546
$event->finish($this->laravel, $this->argument('code'));

src/Illuminate/Console/Scheduling/ScheduleListCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Console\Application;
99
use Illuminate\Console\Command;
1010
use Illuminate\Support\Carbon;
11+
use Illuminate\Support\Collection;
1112
use ReflectionClass;
1213
use ReflectionFunction;
1314
use Symfony\Component\Console\Attribute\AsCommand;
@@ -50,7 +51,7 @@ class ScheduleListCommand extends Command
5051
*/
5152
public function handle(Schedule $schedule)
5253
{
53-
$events = collect($schedule->events());
54+
$events = new Collection($schedule->events());
5455

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

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

9394
/**
@@ -244,7 +245,7 @@ private function formatCronExpression($expression, $spacing)
244245
{
245246
$expressions = preg_split("/\s+/", $expression);
246247

247-
return collect($spacing)
248+
return (new Collection($spacing))
248249
->map(fn ($length, $index) => str_pad($expressions[$index], $length))
249250
->implode(' ');
250251
}

0 commit comments

Comments
 (0)