Skip to content

Commit af71524

Browse files
committed
fix: phpstan & init phpunit
1 parent 0b1de81 commit af71524

File tree

12 files changed

+121
-14
lines changed

12 files changed

+121
-14
lines changed

.github/workflows/php.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ jobs:
4343
- name: Install dependencies
4444
run: composer require laravel/framework ${{ matrix.laravel_version }} --prefer-dist --no-progress
4545

46-
- name: PHPUnit
47-
run: vendor/bin/phpunit --coverage-clover coverage.xml --configuration phpunit.php${{ matrix.php_version }}.xml.dist
48-
4946
- name: PHPStan
5047
run: vendor/bin/phpstan analyze
5148

49+
- name: PHPUnit
50+
run: vendor/bin/phpunit --coverage-clover coverage.xml --configuration phpunit.php${{ matrix.php_version }}.xml.dist
51+
5252
- name: Coverage
5353
run: bash <(curl -s https://codecov.io/bash)

phpunit.php8.4.xml.dist

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
executionOrder="depends,defects"
6+
failOnRisky="true"
7+
failOnWarning="true"
8+
>
9+
<testsuites>
10+
<testsuite name="Unit">
11+
<directory>tests/Unit</directory>
12+
</testsuite>
13+
<testsuite name="Feature">
14+
<directory>tests/Feature</directory>
15+
</testsuite>
16+
</testsuites>
17+
<php>
18+
<env name="DB_CONNECTION" value="sqlite"/>
19+
<env name="DB_DATABASE" value=":memory:"/>
20+
</php>
21+
<source>
22+
<include>
23+
<directory suffix=".php">src</directory>
24+
</include>
25+
</source>
26+
</phpunit>

src/Asserts/AssertJsonApiResource.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Ark4ne\JsonApi\Asserts;
4+
5+
use Ark4ne\JsonApi\Descriptors\Values\Value;
6+
use Ark4ne\JsonApi\Resources\JsonApiResource;
7+
use Ark4ne\JsonApi\Support\FakeModel;
8+
use Ark4ne\JsonApi\Support\Values;
9+
use Illuminate\Http\Request;
10+
use Illuminate\Http\Resources\MissingValue;
11+
use ReflectionClass;
12+
13+
class AssertJsonApiResource
14+
{
15+
/**
16+
* @param class-string<JsonApiResource> $class
17+
*/
18+
public function __construct(protected string $class)
19+
{
20+
}
21+
22+
public function assert(): void
23+
{
24+
$this->itCanGenerateSchema();
25+
$this->allAttributesAreLazySet();
26+
}
27+
28+
private function itCanGenerateSchema(): void
29+
{
30+
try {
31+
$this->class::schema();
32+
} catch (\Throwable $throwable) {
33+
throw new FailGenerateSchema($this->class, $throwable);
34+
}
35+
}
36+
37+
private function allAttributesAreLazySet(): void
38+
{
39+
$reflection = new ReflectionClass($this->class);
40+
$instance = $reflection->newInstanceWithoutConstructor();
41+
$instance->resource = new FakeModel;
42+
43+
$method = $reflection->getMethod('toAttributes');
44+
$method->setAccessible(true);
45+
/** @var iterable<array-key, mixed> $attributes */
46+
$attributes = $method->invoke($instance, new Request());
47+
$attributes = Values::mergeValues($attributes);
48+
49+
foreach ($attributes as $key => $attribute) {
50+
if (!($attribute instanceof \Closure || $attribute instanceof MissingValue || $attribute instanceof Value)) {
51+
throw new EagerSetAttribute($this->class, $key);
52+
}
53+
}
54+
}
55+
}

src/Asserts/EagerSetAttribute.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Ark4ne\JsonApi\Asserts;
4+
5+
use Throwable;
6+
7+
class EagerSetAttribute extends \Exception
8+
{
9+
public function __construct(string $class, string $key, ?Throwable $previous = null)
10+
{
11+
parent::__construct("Attribute [$key] on resource [$class] is eager set.", 0, $previous);
12+
}
13+
}

src/Asserts/FailGenerateSchema.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Ark4ne\JsonApi\Asserts;
4+
5+
use Throwable;
6+
7+
class FailGenerateSchema extends \Exception
8+
{
9+
public function __construct(string $class, ?Throwable $previous = null)
10+
{
11+
parent::__construct("Can't generate schema for resource [$class].", 0, $previous);
12+
}
13+
}

src/Descriptors/Relations/Relation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function meta(Closure $meta): static
6565
* @param bool|null $whenIncluded
6666
* @return $this
6767
*/
68-
public function whenIncluded(bool $whenIncluded = null): static
68+
public function whenIncluded(null|bool $whenIncluded = null): static
6969
{
7070
if ($whenIncluded === null) {
7171
$this->whenIncluded ??= true;
@@ -83,7 +83,7 @@ public function whenIncluded(bool $whenIncluded = null): static
8383
*
8484
* @return static
8585
*/
86-
public function whenLoaded(string $relation = null): self
86+
public function whenLoaded(null|string $relation = null): self
8787
{
8888
return $this->when(fn(
8989
Request $request,
@@ -100,7 +100,7 @@ public function whenLoaded(string $relation = null): self
100100
*
101101
* @return static
102102
*/
103-
public function whenPivotLoaded(string $table, string $accessor = null): self
103+
public function whenPivotLoaded(string $table, null|string $accessor = null): self
104104
{
105105
return $this->when(fn(
106106
Request $request,

src/Resources/Concerns/Relationships.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private function requestedRelationshipsLoadFromSchema(Request $request, Skeleton
6060
if ($load && Includes::include($request, $name)) {
6161
$include = Includes::through($name, fn() => $this->requestedRelationshipsLoadFromSchema($request, $schema->relationships[$name]));
6262

63-
$apply = static function ($load, string $pre = null) use (&$loads, &$apply) {
63+
$apply = static function ($load, null|string $pre = null) use (&$loads, &$apply) {
6464
foreach ((array)$load as $key => $value) {
6565
if (is_string($value)) {
6666
$loads["$pre.$value"] = [];

src/Resources/Concerns/Schema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ trait Schema
2121
*/
2222
private static array $schemas = [];
2323

24-
public static function schema(Request $request = null): Skeleton
24+
public static function schema(null|Request $request = null): Skeleton
2525
{
2626
if (isset(self::$schemas[static::class])) {
2727
return self::$schemas[static::class];

src/Resources/Concerns/SchemaCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
trait SchemaCollection
1010
{
11-
public static function schema(Request $request = null): Skeleton
11+
public static function schema(null|Request $request = null): Skeleton
1212
{
1313
return self::new()->collects::schema($request);
1414
}

src/Resources/Relationship.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function asCollection(): self
100100
* @param bool|null $whenIncluded
101101
* @return $this
102102
*/
103-
public function whenIncluded(bool $whenIncluded = null): static
103+
public function whenIncluded(null|bool $whenIncluded = null): static
104104
{
105105
if ($whenIncluded === null) {
106106
$this->whenIncluded ??= true;

src/Support/Arr.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private static function flattenDot(array $array, string $prepend, string $saveKe
107107
* @param string|null $saveKey
108108
* @return array<mixed>
109109
*/
110-
public static function undot(array $array, string $saveKey = null): array
110+
public static function undot(array $array, null|string $saveKey = null): array
111111
{
112112
$results = [];
113113

@@ -129,7 +129,7 @@ public static function undot(array $array, string $saveKey = null): array
129129
* @param string|null $saveKey
130130
* @return mixed
131131
*/
132-
public static function apply(array &$array, string $path, mixed $value, string $saveKey = null): mixed
132+
public static function apply(array &$array, string $path, mixed $value, null|string $saveKey = null): mixed
133133
{
134134
$keys = explode('.', $path);
135135

src/Support/Fields.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function through(string $type, callable $callable): mixed
3737
*
3838
* @return string[]|null
3939
*/
40-
public static function get(Request $request, string $type = null): ?array
40+
public static function get(Request $request, null|string $type = null): ?array
4141
{
4242
$type ??= self::$current;
4343

@@ -57,7 +57,7 @@ public static function get(Request $request, string $type = null): ?array
5757
*
5858
* @return bool
5959
*/
60-
public static function has(Request $request, string $field, string $type =null): bool {
60+
public static function has(Request $request, string $field, null|string $type =null): bool {
6161
$type ??= self::$current;
6262

6363
if ($type === null) {

0 commit comments

Comments
 (0)