Skip to content

Commit 6f5602b

Browse files
committed
feat: add better closure typing in QueriesRelationships
1 parent 43993ed commit 6f5602b

File tree

2 files changed

+241
-135
lines changed

2 files changed

+241
-135
lines changed

src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php

Lines changed: 95 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ trait QueriesRelationships
2424
/**
2525
* Add a relationship count / exists condition to the query.
2626
*
27-
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
27+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
28+
*
29+
* @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation
2830
* @param string $operator
2931
* @param int $count
3032
* @param string $boolean
31-
* @param \Closure|null $callback
33+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|null $callback
3234
* @return $this
3335
*
3436
* @throws \RuntimeException
@@ -79,7 +81,7 @@ public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', ?
7981
* @param string $operator
8082
* @param int $count
8183
* @param string $boolean
82-
* @param \Closure|null $callback
84+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<*>): mixed)|null $callback
8385
* @return $this
8486
*/
8587
protected function hasNested($relations, $operator = '>=', $count = 1, $boolean = 'and', $callback = null)
@@ -121,9 +123,11 @@ public function orHas($relation, $operator = '>=', $count = 1)
121123
/**
122124
* Add a relationship count / exists condition to the query.
123125
*
124-
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
126+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
127+
*
128+
* @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation
125129
* @param string $boolean
126-
* @param \Closure|null $callback
130+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|null $callback
127131
* @return $this
128132
*/
129133
public function doesntHave($relation, $boolean = 'and', ?Closure $callback = null)
@@ -145,8 +149,10 @@ public function orDoesntHave($relation)
145149
/**
146150
* Add a relationship count / exists condition to the query with where clauses.
147151
*
148-
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
149-
* @param \Closure|null $callback
152+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
153+
*
154+
* @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation
155+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|null $callback
150156
* @param string $operator
151157
* @param int $count
152158
* @return $this
@@ -161,8 +167,8 @@ public function whereHas($relation, ?Closure $callback = null, $operator = '>=',
161167
*
162168
* Also load the relationship with same condition.
163169
*
164-
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
165-
* @param \Closure|null $callback
170+
* @param string $relation
171+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<*>|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|null $callback
166172
* @param string $operator
167173
* @param int $count
168174
* @return $this
@@ -176,8 +182,10 @@ public function withWhereHas($relation, ?Closure $callback = null, $operator = '
176182
/**
177183
* Add a relationship count / exists condition to the query with where clauses and an "or".
178184
*
179-
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
180-
* @param \Closure|null $callback
185+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
186+
*
187+
* @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation
188+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|null $callback
181189
* @param string $operator
182190
* @param int $count
183191
* @return $this
@@ -190,8 +198,10 @@ public function orWhereHas($relation, ?Closure $callback = null, $operator = '>=
190198
/**
191199
* Add a relationship count / exists condition to the query with where clauses.
192200
*
193-
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
194-
* @param \Closure|null $callback
201+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
202+
*
203+
* @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation
204+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|null $callback
195205
* @return $this
196206
*/
197207
public function whereDoesntHave($relation, ?Closure $callback = null)
@@ -202,8 +212,10 @@ public function whereDoesntHave($relation, ?Closure $callback = null)
202212
/**
203213
* Add a relationship count / exists condition to the query with where clauses and an "or".
204214
*
205-
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
206-
* @param \Closure|null $callback
215+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
216+
*
217+
* @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation
218+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|null $callback
207219
* @return $this
208220
*/
209221
public function orWhereDoesntHave($relation, ?Closure $callback = null)
@@ -214,12 +226,14 @@ public function orWhereDoesntHave($relation, ?Closure $callback = null)
214226
/**
215227
* Add a polymorphic relationship count / exists condition to the query.
216228
*
217-
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
218-
* @param string|array $types
229+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
230+
*
231+
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation
232+
* @param string|array<int, string> $types
219233
* @param string $operator
220234
* @param int $count
221235
* @param string $boolean
222-
* @param \Closure|null $callback
236+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>, string): mixed)|null $callback
223237
* @return $this
224238
*/
225239
public function hasMorph($relation, $types, $operator = '>=', $count = 1, $boolean = 'and', ?Closure $callback = null)
@@ -292,7 +306,7 @@ protected function getBelongsToRelation(MorphTo $relation, $type)
292306
* Add a polymorphic relationship count / exists condition to the query with an "or".
293307
*
294308
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
295-
* @param string|array $types
309+
* @param string|array<int, string> $types
296310
* @param string $operator
297311
* @param int $count
298312
* @return $this
@@ -305,10 +319,12 @@ public function orHasMorph($relation, $types, $operator = '>=', $count = 1)
305319
/**
306320
* Add a polymorphic relationship count / exists condition to the query.
307321
*
308-
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
309-
* @param string|array $types
322+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
323+
*
324+
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation
325+
* @param string|array<int, string> $types
310326
* @param string $boolean
311-
* @param \Closure|null $callback
327+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>, string): mixed)|null $callback
312328
* @return $this
313329
*/
314330
public function doesntHaveMorph($relation, $types, $boolean = 'and', ?Closure $callback = null)
@@ -320,7 +336,7 @@ public function doesntHaveMorph($relation, $types, $boolean = 'and', ?Closure $c
320336
* Add a polymorphic relationship count / exists condition to the query with an "or".
321337
*
322338
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
323-
* @param string|array $types
339+
* @param string|array<int, string> $types
324340
* @return $this
325341
*/
326342
public function orDoesntHaveMorph($relation, $types)
@@ -331,9 +347,11 @@ public function orDoesntHaveMorph($relation, $types)
331347
/**
332348
* Add a polymorphic relationship count / exists condition to the query with where clauses.
333349
*
334-
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
335-
* @param string|array $types
336-
* @param \Closure|null $callback
350+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
351+
*
352+
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation
353+
* @param string|array<int, string> $types
354+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>, string): mixed)|null $callback
337355
* @param string $operator
338356
* @param int $count
339357
* @return $this
@@ -346,9 +364,11 @@ public function whereHasMorph($relation, $types, ?Closure $callback = null, $ope
346364
/**
347365
* Add a polymorphic relationship count / exists condition to the query with where clauses and an "or".
348366
*
349-
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
350-
* @param string|array $types
351-
* @param \Closure|null $callback
367+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
368+
*
369+
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation
370+
* @param string|array<int, array> $types
371+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>, string): mixed)|null $callback
352372
* @param string $operator
353373
* @param int $count
354374
* @return $this
@@ -361,9 +381,11 @@ public function orWhereHasMorph($relation, $types, ?Closure $callback = null, $o
361381
/**
362382
* Add a polymorphic relationship count / exists condition to the query with where clauses.
363383
*
364-
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
365-
* @param string|array $types
366-
* @param \Closure|null $callback
384+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
385+
*
386+
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation
387+
* @param string|array<int, string> $types
388+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>, string): mixed)|null $callback
367389
* @return $this
368390
*/
369391
public function whereDoesntHaveMorph($relation, $types, ?Closure $callback = null)
@@ -374,9 +396,11 @@ public function whereDoesntHaveMorph($relation, $types, ?Closure $callback = nul
374396
/**
375397
* Add a polymorphic relationship count / exists condition to the query with where clauses and an "or".
376398
*
377-
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
378-
* @param string|array $types
379-
* @param \Closure|null $callback
399+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
400+
*
401+
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation
402+
* @param string|array<int, string> $types
403+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>, string): mixed)|null $callback
380404
* @return $this
381405
*/
382406
public function orWhereDoesntHaveMorph($relation, $types, ?Closure $callback = null)
@@ -387,8 +411,10 @@ public function orWhereDoesntHaveMorph($relation, $types, ?Closure $callback = n
387411
/**
388412
* Add a basic where clause to a relationship query.
389413
*
390-
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
391-
* @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column
414+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
415+
*
416+
* @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation
417+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column
392418
* @param mixed $operator
393419
* @param mixed $value
394420
* @return $this
@@ -407,8 +433,10 @@ public function whereRelation($relation, $column, $operator = null, $value = nul
407433
/**
408434
* Add an "or where" clause to a relationship query.
409435
*
410-
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
411-
* @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column
436+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
437+
*
438+
* @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation
439+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column
412440
* @param mixed $operator
413441
* @param mixed $value
414442
* @return $this
@@ -427,8 +455,10 @@ public function orWhereRelation($relation, $column, $operator = null, $value = n
427455
/**
428456
* Add a basic count / exists condition to a relationship query.
429457
*
430-
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
431-
* @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column
458+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
459+
*
460+
* @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation
461+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column
432462
* @param mixed $operator
433463
* @param mixed $value
434464
* @return $this
@@ -447,8 +477,10 @@ public function whereDoesntHaveRelation($relation, $column, $operator = null, $v
447477
/**
448478
* Add an "or where" clause to a relationship query.
449479
*
450-
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
451-
* @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column
480+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
481+
*
482+
* @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string $relation
483+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column
452484
* @param mixed $operator
453485
* @param mixed $value
454486
* @return $this
@@ -467,9 +499,11 @@ public function orWhereDoesntHaveRelation($relation, $column, $operator = null,
467499
/**
468500
* Add a polymorphic relationship condition to the query with a where clause.
469501
*
470-
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
471-
* @param string|array $types
472-
* @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column
502+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
503+
*
504+
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation
505+
* @param string|array<int, string> $types
506+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column
473507
* @param mixed $operator
474508
* @param mixed $value
475509
* @return $this
@@ -484,9 +518,11 @@ public function whereMorphRelation($relation, $types, $column, $operator = null,
484518
/**
485519
* Add a polymorphic relationship condition to the query with an "or where" clause.
486520
*
487-
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
488-
* @param string|array $types
489-
* @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column
521+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
522+
*
523+
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation
524+
* @param string|array<int, string> $types
525+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column
490526
* @param mixed $operator
491527
* @param mixed $value
492528
* @return $this
@@ -501,9 +537,11 @@ public function orWhereMorphRelation($relation, $types, $column, $operator = nul
501537
/**
502538
* Add a polymorphic relationship condition to the query with a doesn't have clause.
503539
*
504-
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
505-
* @param string|array $types
506-
* @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column
540+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
541+
*
542+
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation
543+
* @param string|array<int, string> $types
544+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column
507545
* @param mixed $operator
508546
* @param mixed $value
509547
* @return $this
@@ -518,9 +556,11 @@ public function whereMorphDoesntHaveRelation($relation, $types, $column, $operat
518556
/**
519557
* Add a polymorphic relationship condition to the query with an "or doesn't have" clause.
520558
*
521-
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
522-
* @param string|array $types
523-
* @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column
559+
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
560+
*
561+
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation
562+
* @param string|array<int, string> $types
563+
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column
524564
* @param mixed $operator
525565
* @param mixed $value
526566
* @return $this

0 commit comments

Comments
 (0)