JsonApiErrorData::merge()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\JsonApiError\Data;
6
7
use Illuminate\Contracts\Support\Arrayable;
8
use Illuminate\Support\Arr;
9
use Symfony\Component\HttpFoundation\Response;
10
use Throwable;
11
12
/**
13
 * The JSON:API error data.
14
 *
15
 * @implements Arrayable<string, mixed>
16
 */
17
final class JsonApiErrorData implements Arrayable
18
{
19
    /**
20
     * Instantiate the class.
21
     *
22
     * @param array<string, mixed> $meta
23
     */
24 11
    public function __construct(
25
        public readonly ?string $detail = null,
26
        public readonly int $status = Response::HTTP_BAD_REQUEST,
27
        public readonly ?string $code = null,
28
        public readonly ?string $title = null,
29
        public readonly mixed $id = null,
30
        public readonly ?string $source = null,
31
        public readonly array $meta = [],
32 11
    ) {}
33
34
    /**
35
     * Instantiate the class from the given unexpected throwable.
36
     */
37 1
    public static function unexpected(Throwable $e): self
38
    {
39 1
        return new self(status: Response::HTTP_INTERNAL_SERVER_ERROR, meta: config('app.debug') ? [
40 1
            'message' => $e->getMessage(),
41 1
            'previous_message' => $e->getPrevious()?->getMessage(),
42 1
            'trace' => $e->getTrace(),
43 1
            'previous_trace' => $e->getPrevious()?->getTrace(),
44 1
        ] : []);
45
    }
46
47
    /**
48
     * Instantiate the class from the given unprocessable entity.
49
     */
50 2
    public static function unprocessable(string $detail, ?string $dot = null): self
51
    {
52 2
        return new self($detail, Response::HTTP_UNPROCESSABLE_ENTITY, source: $dot);
53
    }
54
55
    /**
56
     * Retrieve the error as an array.
57
     *
58
     * @return array<string, mixed>
59
     */
60 11
    public function toArray()
61
    {
62 11
        return [
63 11
            'id' => $this->id,
64 11
            'status' => (string) $this->status,
65 11
            'code' => $this->code,
66 11
            'title' => $this->title ?: __("json-api-error::statuses.{$this->status}.title"),
67 11
            'detail' => $this->detail ?: __("json-api-error::statuses.{$this->status}.detail"),
68 11
            'source' => $this->source ? ['pointer' => (new Dot($this->source))->toJsonPointer()] : null,
69 11
            'meta' => $this->meta,
70 11
        ];
71
    }
72
73
    /**
74
     * Merge the given JSON:API error.
75
     */
76 1
    public function merge(JsonApiErrorData $data): self
77
    {
78 1
        $filled = array_filter((array) $this, filled(...));
0 ignored issues
show
Bug introduced by
The type Cerbero\JsonApiError\Data\filled was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
79 1
        $mergeable = Arr::only((array) $data, ['id', 'code', 'meta']);
80
81 1
        return new self(...array_replace_recursive($mergeable, $filled));
0 ignored issues
show
Bug introduced by
array_replace_recursive($mergeable, $filled) is expanded, but the parameter $detail of Cerbero\JsonApiError\Dat...rrorData::__construct() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
        return new self(/** @scrutinizer ignore-type */ ...array_replace_recursive($mergeable, $filled));
Loading history...
82
    }
83
}
84