Skip to content

Commit 150d5af

Browse files
authored
PHP 8.4 Support + Laravel Pint (#33)
* build: support php8.4 * build: add laravel pint * chore: pint files
1 parent a5a367d commit 150d5af

File tree

15 files changed

+227
-168
lines changed

15 files changed

+227
-168
lines changed

.github/workflows/tests.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
strategy:
88
fail-fast: true
99
matrix:
10-
php: [8.1, 8.2, 8.3]
10+
php: [8.1, 8.2, 8.3, 8.4]
1111
stability: ["--prefer-lowest", "--prefer-stable"]
1212
name: PHP ${{ matrix.php }} - ${{ matrix.stability }}
1313
steps:
@@ -31,11 +31,13 @@ jobs:
3131
with:
3232
path: ${{ steps.composer-cache.outputs.dir }}
3333
key: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.stability }}-composer-${{ hashFiles('**/composer.json') }}
34-
restore-keys: |
35-
${{ runner.os }}-${{ matrix.php }}-${{ matrix.stability }}-composer-
34+
restore-keys: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.stability }}-composer-
3635

3736
- name: Install dependencies
3837
run: composer update ${{ matrix.stability }} --prefer-dist --no-interaction --no-suggest
3938

39+
- name: Run style check
40+
run: vendor/bin/pint --test
41+
4042
- name: Execute tests
4143
run: vendor/bin/phpunit

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
}
2121
},
2222
"require": {
23-
"php": "^8.1",
23+
"php": "^8.1||^8.2||^8.3||^8.4",
2424
"laravel/framework": "^10.0|^11.0"
2525
},
2626
"require-dev": {
2727
"orchestra/testbench": "^8.0|^9.0",
28-
"phpunit/phpunit": "^10.0|^11.0"
28+
"phpunit/phpunit": "^10.0|^11.0",
29+
"laravel/pint": "^1.19"
2930
}
3031
}

src/Formatting/FormatDefinition.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
class FormatDefinition
1414
{
1515
protected Collection $formats;
16+
1617
protected bool $isExplicitlyDefault;
18+
1719
protected ReflectionMethod $reflection;
1820

1921
public function __construct(ReflectionMethod $reflection)
2022
{
2123
$this->reflection = $reflection;
2224

2325
$this->formats = (new Collection($this->reflection->getAttributes(Format::class)))
24-
->map(fn(ReflectionAttribute $attribute) => $attribute->newInstance());
25-
$this->isExplicitlyDefault = !empty($this->reflection->getAttributes(IsDefault::class));
26+
->map(fn (ReflectionAttribute $attribute) => $attribute->newInstance());
27+
$this->isExplicitlyDefault = ! empty($this->reflection->getAttributes(IsDefault::class));
2628
}
2729

2830
public function invoke(object $object, $request): mixed
@@ -42,7 +44,7 @@ public function name(): string
4244

4345
public function names(): Collection
4446
{
45-
return $this->formats->map(fn(Format $format) => $format->name() ?? $this->reflection->getName())
47+
return $this->formats->map(fn (Format $format) => $format->name() ?? $this->reflection->getName())
4648
->unique();
4749
}
4850

src/Formatting/FormatManager.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
class FormatManager
1717
{
1818
protected ?string $current;
19+
1920
protected ?FormatDefinition $default;
21+
2022
protected Collection $formats;
23+
2124
protected ReflectionObject $reflection;
25+
2226
protected object $subject;
2327

2428
public function __construct(object $subject)
@@ -27,13 +31,13 @@ public function __construct(object $subject)
2731
$this->subject = $subject;
2832

2933
$definitions = (new Collection($this->reflection->getMethods()))
30-
->filter(fn(ReflectionMethod $method) => !empty($method->getAttributes(Format::class)))
34+
->filter(fn (ReflectionMethod $method) => ! empty($method->getAttributes(Format::class)))
3135
->mapInto(FormatDefinition::class);
3236

3337
$this->formats = $definitions
3438
->tap(Closure::fromCallable([$this, 'preventFormatNameCollisions']))
35-
->flatMap(fn(FormatDefinition $definition) => $definition->names()
36-
->mapWithKeys(fn(string $name) => [$name => $definition]));
39+
->flatMap(fn (FormatDefinition $definition) => $definition->names()
40+
->mapWithKeys(fn (string $name) => [$name => $definition]));
3741

3842
$this->default = $this->determineDefault($definitions);
3943

@@ -67,7 +71,7 @@ public function hasFormat(string $name): bool
6771

6872
public function lacksFormat(string $name): bool
6973
{
70-
return !$this->hasFormat($name);
74+
return ! $this->hasFormat($name);
7175
}
7276

7377
public function select(string $name): static
@@ -87,7 +91,7 @@ protected function determineDefault(Collection $definitions): ?FormatDefinition
8791
return $definitions->first();
8892
}
8993

90-
$definitions = $definitions->filter(fn(FormatDefinition $definition) => $definition->isExplicitlyDefault());
94+
$definitions = $definitions->filter(fn (FormatDefinition $definition) => $definition->isExplicitlyDefault());
9195
$class = $this->reflection;
9296

9397
do {
@@ -99,17 +103,17 @@ protected function determineDefault(Collection $definitions): ?FormatDefinition
99103
->first();
100104

101105
$class = $class->getParentClass();
102-
} while($class && $default === null);
106+
} while ($class && $default === null);
103107

