From 358eaa131799e094e9d593d267dc6cddc675c669 Mon Sep 17 00:00:00 2001 From: Tyler Date: Fri, 13 Dec 2024 14:51:40 -0600 Subject: [PATCH] + Added generics --- src/Mixins/JoinsRelationships.php | 82 +++++++++---------------------- 1 file changed, 23 insertions(+), 59 deletions(-) diff --git a/src/Mixins/JoinsRelationships.php b/src/Mixins/JoinsRelationships.php index a722507..19065b3 100644 --- a/src/Mixins/JoinsRelationships.php +++ b/src/Mixins/JoinsRelationships.php @@ -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 $this */ if (! $morphTypes instanceof MorphTypes) { $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 $this */ $relations = explode('.', $relations); $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 $this */ $joins = $joinQuery->getQuery()->joins ?: []; 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 $this */ $joins = $joinQuery->getQuery()->joins ?: []; 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 $this */ $joinQuery->mergeConstraintsFrom($relation->getQuery()); $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 $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|null $callback */ return function (string $relation, Closure|array|null $callback = null, bool $through = false): Builder { - /** @var Builder $this */ + /** @var Builder $this */ return $this->joinRelation($relation, $callback, 'left', $through); }; } @@ -379,7 +343,7 @@ public function rightJoinRelation(): Closure * @param Closure|array|null $callback */ return function (string $relation, Closure|array|null $callback = null, bool $through = false): Builder { - /** @var Builder $this */ + /** @var Builder $this */ return $this->joinRelation($relation, $callback, 'right', $through); }; } @@ -395,7 +359,7 @@ public function crossJoinRelation(): Closure * @param Closure|array|null $callback */ return function (string $relation, Closure|array|null $callback = null, bool $through = false): Builder { - /** @var Builder $this */ + /** @var Builder $this */ return $this->joinRelation($relation, $callback, 'cross', $through); }; } @@ -411,7 +375,7 @@ public function joinThroughRelation(): Closure * @param Closure|array|null $callback */ return function (string $relation, Closure|array|null $callback = null, string $type = 'inner'): Builder { - /** @var Builder $this */ + /** @var Builder $this */ return $this->joinRelation($relation, $callback, $type, true); }; } @@ -427,7 +391,7 @@ public function leftJoinThroughRelation(): Closure * @param Closure|array|null $callback */ return function (string $relation, Closure|array|null $callback = null): Builder { - /** @var Builder $this */ + /** @var Builder $this */ return $this->joinRelation($relation, $callback, 'left', true); }; } @@ -443,7 +407,7 @@ public function rightJoinThroughRelation(): Closure * @param Closure|array|null $callback */ return function (string $relation, Closure|array|null $callback = null): Builder { - /** @var Builder $this */ + /** @var Builder $this */ return $this->joinRelation($relation, $callback, 'right', true); }; } @@ -459,7 +423,7 @@ public function crossJoinThroughRelation(): Closure * @param Closure|array|null $callback */ return function (string $relation, Closure|array|null $callback = null): Builder { - /** @var Builder $this */ + /** @var Builder $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 $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 $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 $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 $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 $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 $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 $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 $this */ return $this->joinRelation($relation, $callback, 'cross', true, null, $morphTypes); }; }