Skip to content

Commit

Permalink
~ Reached PHPStan level 8
Browse files Browse the repository at this point in the history
  • Loading branch information
tylernathanreed committed Mar 20, 2024
1 parent 131a2cc commit c86eeaa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
"test:static": "phpstan",
"test:style": "pint --test",
"test:style-fix": "pint",
"test:suite": "phpunit"
"test:suite": "phpunit",
"test": [
"@test:style",
"@test:static",
"@test:suite",
"@test:coverage"
]
}
}
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 7
level: 8
paths:
- src
excludePaths:
Expand Down
10 changes: 7 additions & 3 deletions src/Mixins/JoinsRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function joinRelation(): Closure
return $this->joinNestedRelation($relation, $callback, $type, $through, $morphTypes);
}

if (! empty($parts = preg_split('/\s+as\s+/i', $relation))) {
if (($parts = preg_split('/\s+as\s+/i', $relation)) && count($parts) >= 2) {
[$relationName, $alias] = $parts;
} else {
$relationName = $relation;
Expand Down Expand Up @@ -334,6 +334,10 @@ public function getBelongsToJoinRelation(): Closure
// that has the type itself constrained. This allows us to join into a
// singular table, which bypasses the typical headache of morphs.

if (count($morphTypes->items) == 0) {
throw new RuntimeException('joinMorphRelation() requires at least one morph type.');
}

$morphType = array_shift($morphTypes->items);

$belongsTo = $relatedQuery->getBelongsToRelation($relation, $morphType);
Expand Down Expand Up @@ -469,12 +473,12 @@ public function joinMorphRelation(): Closure
* Add a morph to relationship join condition to the query.
*
* @param string|array<Relation|string> $relation
* @param array<string>|string $morphTypes
* @param array<class-string<Model>>|class-string<Model>|true $morphTypes
* @param Closure|array<Closure>|null $callback
*/
return function (
string|array $relation,
array|string $morphTypes = ['*'],
array|string|true $morphTypes = true,
Closure|array|null $callback = null,
string $type = 'inner',
bool $through = false,
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/JoinsRelationshipsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Database\Eloquent\Relations\Relation;
use Reedware\LaravelRelationJoins\Tests\Models\EloquentCountryModelStub;
use Reedware\LaravelRelationJoins\Tests\Models\EloquentPolymorphicCommentModelStub;
use Reedware\LaravelRelationJoins\Tests\Models\EloquentUserModelStub;

class JoinsRelationshipsTest extends TestCase
Expand Down Expand Up @@ -276,4 +277,16 @@ public function multiconstraint_mix_type(Closure $query, string $builderClass)
$this->assertEquals('select * from "users" left join "posts" on "posts"."user_id" = "users"."id" inner join "comments" on "comments"."post_id" = "posts"."id" right join "likes" on "likes"."comment_id" = "comments"."id"', $builder->toSql());
$this->assertEquals($builderClass, get_class($builder));
}

/**
* @test
*
* @dataProvider queryDataProvider
*/
public function throwsOnEmptyMorphTypesArray(Closure $query, string $builderClass)
{
$this->expectExceptionMessage('joinMorphRelation() requires at least one morph type.');

$query(new EloquentPolymorphicCommentModelStub)->joinMorphRelation('commentable', []);
}
}

0 comments on commit c86eeaa

Please sign in to comment.