Skip to content

Commit

Permalink
Fix incorrect PHPDoc for Builder relation
Browse files Browse the repository at this point in the history
  • Loading branch information
kayw-geek committed Sep 12, 2024
1 parent 042c267 commit 2808290
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1537,8 +1537,8 @@ protected function createNestedWhere($whereSlice, $boolean = 'and')
/**
* Set the relationships that should be eager loaded.
*
* @param array<array-key, (\Closure(\Illuminate\Database\Eloquent\Relations\Relation<*,*,*>): mixed)|string>|string $relations
* @param (\Closure(\Illuminate\Database\Eloquent\Relations\Relation<*,*,*>): mixed)|string|null $callback
* @param array<array-key, (\Closure(covariant \Illuminate\Database\Eloquent\Relations\Relation<*,*,*>): mixed)|string>|string $relations
* @param (\Closure(covariant \Illuminate\Database\Eloquent\Relations\Relation<*,*,*>): mixed)|string|null $callback
* @return $this
*/
public function with($relations, $callback = null)
Expand Down Expand Up @@ -1572,7 +1572,7 @@ public function without($relations)
/**
* Set the relationships that should be eager loaded while removing any previously added eager loading specifications.
*
* @param array<array-key, (\Closure(\Illuminate\Database\Eloquent\Relations\Relation<*,*,*>): mixed)|string>|string $relations
* @param array<array-key, (\Closure(covariant \Illuminate\Database\Eloquent\Relations\Relation<*,*,*>): mixed)|string>|string $relations
* @return $this
*/
public function withOnly($relations)
Expand Down
22 changes: 11 additions & 11 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function findOrFail($key)
/**
* Load a set of relationships onto the collection.
*
* @param array<array-key, (callable(\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @param array<array-key, (callable(covariant \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @return $this
*/
public function load($relations)
Expand All @@ -105,7 +105,7 @@ public function load($relations)
/**
* Load a set of aggregations over relationship's column onto the collection.
*
* @param array<array-key, (callable(\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @param array<array-key, (callable(covariant \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @param string $column
* @param string|null $function
* @return $this
Expand Down Expand Up @@ -142,7 +142,7 @@ public function loadAggregate($relations, $column, $function = null)
/**
* Load a set of relationship counts onto the collection.
*
* @param array<array-key, (callable(\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @param array<array-key, (callable(covariant \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @return $this
*/
public function loadCount($relations)
Expand All @@ -153,7 +153,7 @@ public function loadCount($relations)
/**
* Load a set of relationship's max column values onto the collection.
*
* @param array<array-key, (callable(\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @param array<array-key, (callable(covariant \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @param string $column
* @return $this
*/
Expand All @@ -165,7 +165,7 @@ public function loadMax($relations, $column)
/**
* Load a set of relationship's min column values onto the collection.
*
* @param array<array-key, (callable(\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @param array<array-key, (callable(covariant \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @param string $column
* @return $this
*/
Expand All @@ -177,7 +177,7 @@ public function loadMin($relations, $column)
/**
* Load a set of relationship's column summations onto the collection.
*
* @param array<array-key, (callable(\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @param array<array-key, (callable(covariant \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @param string $column
* @return $this
*/
Expand All @@ -189,7 +189,7 @@ public function loadSum($relations, $column)
/**
* Load a set of relationship's average column values onto the collection.
*
* @param array<array-key, (callable(\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @param array<array-key, (callable(covariant \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @param string $column
* @return $this
*/
Expand All @@ -201,7 +201,7 @@ public function loadAvg($relations, $column)
/**
* Load a set of related existences onto the collection.
*
* @param array<array-key, (callable(\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @param array<array-key, (callable(covariant \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @return $this
*/
public function loadExists($relations)
Expand All @@ -212,7 +212,7 @@ public function loadExists($relations)
/**
* Load a set of relationships onto the collection if they are not already eager loaded.
*
* @param array<array-key, (callable(\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @param array<array-key, (callable(covariant \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string>|string $relations
* @return $this
*/
public function loadMissing($relations)
Expand Down Expand Up @@ -284,7 +284,7 @@ protected function loadMissingRelation(self $models, array $path)
* Load a set of relationships onto the mixed relationship collection.
*
* @param string $relation
* @param array<array-key, (callable(\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string> $relations
* @param array<array-key, (callable(covariant \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string> $relations
* @return $this
*/
public function loadMorph($relation, $relations)
Expand All @@ -301,7 +301,7 @@ public function loadMorph($relation, $relations)
* Load a set of relationship counts onto the mixed relationship collection.
*
* @param string $relation
* @param array<array-key, (callable(\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string> $relations
* @param array<array-key, (callable(covariant \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|string> $relations
* @return $this
*/
public function loadMorphCount($relation, $relations)
Expand Down
7 changes: 7 additions & 0 deletions types/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\HasBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Query\Builder as QueryBuilder;
use User;
Expand All @@ -27,11 +28,17 @@ function test(
assertType('Illuminate\Database\Eloquent\Builder<User>', $query->with(['relation' => function ($query) {
// assertType('Illuminate\Database\Eloquent\Relations\Relation<*,*,*>', $query);
}]));
assertType('Illuminate\Database\Eloquent\Builder<User>', $query->with(['relation' => function (HasMany $query) :void {
assertType('Illuminate\Database\Eloquent\Relations\HasMany', $query);
}]));
assertType('Illuminate\Database\Eloquent\Builder<User>', $query->without('relation'));
assertType('Illuminate\Database\Eloquent\Builder<User>', $query->withOnly(['relation']));
assertType('Illuminate\Database\Eloquent\Builder<User>', $query->withOnly(['relation' => function ($query) {
// assertType('Illuminate\Database\Eloquent\Relations\Relation<*,*,*>', $query);
}]));
assertType('Illuminate\Database\Eloquent\Builder<User>', $query->withOnly(['relation' => function (HasOne $query) :void {
assertType('Illuminate\Database\Eloquent\Relations\HasOne', $query);
}]));
assertType('array<int, User>', $query->getModels());
assertType('array<int, User>', $query->eagerLoadRelations([]));
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $query->get());
Expand Down
5 changes: 4 additions & 1 deletion types/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Database\Eloquent\Relations\MorphTo;
use function PHPStan\Testing\assertType;

$collection = User::all();
Expand All @@ -11,9 +12,11 @@
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $collection->load('string'));
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $collection->load(['string']));
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $collection->load(['string' => function ($query) {
// assertType('Illuminate\Database\Eloquent\Relations\Relation<*,*,*>', $query);
//assertType('Illuminate\Database\Eloquent\Relations\Relation<*,*,*>', $query);
}]));

assertType('array{string: Closure(Illuminate\Database\Eloquent\Relations\MorphTo): void}',['string' => function (MorphTo $query):void {}]);
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $collection->load(['string' => function (MorphTo $query):void {}]));
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $collection->loadAggregate('string', 'string'));
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $collection->loadAggregate(['string'], 'string'));
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $collection->loadAggregate(['string'], 'string', 'string'));
Expand Down

0 comments on commit 2808290

Please sign in to comment.