Skip to content

Commit 6d2e91e

Browse files
+ Added support for new relation generics
1 parent df4012a commit 6d2e91e

File tree

1 file changed

+55
-8
lines changed

1 file changed

+55
-8
lines changed

src/RelationJoinQuery.php

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Reedware\LaravelRelationJoins;
44

55
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Database\Eloquent\Relations\BelongsTo;
78
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
89
use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -16,10 +17,32 @@
1617
use Illuminate\Database\Eloquent\Relations\Relation;
1718
use InvalidArgumentException;
1819

20+
/**
21+
* @template TRelatedModel of Model
22+
* @template TDeclaringModel of Model
23+
* @template TIntermediateModel of Model
24+
* @template TResult
25+
*
26+
* @phpstan-type TRelation Relation<TRelatedModel,TDeclaringModel,TResult>
27+
* @phpstan-type TBelongsTo BelongsTo<TRelatedModel,TDeclaringModel>
28+
* @phpstan-type TBelongsToMany BelongsToMany<TRelatedModel,TDeclaringModel>
29+
* @phpstan-type THasOne HasOne<TRelatedModel,TDeclaringModel>
30+
* @phpstan-type THasMany HasMany<TRelatedModel,TDeclaringModel>
31+
* @phpstan-type TMorphOne MorphOne<TRelatedModel,TDeclaringModel>
32+
* @phpstan-type TMorphMany MorphMany<TRelatedModel,TDeclaringModel>
33+
* @phpstan-type THasOneThrough HasOneThrough<TRelatedModel,TIntermediateModel,TDeclaringModel>
34+
* @phpstan-type THasManyThrough HasManyThrough<TRelatedModel,TIntermediateModel,TDeclaringModel>
35+
* @phpstan-type TMorphToMany MorphToMany<TRelatedModel,TDeclaringModel>
36+
*/
1937
class RelationJoinQuery
2038
{
2139
/**
2240
* Adds the constraints for a relationship join.
41+
*
42+
* @param TRelation $relation
43+
* @param Builder<TRelatedModel> $query
44+
* @param Builder<TDeclaringModel> $parentQuery
45+
* @return Builder<TRelatedModel>
2346
*/
2447
public static function get(
2548
Relation $relation,
@@ -53,6 +76,11 @@ public static function get(
5376

5477
/**
5578
* Adds the constraints for a belongs to relationship join.
79+
*
80+
* @param TBelongsTo $relation
81+
* @param Builder<TRelatedModel> $query
82+
* @param Builder<TDeclaringModel> $parentQuery
83+
* @return Builder<TRelatedModel>
5684
*/
5785
protected static function belongsTo(
5886
BelongsTo $relation,
@@ -80,6 +108,11 @@ protected static function belongsTo(
80108

81109
/**
82110
* Adds the constraints for a belongs to many relationship join.
111+
*
112+
* @param TBelongsToMany $relation
113+
* @param Builder<TRelatedModel> $query
114+
* @param Builder<TDeclaringModel> $parentQuery
115+
* @return Builder<TRelatedModel>
83116
*/
84117
protected static function belongsToMany(
85118
BelongsToMany $relation,
@@ -122,7 +155,7 @@ protected static function belongsToMany(
122155

123156
if (($using = $relation->getPivotClass()) != Pivot::class) {
124157
$query->getQuery()->joins[0] = new EloquentJoinClause(
125-
$query->getQuery()->joins[0],
158+
$query->getQuery()->joins[0], // @phpstan-ignore offsetAccess.notFound (Join is added above)
126159
(new $using)->setTable($on)
127160
);
128161
}
@@ -138,6 +171,11 @@ protected static function belongsToMany(
138171

139172
/**
140173
* Adds the constraints for a has one or has many relationship join.
174+
*
175+
* @param THasOne|THasMany|TMorphOne|TMorphMany $relation
176+
* @param Builder<TRelatedModel> $query
177+
* @param Builder<TDeclaringModel> $parentQuery
178+
* @return Builder<TRelatedModel>
141179
*/
142180
protected static function hasOneOrMany(
143181
HasOne|HasMany|MorphOne|MorphMany $relation,
@@ -166,11 +204,10 @@ protected static function hasOneOrMany(
166204
/**
167205
* Adds the constraints for a has one through or has many through relationship join.
168206
*
169-
* Soft deletes on the parent model are not handled correctly until 7.10.0.
170-
* Most of the functionality works as expected otherwise. Given that 6.x
171-
* is nearing EoL, and 7.x is already EoL, we'll let it slide for now.
172-
*
173-
* @see https://github.com/laravel/framework/commit/de4c42f04d609b119a4e0a7e6223c37bfe54cb87
207+
* @param THasOneThrough|THasManyThrough $relation
208+
* @param Builder<TRelatedModel> $query
209+
* @param Builder<TDeclaringModel> $parentQuery
210+
* @return Builder<TRelatedModel>
174211
*/
175212
protected static function hasOneOrManyThrough(
176213
HasOneThrough|HasManyThrough $relation,
@@ -220,7 +257,7 @@ protected static function hasOneOrManyThrough(
220257
// scopes, we are going to define the query through eloquent instead.
221258

222259
$query->getQuery()->joins[0] = new EloquentJoinClause(
223-
$query->getQuery()->joins[0],
260+
$query->getQuery()->joins[0], // @phpstan-ignore offsetAccess.notFound (Join added above)
224261
$relation->getParent()->newInstance()->setTable($on)
225262
);
226263

@@ -233,6 +270,11 @@ protected static function hasOneOrManyThrough(
233270

234271
/**
235272
* Adds the constraints for a morph one or morph many relationship join.
273+
*
274+
* @param TMorphOne|TMorphMany $relation
275+
* @param Builder<TRelatedModel> $query
276+
* @param Builder<TDeclaringModel> $parentQuery
277+
* @return Builder<TRelatedModel>
236278
*/
237279
protected static function morphOneOrMany(
238280
MorphOne|MorphMany $relation,
@@ -254,6 +296,11 @@ protected static function morphOneOrMany(
254296

255297
/**
256298
* Adds the constraints for a morph to many relationship join.
299+
*
300+
* @param TMorphToMany $relation
301+
* @param Builder<TRelatedModel> $query
302+
* @param Builder<TDeclaringModel> $parentQuery
303+
* @return Builder<TRelatedModel>
257304
*/
258305
protected static function morphToMany(
259306
MorphToMany $relation,
@@ -298,7 +345,7 @@ protected static function morphToMany(
298345

299346
if (($using = $relation->getPivotClass()) != Pivot::class) {
300347
$query->getQuery()->joins[0] = new EloquentJoinClause(
301-
$query->getQuery()->joins[0],
348+
$query->getQuery()->joins[0], // @phpstan-ignore offsetAccess.notFound (Join added above)
302349
(new $using)->setTable($on)
303350
);
304351
}

0 commit comments

Comments
 (0)