Skip to content

Commit 4529498

Browse files
calebdwdriesvintsstaudenmeir
authored
[11.x] feat: add generics to Eloquent Builder and Relations (#51851)
* feat: add builder and relation generics * Update src/Illuminate/Auth/EloquentUserProvider.php Co-authored-by: Jonas Staudenmeir <[email protected]> * test: add additional type tests * feat: create HasBuilder trait --------- Co-authored-by: Dries Vints <[email protected]> Co-authored-by: Jonas Staudenmeir <[email protected]>
1 parent f14318f commit 4529498

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2321
-1603
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
"nyholm/psr7": "^1.2",
109109
"orchestra/testbench-core": "^9.1.5",
110110
"pda/pheanstalk": "^5.0",
111-
"phpstan/phpstan": "^1.4.7",
111+
"phpstan/phpstan": "^1.11.5",
112112
"phpunit/phpunit": "^10.5|^11.0",
113113
"predis/predis": "^2.0.2",
114114
"resend/resend-php": "^0.10.0",

src/Illuminate/Auth/EloquentUserProvider.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class EloquentUserProvider implements UserProvider
2727
/**
2828
* The callback that may modify the user retrieval queries.
2929
*
30-
* @var (\Closure(\Illuminate\Database\Eloquent\Builder):mixed)|null
30+
* @var (\Closure(\Illuminate\Database\Eloquent\Builder<*>):mixed)|null
3131
*/
3232
protected $queryCallback;
3333

@@ -177,8 +177,10 @@ public function rehashPasswordIfRequired(UserContract $user, #[\SensitiveParamet
177177
/**
178178
* Get a new query builder for the model instance.
179179
*
180-
* @param \Illuminate\Database\Eloquent\Model|null $model
181-
* @return \Illuminate\Database\Eloquent\Builder
180+
* @template TModel of \Illuminate\Database\Eloquent\Model
181+
*
182+
* @param TModel|null $model
183+
* @return \Illuminate\Database\Eloquent\Builder<TModel>
182184
*/
183185
protected function newModelQuery($model = null)
184186
{
@@ -252,7 +254,7 @@ public function setModel($model)
252254
/**
253255
* Get the callback that modifies the query before retrieving users.
254256
*
255-
* @return \Closure|null
257+
* @return (\Closure(\Illuminate\Database\Eloquent\Builder<*>):mixed)|null
256258
*/
257259
public function getQueryCallback()
258260
{
@@ -262,7 +264,7 @@ public function getQueryCallback()
262264
/**
263265
* Sets the callback to modify the query before retrieving users.
264266
*
265-
* @param (\Closure(\Illuminate\Database\Eloquent\Builder):mixed)|null $queryCallback
267+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<*>):mixed)|null $queryCallback
266268
* @return $this
267269
*/
268270
public function withQuery($queryCallback = null)

src/Illuminate/Database/Concerns/BuildsQueries.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
use InvalidArgumentException;
1919
use RuntimeException;
2020

21+
/**
22+
* @template TValue
23+
*
24+
* @mixin \Illuminate\Database\Eloquent\Builder
25+
* @mixin \Illuminate\Database\Query\Builder
26+
*/
2127
trait BuildsQueries
2228
{
2329
use Conditionable;
@@ -26,7 +32,7 @@ trait BuildsQueries
2632
* Chunk the results of the query.
2733
*
2834
* @param int $count
29-
* @param callable $callback
35+
* @param callable(\Illuminate\Support\Collection<int, TValue>, int): mixed $callback
3036
* @return bool
3137
*/
3238
public function chunk($count, callable $callback)
@@ -65,9 +71,11 @@ public function chunk($count, callable $callback)
6571
/**
6672
* Run a map over each item while chunking.
6773
*
68-
* @param callable $callback
74+
* @template TReturn
75+
*
76+
* @param callable(TValue): TReturn $callback
6977
* @param int $count
70-
* @return \Illuminate\Support\Collection
78+
* @return \Illuminate\Support\Collection<int, TReturn>
7179
*/
7280
public function chunkMap(callable $callback, $count = 1000)
7381
{
@@ -85,7 +93,7 @@ public function chunkMap(callable $callback, $count = 1000)
8593
/**
8694
* Execute a callback over each item while chunking.
8795
*
88-
* @param callable $callback
96+
* @param callable(TValue, int): mixed $callback
8997
* @param int $count
9098
* @return bool
9199
*
@@ -106,7 +114,7 @@ public function each(callable $callback, $count = 1000)
106114
* Chunk the results of a query by comparing IDs.
107115
*
108116
* @param int $count
109-
* @param callable $callback
117+
* @param callable(\Illuminate\Support\Collection<int, TValue>, int): mixed $callback
110118
* @param string|null $column
111119
* @param string|null $alias
112120
* @return bool
@@ -120,7 +128,7 @@ public function chunkById($count, callable $callback, $column = null, $alias = n
120128
* Chunk the results of a query by comparing IDs in descending order.
121129
*
122130
* @param int $count
123-
* @param callable $callback
131+
* @param callable(\Illuminate\Support\Collection<int, TValue>, int): mixed $callback
124132
* @param string|null $column
125133
* @param string|null $alias
126134
* @return bool
@@ -134,7 +142,7 @@ public function chunkByIdDesc($count, callable $callback, $column = null, $alias
134142
* Chunk the results of a query by comparing IDs in a given order.
135143
*
136144
* @param int $count
137-
* @param callable $callback
145+
* @param callable(\Illuminate\Support\Collection<int, TValue>, int): mixed $callback
138146
* @param string|null $column
139147
* @param string|null $alias
140148
* @param bool $descending
@@ -194,7 +202,7 @@ public function orderedChunkById($count, callable $callback, $column = null, $al
194202
/**
195203
* Execute a callback over each item while chunking by ID.
196204
*
197-
* @param callable $callback
205+
* @param callable(TValue, int): mixed $callback
198206
* @param int $count
199207
* @param string|null $column
200208
* @param string|null $alias
@@ -328,7 +336,7 @@ protected function orderedLazyById($chunkSize = 1000, $column = null, $alias = n
328336
* Execute the query and get the first result.
329337
*
330338
* @param array|string $columns
331-
* @return \Illuminate\Database\Eloquent\Model|object|static|null
339+
* @return TValue|null
332340
*/
333341
public function first($columns = ['*'])
334342
{
@@ -339,7 +347,7 @@ public function first($columns = ['*'])
339347
* Execute the query and get the first result if it's the sole matching record.
340348
*
341349
* @param array|string $columns
342-
* @return \Illuminate\Database\Eloquent\Model|object|static|null
350+
* @return TValue
343351
*
344352
* @throws \Illuminate\Database\RecordsNotFoundException
345353
* @throws \Illuminate\Database\MultipleRecordsFoundException
@@ -463,7 +471,7 @@ protected function paginateUsingCursor($perPage, $columns = ['*'], $cursorName =
463471
/**
464472
* Get the original column name of the given column, without any aliasing.
465473
*
466-
* @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $builder
474+
* @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<*> $builder
467475
* @param string $parameter
468476
* @return string
469477
*/

0 commit comments

Comments
 (0)