From 4813f9beb9ba844bba39cf9e6f79fc931433d0eb Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Thu, 29 Aug 2024 19:01:47 +0200 Subject: [PATCH] Increase PHPStan level --- composer.json | 1 + phpstan.neon.dist | 5 ++++- .../Relations/Traits/RetrievesIntermediateTables.php | 5 ++++- src/HasManyDeep.php | 1 + src/HasOneDeep.php | 3 ++- .../Concatenation/EloquentJsonRelations/Models/Project.php | 3 +++ tests/Concatenation/EloquentJsonRelations/Models/Role.php | 3 +++ tests/Concatenation/LaravelAdjacencyList/Models/User.php | 4 ++++ tests/HasManyDeepTest.php | 2 ++ tests/Models/Comment.php | 6 ++++++ tests/Models/Country.php | 5 +++++ tests/Models/Permission.php | 4 ++++ tests/Models/Post.php | 3 +++ 13 files changed, 42 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index ecf2141..b4f7625 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "mockery/mockery": "^1.6", "orchestra/testbench": "^9.0", "phpstan/phpstan": "^1.10", + "phpstan/phpstan-mockery": "^1.1", "phpunit/phpunit": "^11.0", "staudenmeir/eloquent-json-relations": "^1.11", "staudenmeir/laravel-adjacency-list": "^1.21" diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 2b27b27..6066a1f 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,9 +1,12 @@ includes: - ./vendor/larastan/larastan/extension.neon + - ./vendor/phpstan/phpstan-mockery/extension.neon parameters: - level: 1 + level: 2 paths: - src - tests ignoreErrors: + - '#Call to an undefined method .+::getQualifiedDeletedAtColumn\(\)#' + - '#Call to an undefined method .+::withTrashed\(\)#' - '#Unsafe usage of new static\(\).#' diff --git a/src/Eloquent/Relations/Traits/RetrievesIntermediateTables.php b/src/Eloquent/Relations/Traits/RetrievesIntermediateTables.php index 12f046e..5ee5256 100644 --- a/src/Eloquent/Relations/Traits/RetrievesIntermediateTables.php +++ b/src/Eloquent/Relations/Traits/RetrievesIntermediateTables.php @@ -51,7 +51,10 @@ public function withPivot( ?callable $postProcessor = null ) { if ($columns === ['*']) { - $columns = $this->query->getConnection()->getSchemaBuilder()->getColumnListing($table); + /** @var \Illuminate\Database\Connection $connection */ + $connection = $this->query->getConnection(); + + $columns = $connection->getSchemaBuilder()->getColumnListing($table); } $accessor = $accessor ?: $table; diff --git a/src/HasManyDeep.php b/src/HasManyDeep.php index 67ba181..f4f648d 100644 --- a/src/HasManyDeep.php +++ b/src/HasManyDeep.php @@ -18,6 +18,7 @@ /** * @template TRelatedModel of \Illuminate\Database\Eloquent\Model + * * @extends \Illuminate\Database\Eloquent\Relations\HasManyThrough */ class HasManyDeep extends HasManyThrough implements ConcatenableRelation diff --git a/src/HasOneDeep.php b/src/HasOneDeep.php index be743af..25ef901 100644 --- a/src/HasOneDeep.php +++ b/src/HasOneDeep.php @@ -8,7 +8,8 @@ /** * @template TRelatedModel of \Illuminate\Database\Eloquent\Model - * @extends \Illuminate\Database\Eloquent\Relations\HasOneThrough + * + * @extends \Staudenmeir\EloquentHasManyDeep\HasManyDeep */ class HasOneDeep extends HasManyDeep { diff --git a/tests/Concatenation/EloquentJsonRelations/Models/Project.php b/tests/Concatenation/EloquentJsonRelations/Models/Project.php index 6f90ec5..dddf379 100644 --- a/tests/Concatenation/EloquentJsonRelations/Models/Project.php +++ b/tests/Concatenation/EloquentJsonRelations/Models/Project.php @@ -6,6 +6,9 @@ use Staudenmeir\EloquentHasManyDeep\HasRelationships; use Staudenmeir\EloquentJsonRelations\JsonKey; +/** + * @property-read \Illuminate\Database\Eloquent\Relations\Pivot $pivot + */ class Project extends Model { use HasRelationships; diff --git a/tests/Concatenation/EloquentJsonRelations/Models/Role.php b/tests/Concatenation/EloquentJsonRelations/Models/Role.php index b833d42..d4503e1 100644 --- a/tests/Concatenation/EloquentJsonRelations/Models/Role.php +++ b/tests/Concatenation/EloquentJsonRelations/Models/Role.php @@ -7,6 +7,9 @@ use Staudenmeir\EloquentHasManyDeep\HasRelationships; use Staudenmeir\EloquentJsonRelations\JsonKey; +/** + * @property-read \Illuminate\Database\Eloquent\Relations\Pivot $pivot + */ class Role extends Model { use HasRelationships; diff --git a/tests/Concatenation/LaravelAdjacencyList/Models/User.php b/tests/Concatenation/LaravelAdjacencyList/Models/User.php index 0538086..0f7bc98 100644 --- a/tests/Concatenation/LaravelAdjacencyList/Models/User.php +++ b/tests/Concatenation/LaravelAdjacencyList/Models/User.php @@ -10,6 +10,10 @@ use Staudenmeir\EloquentHasManyDeep\HasTableAlias; use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships; +/** + * @property-read \Tests\Concatenation\LaravelAdjacencyList\Models\Post $ancestorPost + * @property-read \Tests\Concatenation\LaravelAdjacencyList\Models\Post $descendantPost + */ class User extends Model { use HasRelationships; diff --git a/tests/HasManyDeepTest.php b/tests/HasManyDeepTest.php index 19d7203..92ee847 100644 --- a/tests/HasManyDeepTest.php +++ b/tests/HasManyDeepTest.php @@ -161,6 +161,7 @@ public function testExistenceQueryForThroughSelfRelationWithoutAliasTrait() public function testWithTrashed() { + /** @var \Tests\Models\User $user */ $user = Comment::find(33)->user() ->withTrashed() ->first(); @@ -181,6 +182,7 @@ public function testWithTrashedIntermediateAndWithCount() { $country = Country::withCount('commentsWithTrashedUsers as count')->first(); + // @phpstan-ignore property.notFound $this->assertEquals(3, $country->count); } } diff --git a/tests/Models/Comment.php b/tests/Models/Comment.php index 42e7c14..254c533 100644 --- a/tests/Models/Comment.php +++ b/tests/Models/Comment.php @@ -8,6 +8,12 @@ use Staudenmeir\EloquentHasManyDeep\HasOneDeep; use Staudenmeir\EloquentHasManyDeep\HasTableAlias; +/** + * @property-read \Tests\Models\Country|null $country + * @property-read \Tests\Models\Country|null $countryWithCustomThroughTable + * @property-read \Tests\Models\Post|null $post + * @property-read \Tests\Models\Post|null $rootPost + */ class Comment extends Model { use HasTableAlias; diff --git a/tests/Models/Country.php b/tests/Models/Country.php index 56fc490..5bb3aba 100644 --- a/tests/Models/Country.php +++ b/tests/Models/Country.php @@ -7,6 +7,11 @@ use Staudenmeir\EloquentHasManyDeep\HasManyDeep; use Staudenmeir\EloquentHasManyDeep\HasOneDeep; +/** + * @property-read \Tests\Models\Comment|null $comment + * @property-read \Tests\Models\Comment|null $commentFromRelations + * @property-read \Tests\Models\Comment|null $commentFromRelationsWithConstraints + */ class Country extends Model { public function comment(): HasOneDeep diff --git a/tests/Models/Permission.php b/tests/Models/Permission.php index 3cdd7a9..b8b9bad 100644 --- a/tests/Models/Permission.php +++ b/tests/Models/Permission.php @@ -5,6 +5,10 @@ use Staudenmeir\EloquentHasManyDeep\HasManyDeep; use Staudenmeir\EloquentHasManyDeep\HasRelationships; +/** + * @property-read \Illuminate\Database\Eloquent\Relations\Pivot $pivot + * @property-read \Illuminate\Database\Eloquent\Relations\Pivot $role_user + */ class Permission extends Model { use HasRelationships; diff --git a/tests/Models/Post.php b/tests/Models/Post.php index c7002d9..715a9e9 100644 --- a/tests/Models/Post.php +++ b/tests/Models/Post.php @@ -7,6 +7,9 @@ use Illuminate\Database\Eloquent\Relations\MorphToMany; use Staudenmeir\EloquentHasManyDeep\HasManyDeep; +/** + * @property-read \Tests\Models\User|null $user + */ class Post extends Model { public function comments(): HasMany