-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
use Reedware\LaravelRelationJoins\MorphTypes; | ||
use RuntimeException; | ||
|
||
/** @mixin Builder */ | ||
/** @template TModel of Model */ | ||
class JoinsRelationships | ||
{ | ||
/** | ||
|
@@ -38,7 +38,7 @@ public function joinRelation(): Closure | |
?Builder $relatedQuery = null, | ||
MorphTypes|array|string|bool $morphTypes = true | ||
): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
if (! $morphTypes instanceof MorphTypes) { | ||
Check failure on line 42 in src/Mixins/JoinsRelationships.php GitHub Actions / Static Analysis (8.2)
Check failure on line 42 in src/Mixins/JoinsRelationships.php GitHub Actions / Static Analysis (8.3)
Check failure on line 42 in src/Mixins/JoinsRelationships.php GitHub Actions / Static Analysis (8.4)
|
||
$morphTypes = new MorphTypes($morphTypes); // @phpstan-ignore-line | ||
} | ||
|
@@ -96,10 +96,8 @@ public function joinRelation(): Closure | |
$this->applyJoinScopes($joinQuery); | ||
} | ||
|
||
$joinType = $joinQuery->getJoinType(); | ||
|
||
$this->addJoinRelationWhere( | ||
$joinQuery, $relation, $joinType ?: $type | ||
$joinQuery, $relation, $type | ||
); | ||
|
||
return ! is_null($relatedQuery) ? $joinQuery : $this; | ||
|
@@ -124,7 +122,7 @@ public function joinNestedRelation(): Closure | |
bool $through, | ||
MorphTypes $morphTypes | ||
): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
$relations = explode('.', $relations); | ||
Check failure on line 126 in src/Mixins/JoinsRelationships.php GitHub Actions / Static Analysis (8.2)
Check failure on line 126 in src/Mixins/JoinsRelationships.php GitHub Actions / Static Analysis (8.3)
Check failure on line 126 in src/Mixins/JoinsRelationships.php GitHub Actions / Static Analysis (8.4)
|
||
|
||
$relatedQuery = $this; | ||
|
@@ -165,7 +163,7 @@ public function applyJoinScopes(): Closure | |
* Applies the eloquent scopes to the specified query. | ||
*/ | ||
return function (Builder $joinQuery): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
$joins = $joinQuery->getQuery()->joins ?: []; | ||
Check failure on line 167 in src/Mixins/JoinsRelationships.php GitHub Actions / Static Analysis (8.2)
Check failure on line 167 in src/Mixins/JoinsRelationships.php GitHub Actions / Static Analysis (8.3)
Check failure on line 167 in src/Mixins/JoinsRelationships.php GitHub Actions / Static Analysis (8.4)
|
||
|
||
foreach ($joins as $join) { | ||
|
@@ -187,7 +185,7 @@ public function callJoinScope(): Closure | |
* Calls the provided callback on the join query. | ||
*/ | ||
return function (Builder $joinQuery, Closure $callback): void { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
$joins = $joinQuery->getQuery()->joins ?: []; | ||
Check failure on line 189 in src/Mixins/JoinsRelationships.php GitHub Actions / Static Analysis (8.2)
Check failure on line 189 in src/Mixins/JoinsRelationships.php GitHub Actions / Static Analysis (8.3)
Check failure on line 189 in src/Mixins/JoinsRelationships.php GitHub Actions / Static Analysis (8.4)
|
||
|
||
array_unshift($joins, $joinQuery); | ||
|
@@ -228,40 +226,6 @@ public function callJoinScope(): Closure | |
}; | ||
} | ||
|
||
/** | ||
* Defines the mixin for {@see $query->getJoinType()}. | ||
*/ | ||
public function getJoinType(): Closure | ||
{ | ||
/** | ||
* Returns the custom provided join type. | ||
*/ | ||
return function (): ?string { | ||
/** @var Builder $this */ | ||
if (! property_exists($this, 'type')) { | ||
return null; | ||
} | ||
|
||
// There's a weird quirk in PHP where if a dynamic property was added | ||
// to a class, and the class has the magic "__get" method defined, | ||
// accessing the property yields a value, but also throws too. | ||
|
||
try { | ||
$type = $this->type; | ||
} | ||
|
||
// There's a bug with code coverage that for whatever reason does not | ||
// consider the "finally" line to be covered, but its contents are. | ||
// While it looks weird, we are going to ignore coverage there. | ||
|
||
// @codeCoverageIgnoreStart | ||
finally { | ||
// @codeCoverageIgnoreEnd | ||
return $type; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Defines the mixin for {@see $query->addJoinRelationWhere()}. | ||
*/ | ||
|
@@ -271,7 +235,7 @@ public function addJoinRelationWhere(): Closure | |
* Add the "join relation" condition where clause to the query. | ||
*/ | ||
return function (Builder $joinQuery, Relation $relation, string $type): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
$joinQuery->mergeConstraintsFrom($relation->getQuery()); | ||
Check failure on line 239 in src/Mixins/JoinsRelationships.php GitHub Actions / Static Analysis (8.2)
Check failure on line 239 in src/Mixins/JoinsRelationships.php GitHub Actions / Static Analysis (8.3)
Check failure on line 239 in src/Mixins/JoinsRelationships.php GitHub Actions / Static Analysis (8.4)
|
||
|
||
$baseJoinQuery = $joinQuery->toBase(); | ||
|
@@ -305,7 +269,7 @@ public function getBelongsToJoinRelation(): Closure | |
* Returns the belongs to relation for the next morph. | ||
*/ | ||
return function (MorphTo $relation, MorphTypes $morphTypes, Builder $relatedQuery): BelongsTo { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
|
||
// When it comes to joining across morph types, we can really only support | ||
// a single type. However, when we're provided multiple types, we will | ||
|
@@ -363,7 +327,7 @@ public function leftJoinRelation(): Closure | |
* @param Closure|array<string,Closure>|null $callback | ||
*/ | ||
return function (string $relation, Closure|array|null $callback = null, bool $through = false): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
return $this->joinRelation($relation, $callback, 'left', $through); | ||
}; | ||
} | ||
|
@@ -379,7 +343,7 @@ public function rightJoinRelation(): Closure | |
* @param Closure|array<string,Closure>|null $callback | ||
*/ | ||
return function (string $relation, Closure|array|null $callback = null, bool $through = false): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
return $this->joinRelation($relation, $callback, 'right', $through); | ||
}; | ||
} | ||
|
@@ -395,7 +359,7 @@ public function crossJoinRelation(): Closure | |
* @param Closure|array<string,Closure>|null $callback | ||
*/ | ||
return function (string $relation, Closure|array|null $callback = null, bool $through = false): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
return $this->joinRelation($relation, $callback, 'cross', $through); | ||
}; | ||
} | ||
|
@@ -411,7 +375,7 @@ public function joinThroughRelation(): Closure | |
* @param Closure|array<string,Closure>|null $callback | ||
*/ | ||
return function (string $relation, Closure|array|null $callback = null, string $type = 'inner'): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
return $this->joinRelation($relation, $callback, $type, true); | ||
}; | ||
} | ||
|
@@ -427,7 +391,7 @@ public function leftJoinThroughRelation(): Closure | |
* @param Closure|array<string,Closure>|null $callback | ||
*/ | ||
return function (string $relation, Closure|array|null $callback = null): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
return $this->joinRelation($relation, $callback, 'left', true); | ||
}; | ||
} | ||
|
@@ -443,7 +407,7 @@ public function rightJoinThroughRelation(): Closure | |
* @param Closure|array<string,Closure>|null $callback | ||
*/ | ||
return function (string $relation, Closure|array|null $callback = null): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
return $this->joinRelation($relation, $callback, 'right', true); | ||
}; | ||
} | ||
|
@@ -459,7 +423,7 @@ public function crossJoinThroughRelation(): Closure | |
* @param Closure|array<string,Closure>|null $callback | ||
*/ | ||
return function (string $relation, Closure|array|null $callback = null): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
return $this->joinRelation($relation, $callback, 'cross', true); | ||
}; | ||
} | ||
|
@@ -484,7 +448,7 @@ public function joinMorphRelation(): Closure | |
bool $through = false, | ||
?Builder $relatedQuery = null | ||
): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
return $this->joinRelation($relation, $callback, $type, $through, $relatedQuery, $morphTypes); | ||
}; | ||
} | ||
|
@@ -507,7 +471,7 @@ public function leftJoinMorphRelation(): Closure | |
Closure|array|null $callback = null, | ||
bool $through = false | ||
): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
return $this->joinRelation($relation, $callback, 'left', $through, null, $morphTypes); | ||
}; | ||
} | ||
|
@@ -530,7 +494,7 @@ public function rightJoinMorphRelation(): Closure | |
Closure|array|null $callback = null, | ||
bool $through = false | ||
): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
return $this->joinRelation($relation, $callback, 'right', $through, null, $morphTypes); | ||
}; | ||
} | ||
|
@@ -553,7 +517,7 @@ public function crossJoinMorphRelation(): Closure | |
Closure|array|null $callback = null, | ||
bool $through = false | ||
): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
return $this->joinRelation($relation, $callback, 'cross', $through, null, $morphTypes); | ||
}; | ||
} | ||
|
@@ -576,7 +540,7 @@ public function joinThroughMorphRelation(): Closure | |
Closure|array|null $callback = null, | ||
string $type = 'inner' | ||
): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
return $this->joinRelation($relation, $callback, $type, true, null, $morphTypes); | ||
}; | ||
} | ||
|
@@ -598,7 +562,7 @@ public function leftJoinThroughMorphRelation(): Closure | |
array|string $morphTypes = ['*'], | ||
Closure|array|null $callback = null | ||
): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
return $this->joinRelation($relation, $callback, 'left', true, null, $morphTypes); | ||
}; | ||
} | ||
|
@@ -620,7 +584,7 @@ public function rightJoinThroughMorphRelation(): Closure | |
array|string $morphTypes = ['*'], | ||
Closure|array|null $callback = null | ||
): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
return $this->joinRelation($relation, $callback, 'right', true, null, $morphTypes); | ||
}; | ||
} | ||
|
@@ -642,7 +606,7 @@ public function crossJoinThroughMorphRelation(): Closure | |
array|string $morphTypes = ['*'], | ||
Closure|array|null $callback = null | ||
): Builder { | ||
/** @var Builder $this */ | ||
/** @var Builder<TModel> $this */ | ||
return $this->joinRelation($relation, $callback, 'cross', true, null, $morphTypes); | ||
}; | ||
} | ||
|