Skip to content

Commit

Permalink
[11.x] prefer new Collection() over collect() (#53563)
Browse files Browse the repository at this point in the history
* wip

* wip

* minor formatting
  • Loading branch information
browner12 authored Nov 18, 2024
1 parent 8f5cb77 commit 2bf723a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
5 changes: 3 additions & 2 deletions Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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));
Expand Down
15 changes: 8 additions & 7 deletions Compilers/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.'::');
})
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand All @@ -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})"
Expand Down
5 changes: 3 additions & 2 deletions ComponentAttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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]) &&
Expand Down
5 changes: 3 additions & 2 deletions View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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('');
}

/**
Expand Down Expand Up @@ -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('');
}

/**
Expand Down

0 comments on commit 2bf723a

Please sign in to comment.