Skip to content

Commit d4c2722

Browse files
committed
Added Laravel 12 support
1 parent 7d9b6df commit d4c2722

15 files changed

+306
-107
lines changed

.github/workflows/unittests.yml

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,89 @@ jobs:
1212
laravel: 10.*
1313
testbench: 8.*
1414
composer-flag: '--prefer-stable'
15+
phpunit-config: 'phpunit-10.xml'
1516
- php: 8.2
1617
laravel: 10.*
1718
testbench: 8.*
1819
composer-flag: '--prefer-stable'
20+
phpunit-config: 'phpunit-10.xml'
1921
- php: 8.3
2022
laravel: 10.*
2123
testbench: 8.*
2224
composer-flag: '--prefer-stable'
25+
phpunit-config: 'phpunit-10.xml'
2326
- php: 8.1
2427
laravel: 10.*
2528
testbench: 8.*
2629
composer-flag: '--prefer-lowest'
30+
phpunit-config: 'phpunit-10.xml'
2731
- php: 8.2
2832
laravel: 10.*
2933
testbench: 8.*
3034
composer-flag: '--prefer-lowest'
35+
phpunit-config: 'phpunit-10.xml'
3136
- php: 8.3
3237
laravel: 10.*
3338
testbench: 8.*
3439
composer-flag: '--prefer-lowest'
40+
phpunit-config: 'phpunit-10.xml'
3541
# Laravel 11.*
3642
- php: 8.2
3743
laravel: 11.*
3844
testbench: 9.*
3945
composer-flag: '--prefer-stable'
46+
phpunit-config: 'phpunit.xml'
4047
- php: 8.3
4148
laravel: 11.*
4249
testbench: 9.*
4350
composer-flag: '--prefer-stable'
51+
phpunit-config: 'phpunit.xml'
4452
- php: 8.2
4553
laravel: 11.*
4654
testbench: 9.*
4755
composer-flag: '--prefer-lowest'
56+
phpunit-config: 'phpunit.xml'
4857
- php: 8.2
4958
laravel: 11.*
5059
testbench: 9.*
5160
composer-flag: '--prefer-lowest'
61+
phpunit-config: 'phpunit.xml'
62+
- php: 8.4
63+
laravel: 11.*
64+
testbench: 9.*
65+
composer-flag: '--prefer-stable'
66+
phpunit-config: 'phpunit.xml'
67+
# Laravel 12
68+
- php: 8.2
69+
laravel: 12.*
70+
testbench: 10.*
71+
composer-flag: '--prefer-stable'
72+
phpunit-config: 'phpunit.xml'
73+
- php: 8.3
74+
laravel: 12.*
75+
testbench: 10.*
76+
composer-flag: '--prefer-stable'
77+
phpunit-config: 'phpunit.xml'
78+
- php: 8.4
79+
laravel: 12.*
80+
testbench: 10.*
81+
composer-flag: '--prefer-stable'
82+
phpunit-config: 'phpunit.xml'
83+
- php: 8.2
84+
laravel: 12.*
85+
testbench: 10.*
86+
composer-flag: '--prefer-lowest'
87+
phpunit-config: 'phpunit.xml'
88+
- php: 8.3
89+
laravel: 12.*
90+
testbench: 10.*
91+
composer-flag: '--prefer-lowest'
92+
phpunit-config: 'phpunit.xml'
93+
- php: 8.4
94+
laravel: 12.*
95+
testbench: 10.*
96+
composer-flag: '--prefer-lowest'
97+
phpunit-config: 'phpunit.xml'
5298

5399
runs-on: ubuntu-latest
54100

@@ -69,7 +115,7 @@ jobs:
69115
run: composer update ${{ matrix.composer-flag }} --prefer-dist --no-interaction
70116

71117
- name: Run PHPUnit
72-
run: XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml
118+
run: XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text --config="${{ matrix.phpunit-config }}" --coverage-clover=coverage.xml
73119

74120
- name: Upload coverage reports to Codecov
75121
uses: codecov/codecov-action@v4

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
"require": {
1414
"php": ">=8.1",
1515
"composer/composer": "^2",
16-
"illuminate/console": "^10|^11",
17-
"illuminate/database": "^10|^11",
18-
"illuminate/support": "^10|^11"
16+
"illuminate/console": "^10|^11|^12",
17+
"illuminate/database": "^10|^11|^12",
18+
"illuminate/support": "^10|^11|^12"
1919
},
2020
"require-dev": {
2121
"friendsofphp/php-cs-fixer": "^3",
22-
"larastan/larastan": "^2.9",
23-
"orchestra/testbench": "^8|^9",
24-
"phpunit/phpunit": "^10",
22+
"larastan/larastan": "^2|^3",
23+
"orchestra/testbench": "^8|^9|^10",
24+
"phpunit/phpunit": "^10|^11",
2525
"squizlabs/php_codesniffer": "^3.5"
2626
},
2727
"autoload": {
@@ -53,6 +53,6 @@
5353
"config": {
5454
"sort-packages": true
5555
},
56-
"minimum-stability": "dev",
56+
"minimum-stability": "stable",
5757
"prefer-stable": true
5858
}

