Skip to content

Commit

Permalink
Added laravel 11 and php 8.3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Mar 1, 2024
1 parent b2ca00e commit 8211f50
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 73 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
53 changes: 36 additions & 17 deletions .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 13 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": {
Expand All @@ -48,5 +52,7 @@
},
"config": {
"sort-packages": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -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/

Expand Down
11 changes: 11 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
includes:
- ./vendor/larastan/larastan/extension.neon

parameters:

paths:
- src
- tests

# Level 9 is the highest level
level: 9
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 3 additions & 7 deletions src/ComputedAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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<int, string>
*/
public function getComputedAttributeConfiguration(): array
{
if (isset($this->computed)) {
return $this->computed;
} else {
return [];
}
return $this->computed ?? [];
}
}
3 changes: 1 addition & 2 deletions src/Console/GenerateComputedAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
9 changes: 1 addition & 8 deletions src/Parser/ModelAttributeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@

class ModelAttributeParser
{
/**
* ModelAttributeParser constructor.
*/
public function __construct()
{
}

/**
* @return string
*/
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
19 changes: 11 additions & 8 deletions src/Parser/ModelAttributesEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@

namespace Korridor\LaravelComputedAttributes\Parser;

use Illuminate\Database\Eloquent\Model;
use Korridor\LaravelComputedAttributes\ComputedAttributes;

class ModelAttributesEntry
{
/**
* @var string
* @var class-string<Model>
*/
private $model;
private string $model;

/**
* @var string[]
* @var array<int, string>
*/
private $attributes;
private array $attributes;

/**
* ModelAttributesEntry constructor.
*
* @param string $model
* @param string[] $attributes
* @param class-string<Model> $model
* @param array<int, string> $attributes
*/
public function __construct(string $model, array $attributes)
{
Expand All @@ -29,15 +32,15 @@ public function __construct(string $model, array $attributes)
}

/**
* @return string
* @return class-string<Model|ComputedAttributes>
*/
public function getModel(): string
{
return $this->model;
}

/**
* @return array
* @return array<int, string>
*/
public function getAttributes(): array
{
Expand Down
3 changes: 0 additions & 3 deletions tests/Feature/GenerateComputedAttributesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,8 +14,6 @@

class GenerateComputedAttributesCommandTest extends TestCase
{
use RefreshDatabase;

public function testCommandComputesAttributesForAllModelsWithTraitAndAllThereAttributes(): void
{
// Arrange
Expand Down
3 changes: 0 additions & 3 deletions tests/Feature/ValidateComputedAttributesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@

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;
use Korridor\LaravelComputedAttributes\Tests\TestEnvironment\Models\Vote;

class ValidateComputedAttributesCommandTest extends TestCase
{
use RefreshDatabase;

public function testCommandComputesAttributesForAllModelsWithTraitAndAllThereAttributes(): void
{
// Arrange
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function getEnvironmentSetUp($app): void

/**
* @param Application $app
* @return array
* @return array<int, class-string>
*/
protected function getPackageProviders($app): array
{
Expand Down
31 changes: 20 additions & 11 deletions tests/TestEnvironment/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vote> $votes
*/
class Post extends Model
{
use ComputedAttributes;
Expand All @@ -19,25 +28,25 @@ 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<int, string>
*/
protected $computed = [
protected array $computed = [
'complex_calculation',
'sum_of_votes',
];

/**
* The attributes that should be cast to native types.
*
* @var array
* @var array<string, string>
*/
protected $casts = [
'complex_calculation' => 'int',
'sum_of_votes' => 'int',
];

/**
* @var array
* @var array<string, class-string>
*/
protected $dispatchesEvents = [
'saved' => PostSaved::class,
Expand Down Expand Up @@ -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<self> $builder
* @param array<string> $attributes Attributes that will be generated.
* @return Builder<self>
*/
public function scopeComputedAttributesGenerate(Builder $builder, array $attributes): Builder
{
Expand All @@ -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<self> $builder
* @param array<string> $attributes Attributes that will be validated.
* @return Builder<self>
*/
public function scopeComputedAttributesValidate(Builder $builder, array $attributes): Builder
{
Expand All @@ -105,7 +114,7 @@ public function scopeComputedAttributesValidate(Builder $builder, array $attribu
*/

/**
* @return HasMany|Vote
* @return HasMany<Vote>
*/
public function votes(): HasMany
{
Expand Down
Loading

0 comments on commit 8211f50

Please sign in to comment.