104108
return $default;
105109
}
106110

107111
protected function preventFormatNameCollisions(Collection $formatMethods): void
108112
{
109-
$formatMethods->flatMap(fn(FormatDefinition $definition) => $definition->names())
113+
$formatMethods->flatMap(fn (FormatDefinition $definition) => $definition->names())
110114
->countBy()
111-
->filter(fn(int $count) => $count > 1)
112-
->whenNotEmpty(fn(Collection $collisions) => throw new FormatNameCollisionException(
115+
->filter(fn (int $count) => $count > 1)
116+
->whenNotEmpty(fn (Collection $collisions) => throw new FormatNameCollisionException(
113117
$this->subject,
114118
$collisions->keys()->first(),
115119
));

src/Resource.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ abstract class Resource extends JsonResource
1616
use SetsResponseStatus;
1717

1818
protected FormatManager $formatManager;
19+
1920
protected Collection $modifications;
2021

2122
public function __construct($resource)
@@ -48,8 +49,8 @@ public function format(string $name): static
4849

4950
public function modify(callable|array $modification): static
5051
{
51-
$modification = !is_callable($modification)
52-
? fn(array $data) => array_merge($data, $modification)
52+
$modification = ! is_callable($modification)
53+
? fn (array $data) => array_merge($data, $modification)
5354
: $modification;
5455

5556
$this->modifications->push($modification);
@@ -62,6 +63,6 @@ public function toArray($request)
6263
$currentFormat = $this->formatManager->current() ?? throw new NoFormatSelectedException($this);
6364
$data = $currentFormat->invoke($this, $request);
6465

65-
return $this->modifications->reduce(fn($carry, $modification) => $modification($carry, $this), $data);
66+
return $this->modifications->reduce(fn ($carry, $modification) => $modification($carry, $this), $data);
6667
}
6768
}

src/ResourceCollection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public function __construct($resource)
2020
{
2121
parent::__construct($resource);
2222

23-
if (!is_a($this->collects, Resource::class, true)) {
23+
if (! is_a($this->collects, Resource::class, true)) {
2424
throw new CannotEnhanceBaseResourcesException($this->collects);
2525
}
2626
}
2727

2828
public function __call($method, $parameters): mixed
2929
{
3030
if ((new ReflectionClass($this->collects))->hasMethod($method)) {
31-
$this->collection->map(fn(Resource $resource) => $resource->{$method}(...$parameters));
31+
$this->collection->map(fn (Resource $resource) => $resource->{$method}(...$parameters));
3232

3333
return $this;
3434
}
@@ -38,7 +38,7 @@ public function __call($method, $parameters): mixed
3838

3939
public function format(string $name): static
4040
{
41-
$this->collection->each(fn(Resource $resource) => $resource->format($name));
41+
$this->collection->each(fn (Resource $resource) => $resource->format($name));
4242

4343
return $this;
4444
}

tests/TestCase.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,4 @@
66

77
use Orchestra\Testbench\TestCase as OrchestraTestCase;
88

9-
class TestCase extends OrchestraTestCase
10-
{
11-
12-
}
9+
class TestCase extends OrchestraTestCase {}

tests/Unit/AnonymousResourceCollectionTest.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public function test_anonymous_collection_records_are_formatted_correctly(
1919
AnonymousResourceCollection $collection,
2020
array $expectedData
2121
): void {
22-
# Act
22+
// Act
2323
$actualData = $collection->toArray(request());
2424

25-
# Assert
25+
// Assert
2626
$this->assertSame($expectedData, $actualData);
2727
}
2828

@@ -31,16 +31,16 @@ public function test_anonymous_collection_can_be_modified_dynamically(
3131
ResourceCollection $resource,
3232
array $expectedData,
3333
): void {
34-
# Act
34+
// Act
3535
$actualData = $resource->toArray(request());
3636

37-
# Assert
37+
// Assert
3838
$this->assertSame($expectedData, $actualData);
3939
}
4040

4141
public function test_response_status_can_be_set(): void
4242
{
43-
# Arrange
43+
// Arrange
4444
$john = new stdClass;
4545
$john->id = 1;
4646
$john->firstName = 'John';
@@ -53,14 +53,14 @@ public function test_response_status_can_be_set(): void
5353

5454
$collection = ImplicitDefaultResource::collection([$john, $jane]);
5555

56-
# Act
56+
// Act
5757
$response = $collection->setResponseStatus(201)->response();
5858

59-
# Assert
59+
// Assert
6060
$this->assertSame(201, $response->getStatusCode());
6161
}
6262

63-
# region Data Providers
63+
// region Data Providers
6464

6565
public static function formatProvider(): array
6666
{
@@ -176,7 +176,7 @@ public static function modificationProvider(): array
176176
],
177177
'closure modification adding data' => [
178178
'resource' => ImplicitDefaultResource::collection([$john, $jane])
179-
->modify(fn(array $data) => array_merge($data, ['middle_initial' => 'A.'])),
179+
->modify(fn (array $data) => array_merge($data, ['middle_initial' => 'A.'])),
180180
'expectedData' => [
181181
[
182182
'first_name' => 'John',
@@ -194,7 +194,7 @@ public static function modificationProvider(): array
194194
],
195195
'closure modification overwriting data' => [
196196
'resource' => ImplicitDefaultResource::collection([$john, $jane])
197-
->modify(fn(array $data) => array_merge($data, ['first_name' => 'Jon'])),
197+
->modify(fn (array $data) => array_merge($data, ['first_name' => 'Jon'])),
198198
'expectedData' => [
199199
[
200200
'first_name' => 'Jon',
@@ -210,7 +210,7 @@ public static function modificationProvider(): array
210210
],
211211
'closure modification completely overwriting data' => [
212212
'resource' => ImplicitDefaultResource::collection([$john, $jane])
213-
->modify(fn() => ['id' => 1]),
213+
->modify(fn () => ['id' => 1]),
214214
'expectedData' => [
215215
['id' => 1],
216216
['id' => 1],
@@ -238,7 +238,8 @@ public static function modificationProvider(): array
238238
],
239239
'invokable modification adding data' => [
240240
'resource' => ImplicitDefaultResource::collection([$john, $jane])
241-
->modify(new class {
241+
->modify(new class
242+
{
242243
public function __invoke(array $data): array
243244
{
244245
return array_merge($data, ['middle_initial' => 'A.']);
@@ -261,7 +262,8 @@ public function __invoke(array $data): array
261262
],
262263
'invokable modification overwriting data' => [
263264
'resource' => ImplicitDefaultResource::collection([$john, $jane])
264-
->modify(new class {
265+
->modify(new class
266+
{
265267
public function __invoke(array $data): array
266268
{
267269
return array_merge($data, ['first_name' => 'Jon']);
@@ -282,7 +284,8 @@ public function __invoke(array $data): array
282284
],
283285
'invokable modification completely overwriting data' => [
284286
'resource' => ImplicitDefaultResource::collection([$john, $jane])
285-
->modify(new class {
287+
->modify(new class
288+
{
286289
public function __invoke(array $data): array
287290
{
288291
return ['id' => 1];
@@ -295,7 +298,8 @@ public function __invoke(array $data): array
295298
],
296299
'invokable modification accessing resource' => [
297300
'resource' => ImplicitDefaultResource::collection([$john, $jane])
298-
->modify(new class {
301+
->modify(new class
302+
{
299303
public function __invoke(array $data, ImplicitDefaultResource $resource): array
300304
{
301305
$data['id'] = $resource->resource->id * 2;
@@ -324,7 +328,8 @@ public function __invoke(array $data, ImplicitDefaultResource $resource): array
324328

325329
return $data;
326330
})
327-
->modify(new class {
331+
->modify(new class
332+
{
328333
public function __invoke(array $data, ImplicitDefaultResource $resource): array
329334
{
330335
$data['id'] = $resource->resource->id * 2;
@@ -350,5 +355,5 @@ public function __invoke(array $data, ImplicitDefaultResource $resource): array
350355
];
351356
}
352357

353-
# endregion
358+
// endregion
354359
}

tests/Unit/Enhancements/ExceptTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,21 @@ public function test_except_enhancement_can_be_applied_to_resources(
1818
Resource $resource,
1919
array $expectedData,
2020
): void {
21-
# Act
21+
// Act
2222
$actualData = $resource->toArray(request());
2323

24-
# Assert
24+
// Assert
2525
$this->assertSame($expectedData, $actualData);
2626
}
2727

28-
# region Data Providers
28+
// region Data Providers
2929

3030
public static function resourceProvider(): array
3131
{
3232
return [
3333
'applied manually' => [
34-
'resource' => (new class(null) extends Resource {
34+
'resource' => (new class(null) extends Resource
35+
{
3536
#[Format]
3637
public function foo(): array
3738
{
@@ -48,7 +49,8 @@ public function foo(): array
4849
],
4950
],
5051
'applied via trait' => [
51-
'resource' => (new class(null) extends Resource {
52+
'resource' => (new class(null) extends Resource
53+
{
5254
use HasExceptEnhancement;
5355

5456
#[Format]
@@ -69,5 +71,5 @@ public function foo(): array
6971
];
7072
}
7173

72-
# endregion
74+
// endregion
7375
}

0 commit comments

Comments
 (0)