diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml new file mode 100644 index 0000000..7601f21 --- /dev/null +++ b/.github/workflows/static-analysis.yml @@ -0,0 +1,43 @@ +name: Static Analysis + +on: [ push ] + +jobs: + PHPUnit: + strategy: + matrix: + include: + # Laravel 10.* + - php: 8.1 + laravel: 10.* + composer-flag: '--prefer-stable' + - php: 8.2 + laravel: 10.* + composer-flag: '--prefer-stable' + - php: 8.1 + laravel: 10.* + composer-flag: '--prefer-lowest' + - php: 8.2 + laravel: 10.* + composer-flag: '--prefer-lowest' + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: curl, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, iconv + coverage: none + + - name: Install dependencies + run: composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update + + - name: Update dependencies + run: composer update ${{ matrix.composer-flag }} --prefer-dist --no-interaction + + - name: Run PhpStan + run: composer analyse diff --git a/composer.json b/composer.json index f0ed515..5fcf8cd 100644 --- a/composer.json +++ b/composer.json @@ -16,8 +16,10 @@ "illuminate/database": "^10" }, "require-dev": { - "phpunit/phpunit": "^10", "friendsofphp/php-cs-fixer": "^3", + "nunomaduro/larastan": "^2.0", + "orchestra/testbench": "^8.9", + "phpunit/phpunit": "^10", "squizlabs/php_codesniffer": "^3.5" }, "autoload": { @@ -31,10 +33,13 @@ } }, "scripts": { - "test": "vendor/bin/phpunit", - "test-coverage": "vendor/bin/phpunit --coverage-html coverage", - "fix": "./vendor/bin/php-cs-fixer fix", - "lint": "./vendor/bin/phpcs --error-severity=1 --warning-severity=8 --extensions=php" + "test": "@php vendor/bin/phpunit", + "test-coverage": "@php vendor/bin/phpunit --coverage-html coverage", + "fix": "@php ./vendor/bin/php-cs-fixer fix", + "lint": "@php ./vendor/bin/phpcs --error-severity=1 --warning-severity=8 --extensions=php", + "analyse": [ + "@php ./vendor/bin/phpstan analyse --memory-limit=2G" + ] }, "config": { "sort-packages": true diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..b81977c --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,11 @@ +includes: + - ./vendor/nunomaduro/larastan/extension.neon + +parameters: + + paths: + - src + - tests + + # Level 9 is the highest level + level: 5 diff --git a/readme.md b/readme.md index 9664cbc..ea03623 100644 --- a/readme.md +++ b/readme.md @@ -48,7 +48,7 @@ class User extends Model // ... /** - * @return HasManyMerged|Message + * @return HasManyMerged */ public function messages(): HasManyMerged { @@ -56,7 +56,7 @@ class User extends Model } /** - * @return HasMany|Message + * @return HasMany */ public function sentMessages(): HasMany { @@ -64,7 +64,7 @@ class User extends Model } /** - * @return HasMany|Message + * @return HasMany */ public function receivedMessages(): HasMany { diff --git a/src/HasManyMerged.php b/src/HasManyMerged.php index 8c7ede7..56a7c56 100644 --- a/src/HasManyMerged.php +++ b/src/HasManyMerged.php @@ -9,6 +9,10 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Relation; +/** + * @template TRelatedModel of Model + * @extends Relation + */ class HasManyMerged extends Relation { /** @@ -28,9 +32,9 @@ class HasManyMerged extends Relation /** * Create a new has one or many relationship instance. * - * @param Builder $query + * @param Builder $query * @param Model $parent - * @param array $foreignKeys + * @param string[] $foreignKeys * @param string $localKey * @return void */ @@ -241,7 +245,7 @@ public function getQualifiedForeignKeyNames(): array /** * Get the results of the relationship. * - * @return mixed + * @phpstan-return \Traversable */ public function getResults() { @@ -251,8 +255,8 @@ public function getResults() /** * Execute the query as a "select" statement. * - * @param array $columns - * @return \Illuminate\Database\Eloquent\Collection + * @param string[] $columns + * @return Collection */ public function get($columns = ['*']) { diff --git a/src/HasManyMergedRelation.php b/src/HasManyMergedRelation.php index 5355388..bb9e924 100644 --- a/src/HasManyMergedRelation.php +++ b/src/HasManyMergedRelation.php @@ -4,10 +4,12 @@ namespace Korridor\LaravelHasManyMerged; +use Illuminate\Database\Eloquent\Model; + trait HasManyMergedRelation { /** - * @param string $related + * @param class-string $related * @param string[]|null $foreignKeys * @param string|null $localKey * @return HasManyMerged diff --git a/tests/HasManyMergedTest.php b/tests/HasManyMergedTest.php index 7e2e104..dfdc41f 100644 --- a/tests/HasManyMergedTest.php +++ b/tests/HasManyMergedTest.php @@ -65,7 +65,7 @@ public function testHasManyMergedWithTwoUsersWereBothAreSenderOrReceiverOfTheSam $user2 = User::find(12); $messagesOfUser1 = $user1->messages; $messagesOfUser2 = $user2->messages; - $queries = $this->db::getQueryLog(); + $queries = $this->db::connection()->getQueryLog(); $this->db::connection()->disableQueryLog(); // Assert @@ -90,7 +90,7 @@ public function testHasManyMergedWithTwoUsersWereBothAreSenderOrReceiverOfTheSam $user2 = $users->firstWhere('id', 12); $messagesOfUser1 = $user1->messages; $messagesOfUser2 = $user2->messages; - $queries = $this->db::getQueryLog(); + $queries = $this->db::connection()->getQueryLog(); $this->db::connection()->disableQueryLog(); // Assert @@ -115,7 +115,7 @@ public function testHasManyMergedWithTwoUsersWereBothAreSenderOrReceiverOfTheSam $user2 = $users->firstWhere('id', 12); $messagesOfUser1 = $user1->messages; $messagesOfUser2 = $user2->messages; - $queries = $this->db::getQueryLog(); + $queries = $this->db::connection()->getQueryLog(); $this->db::connection()->disableQueryLog(); // Assert @@ -146,7 +146,7 @@ public function testHasManyMergedWithTwoUsersWereBothAreSenderOrReceiverOfTheSam $user2 = $users->firstWhere('id', 12); $messagesOfUser1 = $user1->messages; $messagesOfUser2 = $user2->messages; - $queries = $this->db::getQueryLog(); + $queries = $this->db::connection()->getQueryLog(); $this->db::connection()->disableQueryLog(); // Assert @@ -171,7 +171,7 @@ public function testHasManyMergedWithTwoUsersWereBothAreSenderOrReceiverOfTheSam $users = User::with(['messages'])->whereHas('messages', function (Builder $builder): void { $builder->where('content_integer', '=', 2); })->get(); - $queries = $this->db::getQueryLog(); + $queries = $this->db::connection()->getQueryLog(); $this->db::connection()->disableQueryLog(); // Assert @@ -192,7 +192,7 @@ public function testHasManyMergedWithTwoUsersWereBothAreSenderOrReceiverOfTheSam $user2 = $users->firstWhere('id', 12); $messagesOfUser1 = $user1->messages; $messagesOfUser2 = $user2->messages; - $queries = $this->db::getQueryLog(); + $queries = $this->db::connection()->getQueryLog(); $this->db::connection()->disableQueryLog(); // Assert @@ -221,7 +221,7 @@ public function testHasManyMergedWithTwoUsersWereBothAreSenderOrReceiverOfTheSam $user2 = $users->firstWhere('id', 12); $messagesOfUser1 = $user1->messages; $messagesOfUser2 = $user2->messages; - $queries = $this->db::getQueryLog(); + $queries = $this->db::connection()->getQueryLog(); $this->db::connection()->disableQueryLog(); // Assert @@ -250,7 +250,7 @@ public function testHasManyMergedWithTwoUsersWereBothAreSenderOrReceiverOfTheSam $user2 = $users->firstWhere('id', 12); $messagesOfUser1 = $user1->messages; $messagesOfUser2 = $user2->messages; - $queries = $this->db::getQueryLog(); + $queries = $this->db::connection()->getQueryLog(); $this->db::connection()->disableQueryLog(); // Assert diff --git a/tests/Models/Message.php b/tests/Models/Message.php index 30618dd..92b26d7 100644 --- a/tests/Models/Message.php +++ b/tests/Models/Message.php @@ -8,6 +8,9 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; use Korridor\LaravelHasManyMerged\HasManyMergedRelation; +/** + * @property int $id + */ class Message extends Model { use HasManyMergedRelation; @@ -26,7 +29,7 @@ class Message extends Model ]; /** - * @return BelongsTo + * @return BelongsTo */ public function sender(): BelongsTo { @@ -34,7 +37,7 @@ public function sender(): BelongsTo } /** - * @return BelongsTo + * @return BelongsTo */ public function receiver(): BelongsTo { diff --git a/tests/Models/User.php b/tests/Models/User.php index e486606..db41cb8 100644 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -9,6 +9,13 @@ use Korridor\LaravelHasManyMerged\HasManyMerged; use Korridor\LaravelHasManyMerged\HasManyMergedRelation; +/** + * @property int $id + * @property int $other_unique_id + * @property string $name + * @property int $messages_sum_content_integer + * @property int $messages_count + */ class User extends Model { use HasManyMergedRelation; diff --git a/tests/TestCase.php b/tests/TestCase.php index a2a6164..74f980b 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -10,7 +10,7 @@ abstract class TestCase extends PHPUnitTestCase { - protected $db; + protected Manager $db; protected function setUp(): void {