From 93642da76c76090bda50eefa7e72624b78908c8a Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Mon, 2 Dec 2024 16:32:27 -0600 Subject: [PATCH] always alias the `Illuminate\Database\Eloquent\Collection` while there is no behavior change in this commit, my intent behind this is to help prevent stupid mistakes from assuming what Collection is being referenced. by always aliasing this class explicitly, we gain a little confidence when in the code about what version we are referencing. our use of `Illuminate\Support\Collection` far outweights our use of `Illuminate\Database\Eloquent\Collection`. however, it's very common for both to be used in a file that uses `Illuminate\Database\Eloquent\Collection`. therefore, I say we treat the base Collection as the default, and our Eloquent Collection as our alias ALL the time. this will provide consistency and help avoid stupid mistakes. it is also helpful when doing global searches because now we have a unique token to search for. this idea came to me as I was working on #53726 because making sure I was referencing the correct Collection was the gotcha I had to pay the closest attention to. --- .../Database/Eloquent/Concerns/HasRelationships.php | 4 ++-- .../Eloquent/Concerns/QueriesRelationships.php | 4 ++-- .../Database/Eloquent/Relations/BelongsTo.php | 4 ++-- .../Database/Eloquent/Relations/BelongsToMany.php | 6 +++--- .../Relations/Concerns/InteractsWithPivotTable.php | 4 ++-- src/Illuminate/Database/Eloquent/Relations/HasMany.php | 4 ++-- .../Database/Eloquent/Relations/HasManyThrough.php | 4 ++-- src/Illuminate/Database/Eloquent/Relations/HasOne.php | 4 ++-- .../Database/Eloquent/Relations/HasOneOrMany.php | 10 +++++----- .../Eloquent/Relations/HasOneOrManyThrough.php | 4 ++-- .../Database/Eloquent/Relations/HasOneThrough.php | 4 ++-- .../Database/Eloquent/Relations/MorphMany.php | 4 ++-- .../Database/Eloquent/Relations/MorphOne.php | 4 ++-- src/Illuminate/Database/Eloquent/Relations/MorphTo.php | 10 +++++----- .../Database/Eloquent/Relations/Relation.php | 4 ++-- .../Notifications/DatabaseNotificationCollection.php | 4 ++-- src/Illuminate/Testing/TestView.php | 6 +++--- 17 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index c62132a26185..0664e80780bb 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -5,7 +5,7 @@ use Closure; use Illuminate\Database\ClassMorphViolationException; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\PendingHasThroughRelationship; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -804,7 +804,7 @@ public function touchOwners() $this->$relation->fireModelEvent('saved', false); $this->$relation->touchOwners(); - } elseif ($this->$relation instanceof Collection) { + } elseif ($this->$relation instanceof EloquentCollection) { $this->$relation->each->touchOwners(); } } diff --git a/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php index 3a33343d7e67..ff9e7ecd45e0 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php @@ -5,7 +5,7 @@ use BadMethodCallException; use Closure; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\RelationNotFoundException; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphTo; @@ -550,7 +550,7 @@ public function orWhereNotMorphedTo($relation, $model) */ public function whereBelongsTo($related, $relationshipName = null, $boolean = 'and') { - if (! $related instanceof Collection) { + if (! $related instanceof EloquentCollection) { $relatedCollection = $related->newCollection([$related]); } else { $relatedCollection = $related; diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php index 7822de3c2ad0..d38d512af924 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php @@ -3,7 +3,7 @@ namespace Illuminate\Database\Eloquent\Relations; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Concerns\ComparesRelatedModels; use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; @@ -150,7 +150,7 @@ public function initRelation(array $models, $relation) } /** @inheritDoc */ - public function match(array $models, Collection $results, $relation) + public function match(array $models, EloquentCollection $results, $relation) { // First we will get to build a dictionary of the child models by their primary // key of the relationship, then we can easily match the children back onto diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index 8271e8e5d8f9..a7954fb5153f 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -5,7 +5,7 @@ use Closure; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot; @@ -270,7 +270,7 @@ public function initRelation(array $models, $relation) } /** @inheritDoc */ - public function match(array $models, Collection $results, $relation) + public function match(array $models, EloquentCollection $results, $relation) { $dictionary = $this->buildDictionary($results); @@ -296,7 +296,7 @@ public function match(array $models, Collection $results, $relation) * @param \Illuminate\Database\Eloquent\Collection $results * @return array> */ - protected function buildDictionary(Collection $results) + protected function buildDictionary(EloquentCollection $results) { // First we'll build a dictionary of child models keyed by the foreign key // of the relation so that we will easily and quickly match them to the diff --git a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php index 18a2501b66ef..7b13be9d6ef8 100644 --- a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php +++ b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php @@ -3,7 +3,7 @@ namespace Illuminate\Database\Eloquent\Relations\Concerns; use BackedEnum; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Pivot; use Illuminate\Support\Collection as BaseCollection; @@ -611,7 +611,7 @@ protected function parseIds($value) return [$value->{$this->relatedKey}]; } - if ($value instanceof Collection) { + if ($value instanceof EloquentCollection) { return $value->pluck($this->relatedKey)->all(); } diff --git a/src/Illuminate/Database/Eloquent/Relations/HasMany.php b/src/Illuminate/Database/Eloquent/Relations/HasMany.php index 77afb416688f..15b66f56dec6 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasMany.php @@ -2,7 +2,7 @@ namespace Illuminate\Database\Eloquent\Relations; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; /** * @template TRelatedModel of \Illuminate\Database\Eloquent\Model @@ -53,7 +53,7 @@ public function initRelation(array $models, $relation) } /** @inheritDoc */ - public function match(array $models, Collection $results, $relation) + public function match(array $models, EloquentCollection $results, $relation) { return $this->matchMany($models, $results, $relation); } diff --git a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php index 1399df347ed9..37e8410b58b6 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php @@ -3,7 +3,7 @@ namespace Illuminate\Database\Eloquent\Relations; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; /** @@ -46,7 +46,7 @@ public function initRelation(array $models, $relation) } /** @inheritDoc */ - public function match(array $models, Collection $results, $relation) + public function match(array $models, EloquentCollection $results, $relation) { $dictionary = $this->buildDictionary($results); diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOne.php b/src/Illuminate/Database/Eloquent/Relations/HasOne.php index be70cb6d79ef..911d4e26c760 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOne.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOne.php @@ -4,7 +4,7 @@ use Illuminate\Contracts\Database\Eloquent\SupportsPartialRelations; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Concerns\CanBeOneOfMany; use Illuminate\Database\Eloquent\Relations\Concerns\ComparesRelatedModels; @@ -42,7 +42,7 @@ public function initRelation(array $models, $relation) } /** @inheritDoc */ - public function match(array $models, Collection $results, $relation) + public function match(array $models, EloquentCollection $results, $relation) { return $this->matchOne($models, $results, $relation); } diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index 913728b52a10..0ba60ccc9cf7 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php @@ -3,7 +3,7 @@ namespace Illuminate\Database\Eloquent\Relations; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; use Illuminate\Database\Eloquent\Relations\Concerns\SupportsInverseRelations; @@ -119,7 +119,7 @@ public function addEagerConstraints(array $models) * @param string $relation * @return array */ - public function matchOne(array $models, Collection $results, $relation) + public function matchOne(array $models, EloquentCollection $results, $relation) { return $this->matchOneOrMany($models, $results, $relation, 'one'); } @@ -132,7 +132,7 @@ public function matchOne(array $models, Collection $results, $relation) * @param string $relation * @return array */ - public function matchMany(array $models, Collection $results, $relation) + public function matchMany(array $models, EloquentCollection $results, $relation) { return $this->matchOneOrMany($models, $results, $relation, 'many'); } @@ -146,7 +146,7 @@ public function matchMany(array $models, Collection $results, $relation) * @param string $type * @return array */ - protected function matchOneOrMany(array $models, Collection $results, $relation, $type) + protected function matchOneOrMany(array $models, EloquentCollection $results, $relation, $type) { $dictionary = $this->buildDictionary($results); @@ -189,7 +189,7 @@ protected function getRelationValue(array $dictionary, $key, $type) * @param \Illuminate\Database\Eloquent\Collection $results * @return array> */ - protected function buildDictionary(Collection $results) + protected function buildDictionary(EloquentCollection $results) { $foreign = $this->getForeignKeyName(); diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrManyThrough.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrManyThrough.php index f17152613121..5ae9204fd144 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneOrManyThrough.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneOrManyThrough.php @@ -5,7 +5,7 @@ use Closure; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; @@ -178,7 +178,7 @@ public function addEagerConstraints(array $models) * @param \Illuminate\Database\Eloquent\Collection $results * @return array> */ - protected function buildDictionary(Collection $results) + protected function buildDictionary(EloquentCollection $results) { $dictionary = []; diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php b/src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php index 7cf7850d114d..21de2e301213 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php @@ -2,7 +2,7 @@ namespace Illuminate\Database\Eloquent\Relations; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels; @@ -35,7 +35,7 @@ public function initRelation(array $models, $relation) } /** @inheritDoc */ - public function match(array $models, Collection $results, $relation) + public function match(array $models, EloquentCollection $results, $relation) { $dictionary = $this->buildDictionary($results); diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphMany.php index cbdd1d55b586..86fab64d4e80 100755 --- a/src/Illuminate/Database/Eloquent/Relations/MorphMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphMany.php @@ -2,7 +2,7 @@ namespace Illuminate\Database\Eloquent\Relations; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; /** * @template TRelatedModel of \Illuminate\Database\Eloquent\Model @@ -54,7 +54,7 @@ public function initRelation(array $models, $relation) } /** @inheritDoc */ - public function match(array $models, Collection $results, $relation) + public function match(array $models, EloquentCollection $results, $relation) { return $this->matchMany($models, $results, $relation); } diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphOne.php b/src/Illuminate/Database/Eloquent/Relations/MorphOne.php index e9d7dd267518..fa3632efb3d1 100755 --- a/src/Illuminate/Database/Eloquent/Relations/MorphOne.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphOne.php @@ -4,7 +4,7 @@ use Illuminate\Contracts\Database\Eloquent\SupportsPartialRelations; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Concerns\CanBeOneOfMany; use Illuminate\Database\Eloquent\Relations\Concerns\ComparesRelatedModels; @@ -42,7 +42,7 @@ public function initRelation(array $models, $relation) } /** @inheritDoc */ - public function match(array $models, Collection $results, $relation) + public function match(array $models, EloquentCollection $results, $relation) { return $this->matchOne($models, $results, $relation); } diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php index 9dc5dd8b98ce..09129d2220aa 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php @@ -4,7 +4,7 @@ use BadMethodCallException; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; @@ -96,7 +96,7 @@ public function __construct(Builder $query, Model $parent, $foreignKey, $ownerKe #[\Override] public function addEagerConstraints(array $models) { - $this->buildDictionary($this->models = Collection::make($models)); + $this->buildDictionary($this->models = EloquentCollection::make($models)); } /** @@ -105,7 +105,7 @@ public function addEagerConstraints(array $models) * @param \Illuminate\Database\Eloquent\Collection $models * @return void */ - protected function buildDictionary(Collection $models) + protected function buildDictionary(EloquentCollection $models) { foreach ($models as $model) { if ($model->{$this->morphType}) { @@ -201,7 +201,7 @@ public function createModelByType($type) /** @inheritDoc */ #[\Override] - public function match(array $models, Collection $results, $relation) + public function match(array $models, EloquentCollection $results, $relation) { return $models; } @@ -213,7 +213,7 @@ public function match(array $models, Collection $results, $relation) * @param \Illuminate\Database\Eloquent\Collection $results * @return void */ - protected function matchToMorphParents($type, Collection $results) + protected function matchToMorphParents($type, EloquentCollection $results) { foreach ($results as $result) { $ownerKey = ! is_null($this->ownerKey) ? $this->getDictionaryKey($result->{$this->ownerKey}) : $result->getKey(); diff --git a/src/Illuminate/Database/Eloquent/Relations/Relation.php b/src/Illuminate/Database/Eloquent/Relations/Relation.php index d5caa8880669..7c72e49faabd 100755 --- a/src/Illuminate/Database/Eloquent/Relations/Relation.php +++ b/src/Illuminate/Database/Eloquent/Relations/Relation.php @@ -5,7 +5,7 @@ use Closure; use Illuminate\Contracts\Database\Eloquent\Builder as BuilderContract; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\MultipleRecordsFoundException; @@ -152,7 +152,7 @@ abstract public function initRelation(array $models, $relation); * @param string $relation * @return array */ - abstract public function match(array $models, Collection $results, $relation); + abstract public function match(array $models, EloquentCollection $results, $relation); /** * Get the results of the relationship. diff --git a/src/Illuminate/Notifications/DatabaseNotificationCollection.php b/src/Illuminate/Notifications/DatabaseNotificationCollection.php index 5bac0272dd7b..03e529adb47b 100644 --- a/src/Illuminate/Notifications/DatabaseNotificationCollection.php +++ b/src/Illuminate/Notifications/DatabaseNotificationCollection.php @@ -2,7 +2,7 @@ namespace Illuminate\Notifications; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; /** * @template TKey of array-key @@ -10,7 +10,7 @@ * * @extends \Illuminate\Database\Eloquent\Collection */ -class DatabaseNotificationCollection extends Collection +class DatabaseNotificationCollection extends EloquentCollection { /** * Mark all notifications as read. diff --git a/src/Illuminate/Testing/TestView.php b/src/Illuminate/Testing/TestView.php index 50a3bd4ed928..a31a814ab402 100644 --- a/src/Illuminate/Testing/TestView.php +++ b/src/Illuminate/Testing/TestView.php @@ -3,7 +3,7 @@ namespace Illuminate\Testing; use Closure; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; use Illuminate\Support\Traits\Macroable; @@ -61,10 +61,10 @@ public function assertViewHas($key, $value = null) PHPUnit::assertTrue($value(Arr::get($this->view->gatherData(), $key))); } elseif ($value instanceof Model) { PHPUnit::assertTrue($value->is(Arr::get($this->view->gatherData(), $key))); - } elseif ($value instanceof Collection) { + } elseif ($value instanceof EloquentCollection) { $actual = Arr::get($this->view->gatherData(), $key); - PHPUnit::assertInstanceOf(Collection::class, $actual); + PHPUnit::assertInstanceOf(EloquentCollection::class, $actual); PHPUnit::assertSameSize($value, $actual); $value->each(fn ($item, $index) => PHPUnit::assertTrue($actual->get($index)->is($item)));