Skip to content

Commit

Permalink
+ Added generics
Browse files Browse the repository at this point in the history
  • Loading branch information
tylernathanreed authored Dec 13, 2024
1 parent 6d2e91e commit 358eaa1
Showing 1 changed file with 23 additions and 59 deletions.
82 changes: 23 additions & 59 deletions src/Mixins/JoinsRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Reedware\LaravelRelationJoins\MorphTypes;
use RuntimeException;

/** @mixin Builder */
/** @template TModel of Model */
class JoinsRelationships
{
/**
Expand All @@ -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

View workflow job for this annotation

GitHub Actions / Static Analysis (8.2)

PHPDoc tag @var for variable $this contains generic type Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model> but class Illuminate\Database\Eloquent\Builder is not generic.

Check failure on line 42 in src/Mixins/JoinsRelationships.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.3)

PHPDoc tag @var for variable $this contains generic type Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model> but class Illuminate\Database\Eloquent\Builder is not generic.

Check failure on line 42 in src/Mixins/JoinsRelationships.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.4)

PHPDoc tag @var for variable $this contains generic type Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model> but class Illuminate\Database\Eloquent\Builder is not generic.
$morphTypes = new MorphTypes($morphTypes); // @phpstan-ignore-line
}
Expand Down Expand Up @@ -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;
Expand All @@ -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

View workflow job for this annotation

GitHub Actions / Static Analysis (8.2)

PHPDoc tag @var for variable $this contains generic type Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model> but class Illuminate\Database\Eloquent\Builder is not generic.

Check failure on line 126 in src/Mixins/JoinsRelationships.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.3)

PHPDoc tag @var for variable $this contains generic type Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model> but class Illuminate\Database\Eloquent\Builder is not generic.

Check failure on line 126 in src/Mixins/JoinsRelationships.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.4)

PHPDoc tag @var for variable $this contains generic type Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model> but class Illuminate\Database\Eloquent\Builder is not generic.

$relatedQuery = $this;
Expand Down Expand Up @@ -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

View workflow job for this annotation

GitHub Actions / Static Analysis (8.2)

PHPDoc tag @var for variable $this contains generic type Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model> but class Illuminate\Database\Eloquent\Builder is not generic.

Check failure on line 167 in src/Mixins/JoinsRelationships.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.3)

PHPDoc tag @var for variable $this contains generic type Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model> but class Illuminate\Database\Eloquent\Builder is not generic.

Check failure on line 167 in src/Mixins/JoinsRelationships.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.4)

PHPDoc tag @var for variable $this contains generic type Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model> but class Illuminate\Database\Eloquent\Builder is not generic.

foreach ($joins as $join) {
Expand All @@ -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

View workflow job for this annotation

GitHub Actions / Static Analysis (8.2)

PHPDoc tag @var for variable $this contains generic type Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model> but class Illuminate\Database\Eloquent\Builder is not generic.

Check failure on line 189 in src/Mixins/JoinsRelationships.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.3)

PHPDoc tag @var for variable $this contains generic type Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model> but class Illuminate\Database\Eloquent\Builder is not generic.

Check failure on line 189 in src/Mixins/JoinsRelationships.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.4)

PHPDoc tag @var for variable $this contains generic type Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model> but class Illuminate\Database\Eloquent\Builder is not generic.

array_unshift($joins, $joinQuery);
Expand Down Expand Up @@ -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()}.
*/
Expand All @@ -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

View workflow job for this annotation

GitHub Actions / Static Analysis (8.2)

PHPDoc tag @var for variable $this contains generic type Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model> but class Illuminate\Database\Eloquent\Builder is not generic.

Check failure on line 239 in src/Mixins/JoinsRelationships.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.3)

PHPDoc tag @var for variable $this contains generic type Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model> but class Illuminate\Database\Eloquent\Builder is not generic.

Check failure on line 239 in src/Mixins/JoinsRelationships.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.4)

PHPDoc tag @var for variable $this contains generic type Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model> but class Illuminate\Database\Eloquent\Builder is not generic.

$baseJoinQuery = $joinQuery->toBase();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
};
}
Expand All @@ -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);
};
}
Expand All @@ -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);
};
}
Expand All @@ -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);
};
}
Expand All @@ -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);
};
}
Expand All @@ -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);
};
}
Expand All @@ -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);
};
}
Expand All @@ -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);
};
}
Expand All @@ -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);
};
}
Expand All @@ -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);
};
}
Expand All @@ -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);
};
}
Expand All @@ -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);
};
}
Expand All @@ -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);
};
}
Expand All @@ -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);
};
}
Expand All @@ -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);
};
}
Expand Down

0 comments on commit 358eaa1

Please sign in to comment.