From 2bf723ac9dbf1ccdb8b9c35d5ebcec43d8342e38 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Mon, 18 Nov 2024 15:42:02 -0600 Subject: [PATCH] [11.x] prefer `new Collection()` over `collect()` (#53563) * wip * wip * minor formatting --- Compilers/BladeCompiler.php | 5 +++-- Compilers/ComponentTagCompiler.php | 15 ++++++++------- ComponentAttributeBag.php | 5 +++-- View.php | 5 +++-- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Compilers/BladeCompiler.php b/Compilers/BladeCompiler.php index 396c21a45..7ea0b6a4f 100644 --- a/Compilers/BladeCompiler.php +++ b/Compilers/BladeCompiler.php @@ -7,6 +7,7 @@ use Illuminate\Contracts\View\Factory as ViewFactory; use Illuminate\Contracts\View\View; use Illuminate\Support\Arr; +use Illuminate\Support\Collection; use Illuminate\Support\Str; use Illuminate\Support\Traits\ReflectsClosures; use Illuminate\View\Component; @@ -220,7 +221,7 @@ protected function appendFilePath($contents) */ protected function getOpenAndClosingPhpTokens($contents) { - return collect(token_get_all($contents)) + return (new Collection(token_get_all($contents))) ->pluck(0) ->filter(function ($token) { return in_array($token, [T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO, T_CLOSE_TAG]); @@ -760,7 +761,7 @@ public function component($class, $alias = null, $prefix = '') if (is_null($alias)) { $alias = str_contains($class, '\\View\\Components\\') - ? collect(explode('\\', Str::after($class, '\\View\\Components\\')))->map(function ($segment) { + ? (new Collection(explode('\\', Str::after($class, '\\View\\Components\\'))))->map(function ($segment) { return Str::kebab($segment); })->implode(':') : Str::kebab(class_basename($class)); diff --git a/Compilers/ComponentTagCompiler.php b/Compilers/ComponentTagCompiler.php index b0a41d079..0c9f5316f 100644 --- a/Compilers/ComponentTagCompiler.php +++ b/Compilers/ComponentTagCompiler.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\View\Factory; use Illuminate\Filesystem\Filesystem; +use Illuminate\Support\Collection; use Illuminate\Support\Str; use Illuminate\View\AnonymousComponent; use Illuminate\View\DynamicComponent; @@ -362,7 +363,7 @@ protected function guessAnonymousComponentUsingPaths(Factory $viewFactory, strin */ protected function guessAnonymousComponentUsingNamespaces(Factory $viewFactory, string $component) { - return collect($this->blade->getAnonymousComponentNamespaces()) + return (new Collection($this->blade->getAnonymousComponentNamespaces())) ->filter(function ($directory, $prefix) use ($component) { return Str::startsWith($component, $prefix.'::'); }) @@ -478,16 +479,16 @@ public function partitionDataAndAttributes($class, array $attributes) // return all of the attributes as both data and attributes since we have // now way to partition them. The user can exclude attributes manually. if (! class_exists($class)) { - return [collect($attributes), collect($attributes)]; + return [new Collection($attributes), new Collection($attributes)]; } $constructor = (new ReflectionClass($class))->getConstructor(); $parameterNames = $constructor - ? collect($constructor->getParameters())->map->getName()->all() + ? (new Collection($constructor->getParameters()))->map->getName()->all() : []; - return collect($attributes)->partition(function ($value, $key) use ($parameterNames) { + return (new Collection($attributes))->partition(function ($value, $key) use ($parameterNames) { return in_array(Str::camel($key), $parameterNames); })->all(); } @@ -618,7 +619,7 @@ protected function getAttributesFromAttributeString(string $attributeString) return []; } - return collect($matches)->mapWithKeys(function ($match) { + return (new Collection($matches))->mapWithKeys(function ($match) { $attribute = $match['attribute']; $value = $match['value'] ?? null; @@ -765,7 +766,7 @@ protected function compileAttributeEchos(string $attributeString) */ protected function escapeSingleQuotesOutsideOfPhpBlocks(string $value) { - return collect(token_get_all($value))->map(function ($token) { + return (new Collection(token_get_all($value)))->map(function ($token) { if (! is_array($token)) { return $token; } @@ -785,7 +786,7 @@ protected function escapeSingleQuotesOutsideOfPhpBlocks(string $value) */ protected function attributesToString(array $attributes, $escapeBound = true) { - return collect($attributes) + return (new Collection($attributes)) ->map(function (string $value, string $attribute) use ($escapeBound) { return $escapeBound && isset($this->boundAttributes[$attribute]) && $value !== 'true' && ! is_numeric($value) ? "'{$attribute}' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute({$value})" diff --git a/ComponentAttributeBag.php b/ComponentAttributeBag.php index 0e3fdc3ed..befc983b3 100644 --- a/ComponentAttributeBag.php +++ b/ComponentAttributeBag.php @@ -6,6 +6,7 @@ use ArrayIterator; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Support\Arr; +use Illuminate\Support\Collection; use Illuminate\Support\HtmlString; use Illuminate\Support\Str; use Illuminate\Support\Traits\Conditionable; @@ -169,7 +170,7 @@ public function except($keys) */ public function filter($callback) { - return new static(collect($this->attributes)->filter($callback)->all()); + return new static((new Collection($this->attributes))->filter($callback)->all()); } /** @@ -272,7 +273,7 @@ public function merge(array $attributeDefaults = [], $escape = true) : $value; }, $attributeDefaults); - [$appendableAttributes, $nonAppendableAttributes] = collect($this->attributes) + [$appendableAttributes, $nonAppendableAttributes] = (new Collection($this->attributes)) ->partition(function ($value, $key) use ($attributeDefaults) { return $key === 'class' || $key === 'style' || ( isset($attributeDefaults[$key]) && diff --git a/View.php b/View.php index 0751a8dff..7405b33d5 100755 --- a/View.php +++ b/View.php @@ -10,6 +10,7 @@ use Illuminate\Contracts\Support\Renderable; use Illuminate\Contracts\View\Engine; use Illuminate\Contracts\View\View as ViewContract; +use Illuminate\Support\Collection; use Illuminate\Support\MessageBag; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; @@ -101,7 +102,7 @@ public function fragments(?array $fragments = null) { return is_null($fragments) ? $this->allFragments() - : collect($fragments)->map(fn ($f) => $this->fragment($f))->implode(''); + : (new Collection($fragments))->map(fn ($f) => $this->fragment($f))->implode(''); } /** @@ -143,7 +144,7 @@ public function fragmentsIf($boolean, ?array $fragments = null) */ protected function allFragments() { - return collect($this->render(fn () => $this->factory->getFragments()))->implode(''); + return (new Collection($this->render(fn () => $this->factory->getFragments())))->implode(''); } /**