From 8211f50fd7f4d6a89ef28a9e2ef6159baab2b4a3 Mon Sep 17 00:00:00 2001 From: korridor <26689068+korridor@users.noreply.github.com> Date: Fri, 1 Mar 2024 15:10:25 +0100 Subject: [PATCH] Added laravel 11 and php 8.3 support --- .github/workflows/lint.yml | 4 +- .github/workflows/unittests.yml | 53 +++++++++++++------ composer.json | 20 ++++--- docker/Dockerfile | 2 +- phpstan.neon | 11 ++++ readme.md | 3 +- src/ComputedAttributes.php | 10 ++-- src/Console/GenerateComputedAttributes.php | 3 +- src/Parser/ModelAttributeParser.php | 9 +--- src/Parser/ModelAttributesEntry.php | 19 ++++--- .../GenerateComputedAttributesCommandTest.php | 3 -- .../ValidateComputedAttributesCommandTest.php | 3 -- tests/TestCase.php | 2 +- tests/TestEnvironment/Models/Post.php | 31 +++++++---- tests/TestEnvironment/Models/Vote.php | 8 ++- 15 files changed, 108 insertions(+), 73 deletions(-) create mode 100644 phpstan.neon diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index e3cff75..4975c48 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -14,7 +14,7 @@ jobs: with: command: install only_args: -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist --ignore-platform-reqs - php_version: 8.1 + php_version: 8.3 - name: Run PHP-CS-Fixer run: vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --diff --dry-run @@ -30,7 +30,7 @@ jobs: with: command: install only_args: -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist --ignore-platform-reqs - php_version: 8.1 + php_version: 8.3 - name: Run PHP CodeSniffer run: vendor/bin/phpcs --extensions=php diff --git a/.github/workflows/unittests.yml b/.github/workflows/unittests.yml index 31a503d..b80f22f 100644 --- a/.github/workflows/unittests.yml +++ b/.github/workflows/unittests.yml @@ -7,23 +7,42 @@ jobs: strategy: matrix: include: - # Laravel 10.* - - php: 8.1 - laravel: 10.* - testbench: 8.* - composer-flag: '--prefer-stable' - - php: 8.2 - laravel: 10.* - testbench: 8.* - composer-flag: '--prefer-stable' - - php: 8.1 - laravel: 10.* - testbench: 8.* - composer-flag: '--prefer-lowest' - - php: 8.2 - laravel: 10.* - testbench: 8.* - composer-flag: '--prefer-lowest' + # Laravel 10.* + - php: 8.1 + laravel: 10.* + composer-flag: '--prefer-stable' + - php: 8.2 + laravel: 10.* + composer-flag: '--prefer-stable' + - php: 8.3 + laravel: 10.* + composer-flag: '--prefer-stable' + - php: 8.1 + laravel: 10.* + composer-flag: '--prefer-lowest' + - php: 8.2 + laravel: 10.* + composer-flag: '--prefer-lowest' + - php: 8.3 + laravel: 10.* + composer-flag: '--prefer-lowest' + # Laravel 11.* + - php: 8.2 + laravel: 11.* + testbench: 9.* + composer-flag: '--prefer-stable' + - php: 8.3 + laravel: 11.* + testbench: 9.* + composer-flag: '--prefer-stable' + - php: 8.2 + laravel: 11.* + testbench: 9.* + composer-flag: '--prefer-lowest' + - php: 8.2 + laravel: 11.* + testbench: 9.* + composer-flag: '--prefer-lowest' runs-on: ubuntu-latest diff --git a/composer.json b/composer.json index 7e97311..45c5a41 100644 --- a/composer.json +++ b/composer.json @@ -13,14 +13,15 @@ "require": { "php": ">=8.1", "composer/composer": "^2", - "illuminate/console": "^10", - "illuminate/database": "^10", - "illuminate/support": "^10" + "illuminate/console": "^10|^11", + "illuminate/database": "^10|^11", + "illuminate/support": "^10|^11" }, "require-dev": { - "orchestra/testbench": "^8.0", - "phpunit/phpunit": "^10.0", "friendsofphp/php-cs-fixer": "^3", + "larastan/larastan": "^2.9", + "orchestra/testbench": "^8|^9", + "phpunit/phpunit": "^10", "squizlabs/php_codesniffer": "^3.5" }, "autoload": { @@ -37,7 +38,10 @@ "test": "vendor/bin/phpunit", "test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text --coverage-html coverage", "fix": "./vendor/bin/php-cs-fixer fix", - "lint": "./vendor/bin/phpcs --error-severity=1 --warning-severity=8 --extensions=php" + "lint": "./vendor/bin/phpcs --error-severity=1 --warning-severity=8 --extensions=php", + "analyse": [ + "@php ./vendor/bin/phpstan analyse --memory-limit=2G" + ] }, "extra": { "laravel": { @@ -48,5 +52,7 @@ }, "config": { "sort-packages": true - } + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/docker/Dockerfile b/docker/Dockerfile index a5d71e1..86e2c84 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,4 +1,4 @@ -FROM php:8.1-cli +FROM php:8.3-cli ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/ diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..4037cf0 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,11 @@ +includes: + - ./vendor/larastan/larastan/extension.neon + +parameters: + + paths: + - src + - tests + + # Level 9 is the highest level + level: 9 diff --git a/readme.md b/readme.md index 374f02c..de1ae41 100644 --- a/readme.md +++ b/readme.md @@ -36,7 +36,8 @@ php artisan vendor:publish --tag=computed-attributes-config This package is tested for the following Laravel and PHP versions: - - 10.* (PHP 8.1, 8.2) + - 10.* (PHP 8.1, 8.2, 8.3) + - 11.* (PHP 8.2, 8.3) ## Usage examples diff --git a/src/ComputedAttributes.php b/src/ComputedAttributes.php index 27ebf1d..7bb9acc 100644 --- a/src/ComputedAttributes.php +++ b/src/ComputedAttributes.php @@ -20,7 +20,7 @@ trait ComputedAttributes * @param string $attributeName * @return mixed */ - public function getComputedAttributeValue(string $attributeName) + public function getComputedAttributeValue(string $attributeName): mixed { $functionName = 'get' . Str::studly($attributeName) . 'Computed'; @@ -66,14 +66,10 @@ public function scopeComputedAttributesValidate(Builder $builder, array $attribu * Return the configuration array for this model. * If the configuration array does not exist the function will return an empty array. * - * @return array + * @return array */ public function getComputedAttributeConfiguration(): array { - if (isset($this->computed)) { - return $this->computed; - } else { - return []; - } + return $this->computed ?? []; } } diff --git a/src/Console/GenerateComputedAttributes.php b/src/Console/GenerateComputedAttributes.php index af54cf7..47d5036 100644 --- a/src/Console/GenerateComputedAttributes.php +++ b/src/Console/GenerateComputedAttributes.php @@ -5,7 +5,6 @@ namespace Korridor\LaravelComputedAttributes\Console; use Illuminate\Console\Command; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; use Korridor\LaravelComputedAttributes\ComputedAttributes; @@ -99,7 +98,7 @@ public function handle(): int // Calculate foreach ($modelAttributesEntries as $modelAttributesEntry) { $model = $modelAttributesEntry->getModel(); - /** @var Builder|ComputedAttributes $modelInstance */ + /** @var Model|ComputedAttributes $modelInstance */ $modelInstance = new $model(); $attributes = $modelAttributesEntry->getAttributes(); diff --git a/src/Parser/ModelAttributeParser.php b/src/Parser/ModelAttributeParser.php index 6f34a11..b1987fd 100644 --- a/src/Parser/ModelAttributeParser.php +++ b/src/Parser/ModelAttributeParser.php @@ -13,13 +13,6 @@ class ModelAttributeParser { - /** - * ModelAttributeParser constructor. - */ - public function __construct() - { - } - /** * @return string */ @@ -53,7 +46,7 @@ public function getAllModelClasses(): array $traits = $reflection->getTraitNames(); foreach ($traits as $trait) { if ('Korridor\\LaravelComputedAttributes\\ComputedAttributes' === $trait) { - array_push($models, $class); + $models[] = $class; } } } diff --git a/src/Parser/ModelAttributesEntry.php b/src/Parser/ModelAttributesEntry.php index 2603130..9be96b3 100644 --- a/src/Parser/ModelAttributesEntry.php +++ b/src/Parser/ModelAttributesEntry.php @@ -4,23 +4,26 @@ namespace Korridor\LaravelComputedAttributes\Parser; +use Illuminate\Database\Eloquent\Model; +use Korridor\LaravelComputedAttributes\ComputedAttributes; + class ModelAttributesEntry { /** - * @var string + * @var class-string */ - private $model; + private string $model; /** - * @var string[] + * @var array */ - private $attributes; + private array $attributes; /** * ModelAttributesEntry constructor. * - * @param string $model - * @param string[] $attributes + * @param class-string $model + * @param array $attributes */ public function __construct(string $model, array $attributes) { @@ -29,7 +32,7 @@ public function __construct(string $model, array $attributes) } /** - * @return string + * @return class-string */ public function getModel(): string { @@ -37,7 +40,7 @@ public function getModel(): string } /** - * @return array + * @return array */ public function getAttributes(): array { diff --git a/tests/Feature/GenerateComputedAttributesCommandTest.php b/tests/Feature/GenerateComputedAttributesCommandTest.php index 62bbd76..a8276be 100644 --- a/tests/Feature/GenerateComputedAttributesCommandTest.php +++ b/tests/Feature/GenerateComputedAttributesCommandTest.php @@ -4,7 +4,6 @@ namespace Korridor\LaravelComputedAttributes\Tests\Feature; -use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Event; use Korridor\LaravelComputedAttributes\Tests\TestCase; @@ -15,8 +14,6 @@ class GenerateComputedAttributesCommandTest extends TestCase { - use RefreshDatabase; - public function testCommandComputesAttributesForAllModelsWithTraitAndAllThereAttributes(): void { // Arrange diff --git a/tests/Feature/ValidateComputedAttributesCommandTest.php b/tests/Feature/ValidateComputedAttributesCommandTest.php index eda0532..eca1ecd 100644 --- a/tests/Feature/ValidateComputedAttributesCommandTest.php +++ b/tests/Feature/ValidateComputedAttributesCommandTest.php @@ -4,7 +4,6 @@ namespace Korridor\LaravelComputedAttributes\Tests\Feature; -use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Config; use Korridor\LaravelComputedAttributes\Tests\TestCase; use Korridor\LaravelComputedAttributes\Tests\TestEnvironment\Models\Post; @@ -12,8 +11,6 @@ class ValidateComputedAttributesCommandTest extends TestCase { - use RefreshDatabase; - public function testCommandComputesAttributesForAllModelsWithTraitAndAllThereAttributes(): void { // Arrange diff --git a/tests/TestCase.php b/tests/TestCase.php index b1c8a76..7d9f0d0 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -24,7 +24,7 @@ protected function getEnvironmentSetUp($app): void /** * @param Application $app - * @return array + * @return array */ protected function getPackageProviders($app): array { diff --git a/tests/TestEnvironment/Models/Post.php b/tests/TestEnvironment/Models/Post.php index 459b0b9..b9ab51c 100644 --- a/tests/TestEnvironment/Models/Post.php +++ b/tests/TestEnvironment/Models/Post.php @@ -5,12 +5,21 @@ namespace Korridor\LaravelComputedAttributes\Tests\TestEnvironment\Models; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Korridor\LaravelComputedAttributes\ComputedAttributes; use Korridor\LaravelComputedAttributes\Tests\TestEnvironment\Events\PostSaved; use Korridor\LaravelComputedAttributes\Tests\TestEnvironment\Events\PostSaving; +/** + * @property int $id + * @property string $title + * @property string $content + * @property int $complex_calculation + * @property int $sum_of_votes + * @property-read Collection $votes + */ class Post extends Model { use ComputedAttributes; @@ -19,9 +28,9 @@ class Post extends Model * The attributes that are computed. (f.e. for performance reasons) * These attributes can be regenerated at any time. * - * @var array + * @var array */ - protected $computed = [ + protected array $computed = [ 'complex_calculation', 'sum_of_votes', ]; @@ -29,7 +38,7 @@ class Post extends Model /** * The attributes that should be cast to native types. * - * @var array + * @var array */ protected $casts = [ 'complex_calculation' => 'int', @@ -37,7 +46,7 @@ class Post extends Model ]; /** - * @var array + * @var array */ protected $dispatchesEvents = [ 'saved' => PostSaved::class, @@ -71,9 +80,9 @@ public function getSumOfVotesComputed(): int /** * This scope will be applied during the computed property generation with artisan computed-attributes:generate. * - * @param Builder $builder - * @param array $attributes Attributes that will be generated. - * @return Builder + * @param Builder $builder + * @param array $attributes Attributes that will be generated. + * @return Builder */ public function scopeComputedAttributesGenerate(Builder $builder, array $attributes): Builder { @@ -87,9 +96,9 @@ public function scopeComputedAttributesGenerate(Builder $builder, array $attribu /** * This scope will be applied during the computed property validation with artisan computed-attributes:validate. * - * @param Builder $builder - * @param array $attributes Attributes that will be validated. - * @return Builder + * @param Builder $builder + * @param array $attributes Attributes that will be validated. + * @return Builder */ public function scopeComputedAttributesValidate(Builder $builder, array $attributes): Builder { @@ -105,7 +114,7 @@ public function scopeComputedAttributesValidate(Builder $builder, array $attribu */ /** - * @return HasMany|Vote + * @return HasMany */ public function votes(): HasMany { diff --git a/tests/TestEnvironment/Models/Vote.php b/tests/TestEnvironment/Models/Vote.php index b08e37b..d568580 100644 --- a/tests/TestEnvironment/Models/Vote.php +++ b/tests/TestEnvironment/Models/Vote.php @@ -7,12 +7,16 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +/** + * @property int $id + * @property int $rating + */ class Vote extends Model { /** * The attributes that should be cast to native types. * - * @var array + * @var array */ protected $casts = [ 'rating' => 'int', @@ -23,7 +27,7 @@ class Vote extends Model */ /** - * @return BelongsTo + * @return BelongsTo */ public function post(): BelongsTo {