Skip to content

Commit

Permalink
always alias the Illuminate\Database\Eloquent\Collection (#53735)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
browner12 authored Dec 3, 2024
1 parent 2176406 commit 507f325
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -296,7 +296,7 @@ public function match(array $models, Collection $results, $relation)
* @param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $results
* @return array<array<string, TRelatedModel>>
*/
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -119,7 +119,7 @@ public function addEagerConstraints(array $models)
* @param string $relation
* @return array<int, TDeclaringModel>
*/
public function matchOne(array $models, Collection $results, $relation)
public function matchOne(array $models, EloquentCollection $results, $relation)
{
return $this->matchOneOrMany($models, $results, $relation, 'one');
}
Expand All @@ -132,7 +132,7 @@ public function matchOne(array $models, Collection $results, $relation)
* @param string $relation
* @return array<int, TDeclaringModel>
*/
public function matchMany(array $models, Collection $results, $relation)
public function matchMany(array $models, EloquentCollection $results, $relation)
{
return $this->matchOneOrMany($models, $results, $relation, 'many');
}
Expand All @@ -146,7 +146,7 @@ public function matchMany(array $models, Collection $results, $relation)
* @param string $type
* @return array<int, TDeclaringModel>
*/
protected function matchOneOrMany(array $models, Collection $results, $relation, $type)
protected function matchOneOrMany(array $models, EloquentCollection $results, $relation, $type)
{
$dictionary = $this->buildDictionary($results);

Expand Down Expand Up @@ -189,7 +189,7 @@ protected function getRelationValue(array $dictionary, $key, $type)
* @param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $results
* @return array<array<int, TRelatedModel>>
*/
protected function buildDictionary(Collection $results)
protected function buildDictionary(EloquentCollection $results)
{
$foreign = $this->getForeignKeyName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -178,7 +178,7 @@ public function addEagerConstraints(array $models)
* @param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $results
* @return array<array<TRelatedModel>>
*/
protected function buildDictionary(Collection $results)
protected function buildDictionary(EloquentCollection $results)
{
$dictionary = [];

Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/MorphMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/MorphOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -96,7 +96,7 @@ public function __construct(Builder $query, Model $parent, $foreignKey, $ownerKe
#[\Override]
public function addEagerConstraints(array $models)
{
$this->buildDictionary($this->models = new Collection($models));
$this->buildDictionary($this->models = new EloquentCollection($models));
}

/**
Expand All @@ -105,7 +105,7 @@ public function addEagerConstraints(array $models)
* @param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $models
* @return void
*/
protected function buildDictionary(Collection $models)
protected function buildDictionary(EloquentCollection $models)
{
foreach ($models as $model) {
if ($model->{$this->morphType}) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -213,7 +213,7 @@ public function match(array $models, Collection $results, $relation)
* @param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $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();
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -152,7 +152,7 @@ abstract public function initRelation(array $models, $relation);
* @param string $relation
* @return array<int, TDeclaringModel>
*/
abstract public function match(array $models, Collection $results, $relation);
abstract public function match(array $models, EloquentCollection $results, $relation);

/**
* Get the results of the relationship.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Illuminate\Notifications;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;

/**
* @template TKey of array-key
* @template TModel of DatabaseNotification
*
* @extends \Illuminate\Database\Eloquent\Collection<TKey, TModel>
*/
class DatabaseNotificationCollection extends Collection
class DatabaseNotificationCollection extends EloquentCollection
{
/**
* Mark all notifications as read.
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Testing/TestView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)));
Expand Down

0 comments on commit 507f325

Please sign in to comment.