From dcf545e2e3d6d22a70fced44bd8f728fa96e94bd Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Sun, 6 Oct 2024 20:13:32 +0200 Subject: [PATCH] Add type checks --- .gitattributes | 2 ++ .github/CONTRIBUTING.md | 1 + .github/workflows/static-analysis.yml | 4 +++ composer.json | 2 +- phpstan.types.neon.dist | 6 ++++ types/Models/Comment.php | 10 ++++++ types/Models/Country.php | 11 ++++++ types/Models/Post.php | 10 ++++++ types/Models/User.php | 10 ++++++ types/Relationships.php | 49 +++++++++++++++++++++++++++ 10 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 phpstan.types.neon.dist create mode 100644 types/Models/Comment.php create mode 100644 types/Models/Country.php create mode 100644 types/Models/Post.php create mode 100644 types/Models/User.php create mode 100644 types/Relationships.php diff --git a/.gitattributes b/.gitattributes index c0351e5..caaad67 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,9 +1,11 @@ /.docker export-ignore /.github export-ignore /tests export-ignore +/types export-ignore .gitattributes export-ignore .gitignore export-ignore docker-compose.ci.yml export-ignore docker-compose.yml export-ignore phpstan.neon.dist export-ignore +phpstan.types.neon.dist export-ignore phpunit.xml.dist export-ignore diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 82c239e..bf559ec 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -26,4 +26,5 @@ We accept contributions via Pull Requests on [GitHub](https://github.com/stauden docker compose run --rm php8.3 composer install docker compose run --rm php8.3 vendor/bin/phpunit docker compose run --rm php8.3 vendor/bin/phpstan analyse --memory-limit=-1 +docker compose run --rm php8.3 vendor/bin/phpstan analyse --configuration=phpstan.types.neon.dist --memory-limit=-1 ``` diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index f7110d1..2e0e258 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -31,3 +31,7 @@ jobs: run: | docker compose -f docker-compose.yml -f docker-compose.ci.yml run --rm php${{ matrix.php }} \ vendor/bin/phpstan analyse --error-format=github --memory-limit=-1 + - name: Analyse types + run: | + docker compose -f docker-compose.yml -f docker-compose.ci.yml run --rm php${{ matrix.php }} \ + vendor/bin/phpstan analyse --configuration=phpstan.types.neon.dist --error-format=github --memory-limit=-1 diff --git a/composer.json b/composer.json index e119823..f1cdafa 100644 --- a/composer.json +++ b/composer.json @@ -18,9 +18,9 @@ "barryvdh/laravel-ide-helper": "^3.0", "illuminate/pagination": "^11.0", "korridor/laravel-has-many-merged": "^1.1", + "larastan/larastan": "^2.9", "mockery/mockery": "^1.6", "orchestra/testbench": "^9.0", - "phpstan/phpstan": "^1.12", "phpunit/phpunit": "^11.0", "staudenmeir/eloquent-json-relations": "^1.11", "staudenmeir/laravel-adjacency-list": "^1.21" diff --git a/phpstan.types.neon.dist b/phpstan.types.neon.dist new file mode 100644 index 0000000..188838b --- /dev/null +++ b/phpstan.types.neon.dist @@ -0,0 +1,6 @@ +includes: + - ./vendor/larastan/larastan/extension.neon +parameters: + level: 9 + paths: + - types diff --git a/types/Models/Comment.php b/types/Models/Comment.php new file mode 100644 index 0000000..579b54c --- /dev/null +++ b/types/Models/Comment.php @@ -0,0 +1,10 @@ +', + $country->hasManyDeep(Comment::class, [User::class, Post::class]) + ); + + assertType( + 'Staudenmeir\EloquentHasManyDeep\HasOneDeep', + $country->hasOneDeep(Comment::class, [User::class, Post::class]) + ); + + assertType( + 'Staudenmeir\EloquentHasManyDeep\HasManyDeep', + $country->hasManyDeepFromRelations( + (new Country())->hasManyThrough(Post::class, User::class), + (new Post())->hasMany(Comment::class) + ) + ); + + assertType( + 'Staudenmeir\EloquentHasManyDeep\HasOneDeep', + $country->hasOneDeepFromRelations( + (new Country())->hasManyThrough(Post::class, User::class), + (new Post())->hasMany(Comment::class) + ) + ); + + assertType( + 'Staudenmeir\EloquentHasManyDeep\HasManyDeep', + $country->hasManyDeepFromReverse((new Country())->hasManyDeep(Comment::class, [User::class, Post::class])) + ); + + assertType( + 'Staudenmeir\EloquentHasManyDeep\HasOneDeep', + $country->hasOneDeepFromReverse((new Country())->hasManyDeep(Comment::class, [User::class, Post::class])) + ); +}