phpunit-10.bak

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src/</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Feature">
10+
<directory>tests/Feature</directory>
11+
</testsuite>
12+
</testsuites>
13+
<php>
14+
<env name="APP_ENV" value="testing"/>
15+
<env name="CACHE_DRIVER" value="array"/>
16+
<env name="SESSION_DRIVER" value="array"/>
17+
<env name="QUEUE_DRIVER" value="sync"/>
18+
<env name="DB_CONNECTION" value="sqlite"/>
19+
<env name="DB_DATABASE" value=":memory:"/>
20+
<server name="APP_ENV" value="testing"/>
21+
<server name="CACHE_DRIVER" value="array"/>
22+
<server name="SESSION_DRIVER" value="array"/>
23+
<server name="QUEUE_DRIVER" value="sync"/>
24+
<server name="DB_CONNECTION" value="sqlite"/>
25+
<server name="DB_DATABASE" value=":memory:"/>
26+
</php>
27+
</phpunit>

phpunit.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3-
<coverage>
4-
<include>
5-
<directory suffix=".php">src/</directory>
6-
</include>
7-
</coverage>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
83
<testsuites>
94
<testsuite name="Feature">
105
<directory>tests/Feature</directory>
@@ -24,4 +19,9 @@
2419
<server name="DB_CONNECTION" value="sqlite"/>
2520
<server name="DB_DATABASE" value=":memory:"/>
2621
</php>
22+
<source>
23+
<include>
24+
<directory suffix=".php">src/</directory>
25+
</include>
26+
</source>
2727
</phpunit>

readme.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@ php artisan vendor:publish --tag=computed-attributes-config
3939

4040
This package is tested for the following Laravel and PHP versions:
4141

42+
- 12.* (PHP 8.2, 8.3, 8.4)
43+
- 11.* (PHP 8.2, 8.3, 8.4)
4244
- 10.* (PHP 8.1, 8.2, 8.3)
43-
- 11.* (PHP 8.2, 8.3)
4445

4546
## Usage examples
4647

4748
Here is an example of two computed attributes `complex_calculation` and `sum_of_votes`.
4849
The functions `getComplexCalculationComputed` and `getSumOfVotesComputed` are calculating the computed attributes.
4950

