Skip to content

Commit

Permalink
~ Reached PHPStan level 7
Browse files Browse the repository at this point in the history
  • Loading branch information
tylernathanreed committed Mar 20, 2024
1 parent 602276e commit 131a2cc
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
3 changes: 2 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
parameters:
level: 6
level: 7
paths:
- src
excludePaths:
- tests/*
stubFiles:
- stubs/BelongsToMany.stub
- stubs/EloquentBuilder.stub
- stubs/QueryBuilder.stub
- stubs/Relation.stub
Expand Down
13 changes: 7 additions & 6 deletions src/Mixins/JoinsRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\Relation;
Expand All @@ -27,28 +28,28 @@ public function joinRelation(): Closure
*
* @param Relation|string|array<Relation|string> $relation
* @param Closure|array<string,Closure>|null $callback
* @param MorphTypes|array<string>|string $morphTypes
* @param MorphTypes|array<class-string<Model>>|class-string<Model>|true $morphTypes
*/
return function (
Relation|string|array $relation,
Closure|array|null $callback = null,
string $type = 'inner',
bool $through = false,
?Builder $relatedQuery = null,
MorphTypes|array|string $morphTypes = ['*']
MorphTypes|array|string|true $morphTypes = true
): Builder {
/** @var Builder $this */
if (! $morphTypes instanceof MorphTypes) {
$morphTypes = new MorphTypes($morphTypes);
$morphTypes = new MorphTypes($morphTypes); // @phpstan-ignore-line
}

if (is_string($relation)) {
if (strpos($relation, '.') !== false) {
return $this->joinNestedRelation($relation, $callback, $type, $through, $morphTypes);
}

if (stripos($relation, ' as ') !== false) {
[$relationName, $alias] = preg_split('/\s+as\s+/i', $relation);
if (! empty($parts = preg_split('/\s+as\s+/i', $relation))) {
[$relationName, $alias] = $parts;
} else {
$relationName = $relation;
}
Expand Down Expand Up @@ -310,7 +311,7 @@ public function getBelongsToJoinRelation(): Closure
// a single type. However, when we're provided multiple types, we will
// instead use these one at a time and pass the information along.

if ($morphTypes->items === ['*']) {
if ($morphTypes->all) {
$types = $relatedQuery->model
->newQuery()
->distinct()
Expand Down
18 changes: 13 additions & 5 deletions src/MorphTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@

namespace Reedware\LaravelRelationJoins;

use Illuminate\Database\Eloquent\Model;

class MorphTypes
{
/**
* The underlying morph types.
*
* @var array<string>
* @var array<class-string<Model>>
*/
public array $items;
public array $items = [];

public bool $all = false;

/**
* Creates a new morph types instance.
*
* @param string|array<string> $items
* @param class-string<Model>|array<class-string<Model>>|true $items
*/
public function __construct($items)
public function __construct(array|string|true $items)
{
$this->items = (array) $items;
if ($items === true) {
$this->all = true;
} else {
$this->items = (array) $items;
}
}
}
2 changes: 1 addition & 1 deletion src/RelationJoinQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected static function hasOneOrMany(
* @see https://github.com/laravel/framework/commit/de4c42f04d609b119a4e0a7e6223c37bfe54cb87
*/
protected static function hasOneOrManyThrough(
HasOne|HasManyThrough $relation,
HasOneThrough|HasManyThrough $relation,
Builder $query,
Builder $parentQuery,
string $type = 'inner',
Expand Down
12 changes: 12 additions & 0 deletions stubs/BelongsToMany.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Illuminate\Database\Eloquent\Relations;

use Illuminate\Database\Eloquent\Model;

/**
* @method class-string<Model> getPivotClass()
*/
class BelongsToMany
{
}
2 changes: 2 additions & 0 deletions stubs/EloquentBuilder.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Database\Eloquent;

use Closure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\Relation;
Expand Down Expand Up @@ -31,6 +32,7 @@ use Reedware\LaravelRelationJoins\MorphTypes;
* @method Builder|static leftJoinThroughMorphRelation($relation, $morphTypes = ['*'], Closure $callback = null)
* @method Builder|static rightJoinThroughMorphRelation($relation, $morphTypes = ['*'], Closure $callback = null)
* @method Builder|static crossJoinThroughMorphRelation($relation, $morphTypes = ['*'], Closure $callback = null)
* @method Model getModel()
*
* @mixin \Illuminate\Database\Query\Builder
*/
Expand Down

0 comments on commit 131a2cc

Please sign in to comment.