5051
```php
52+
use Illuminate\Database\Eloquent\Model;
5153
use Korridor\LaravelComputedAttributes\ComputedAttributes;
5254

53-
class Post {
55+
class Post extends Model {
5456

5557
use ComputedAttributes;
5658

src/ComputedAttributes.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
use Illuminate\Support\Str;
1010

1111
/**
12-
* @method static Builder|Model computedAttributesValidate(array $attributes)
13-
* @method static Builder|Model computedAttributesGenerate(array $attributes)
12+
* @method Builder<static> computedAttributesValidate(array<string> $attributes)
13+
* @method Builder<static> computedAttributesGenerate(array<string> $attributes)
14+
* @phpstan-require-extends Model
1415
*/
1516
trait ComputedAttributes
1617
{
@@ -41,9 +42,9 @@ public function setComputedAttributeValue(string $attributeName): void
4142
/**
4243
* This scope will be applied during the computed property generation with artisan computed-attributes:generate.
4344
*
44-
* @param Builder $builder
45-
* @param array $attributes Attributes that will be generated.
46-
* @return Builder
45+
* @param Builder<static> $builder
46+
* @param array<string> $attributes Attributes that will be generated.
47+
* @return Builder<static>
4748
*/
4849
public function scopeComputedAttributesGenerate(Builder $builder, array $attributes): Builder
4950
{
@@ -53,9 +54,9 @@ public function scopeComputedAttributesGenerate(Builder $builder, array $attribu
5354
/**
5455
* This scope will be applied during the computed property validation with artisan computed-attributes:validate.
5556
*
56-
* @param Builder $builder
57-
* @param array $attributes Attributes that will be validated.
58-
* @return Builder
57+
* @param Builder<static> $builder
58+
* @param array<string> $attributes Attributes that will be validated.
59+
* @return Builder<static>
5960
*/
6061
public function scopeComputedAttributesValidate(Builder $builder, array $attributes): Builder
6162
{
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Korridor\LaravelComputedAttributes;
6+
7+
use Illuminate\Database\Eloquent\Builder;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Support\Str;
10+
11+
/**
12+
* @method Builder<static> computedAttributesValidate(array<string> $attributes)
13+
* @method Builder<static> computedAttributesGenerate(array<string> $attributes)
14+
* @phpstan-require-extends Model
15+
*/
16+
interface ComputedAttributesInterface
17+
{
18+
/**
19+
* Compute the given attribute and return the result.
20+
*
21+
* @param string $attributeName
22+
* @return mixed
23+
*/
24+
public function getComputedAttributeValue(string $attributeName): mixed;
25+
26+
/**
27+
* Compute the given attribute and assign the result in the model.
28+
*
29+
* @param string $attributeName
30+
*/
31+
public function setComputedAttributeValue(string $attributeName): void;
32+
33+
/**
34+
* This scope will be applied during the computed property generation with artisan computed-attributes:generate.
35+
*
36+
* @param Builder<static> $builder
37+
* @param array<string> $attributes Attributes that will be generated.
38+
* @return Builder<static>
39+
*/
40+
public function scopeComputedAttributesGenerate(Builder $builder, array $attributes): Builder;
41+
42+
/**
43+
* This scope will be applied during the computed property validation with artisan computed-attributes:validate.
44+
*
45+
* @param Builder<static> $builder
46+
* @param array<string> $attributes Attributes that will be validated.
47+
* @return Builder<static>
48+
*/
49+
public function scopeComputedAttributesValidate(Builder $builder, array $attributes): Builder;
50+
51+
/**
52+
* Return the configuration array for this model.
53+
* If the configuration array does not exist the function will return an empty array.
54+
*
55+
* @return array<int, string>
56+
*/
57+
public function getComputedAttributeConfiguration(): array;
58+
}

src/Console/GenerateComputedAttributes.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
namespace Korridor\LaravelComputedAttributes\Console;
66

77
use Illuminate\Console\Command;
8-
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\Builder;
99
use Illuminate\Support\Collection;
10-
use Korridor\LaravelComputedAttributes\ComputedAttributes;
10+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
11+
use Korridor\LaravelComputedAttributes\ComputedAttributesInterface;
1112
use Korridor\LaravelComputedAttributes\Parser\ModelAttributeParser;
1213
use Korridor\LaravelComputedAttributes\Parser\ModelAttributesEntry;
1314
use Korridor\LaravelComputedAttributes\Parser\ParsingException;
@@ -46,13 +47,19 @@ class GenerateComputedAttributes extends Command
4647
*/
4748
public function handle(): int
4849
{
50+
$this->info('Parsing arguments...');
51+
52+
// Validate modelsAttributes argument
4953
$modelsWithAttributes = $this->argument('modelsAttributes');
54+
if ($modelsWithAttributes !== null && !is_string($modelsWithAttributes)) {
55+
$this->error('Argument modelsAttributes needs to be a string');
5056

51-
$this->info('Parsing arguments...');
57+
return self::FAILURE;
58+
}
5259

5360
// Validate chunkSize option
5461
$chunkSizeRaw = $this->option('chunkSize');
55-
if (preg_match('/^\d+$/', $chunkSizeRaw)) {
62+
if (is_string($chunkSizeRaw) && preg_match('/^\d+$/', $chunkSizeRaw)) {
5663
$chunkSize = (int) $chunkSizeRaw;
5764
if ($chunkSize < 1) {
5865
$this->error('Option chunkSize needs to be greater than zero');
@@ -68,7 +75,7 @@ public function handle(): int
6875
// Validate block option
6976
$chunkRaw = $this->option('chunk');
7077
if ($chunkRaw !== null) {
71-
if (preg_match('/^\d+$/', $chunkRaw)) {
78+
if (is_string($chunkRaw) && preg_match('/^\d+$/', $chunkRaw)) {
7279
$chunk = (int) $chunkRaw;
7380
if ($chunk < 0) {
7481
$this->error('Option chunk needs to be greater or equal than zero');
@@ -98,7 +105,7 @@ public function handle(): int
98105
// Calculate
99106
foreach ($modelAttributesEntries as $modelAttributesEntry) {
100107
$model = $modelAttributesEntry->getModel();
101-
/** @var Model|ComputedAttributes $modelInstance */
108+
/** @var ComputedAttributesInterface $modelInstance */
102109
$modelInstance = new $model();
103110
$attributes = $modelAttributesEntry->getAttributes();
104111

@@ -123,9 +130,14 @@ public function handle(): int
123130
return self::SUCCESS;
124131
}
125132

126-
private function generateModels(Collection $models, array $attributes, ModelAttributesEntry $modelAttributesEntry): void
133+
/**
134+
* @param EloquentCollection<array-key, ComputedAttributesInterface> $models
135+
* @param array<string> $attributes
136+
* @param ModelAttributesEntry $modelAttributesEntry
137+
* @return void
138+
*/
139+
private function generateModels(EloquentCollection $models, array $attributes, ModelAttributesEntry $modelAttributesEntry): void
127140
{
128-
/* @var Model|ComputedAttributes $modelResult */
129141
foreach ($models as $modelResult) {
130142
foreach ($attributes as $attribute) {
131143
$modelResult->setComputedAttributeValue($attribute);

0 commit comments

Comments
 (0)