GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

throwable_decode()   B
last analyzed

Complexity

Conditions 6
Paths 18

Size

Total Lines 53
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 53
ccs 4
cts 4
cp 1
rs 8.7857
cc 6
nc 18
nop 1
crap 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace WyriHaximus;
6
7
use Doctrine\Instantiator\Exception\ExceptionInterface;
8
use Doctrine\Instantiator\Instantiator;
9
use ReflectionClass;
10
use ReflectionException;
11
use ReflectionProperty;
12
use Throwable;
13
14
use function assert;
15
use function get_class;
16 1
use function Safe\json_decode;
17
use function Safe\json_encode;
18
use function serialize;
19
use function unserialize;
20
21
function throwable_json_encode(Throwable $throwable): string
22
{
23
    return json_encode(throwable_encode($throwable));
24 2
}
25
26
/**
27
 * @return array{class: class-string<Throwable>, message: string, code: mixed, file: string, line: int, previous: string|null, originalTrace: array<int, mixed>, additionalProperties: array<string, string>}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{class: class-strin... array<string, string>} at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array{class: class-string<Throwable>, message: string, code: mixed, file: string, line: int, previous: string|null, originalTrace: array<int, mixed>, additionalProperties: array<string, string>}.
Loading history...
28 2
 */
29 2
function throwable_encode(Throwable $throwable): array
30 2
{
31 2
    $json            = [];
32 2
    $json['class']   = get_class($throwable);
33 2
    $json['message'] = $throwable->getMessage();
34 2
    $json['code']    = $throwable->getCode();
35 2
    $json['file']    = $throwable->getFile();
36 2
    $json['line']    = $throwable->getLine();
37 2
    /** @psalm-suppress PossiblyNullArgument */
38 2
    $json['previous'] = $throwable->getPrevious() instanceof Throwable ? throwable_json_encode($throwable->getPrevious()) : null;
0 ignored issues
show
introduced by
$throwable->getPrevious() is always a sub-type of Throwable.
Loading history...
39
40
    $json['originalTrace'] = [];
41 2
    foreach ($throwable->getTrace() as $item) {
42
        $item['args']            = [];
43
        $json['originalTrace'][] = $item;
44
    }
45
46 1
    $json['additionalProperties'] = [];
47
    if ($throwable instanceof AdditionalPropertiesInterface) {
48
        $class = new ReflectionClass($json['class']);
0 ignored issues
show
Unused Code introduced by
The assignment to $class is dead and can be removed.
Loading history...
49
        foreach ($throwable->additionalProperties() as $key) {
50
            $property = new ReflectionProperty($json['class'], $key);
51
            $property->setAccessible(true);
52 6
            $json['additionalProperties'][$key] = serialize($property->getValue($throwable));
53
        }
54
    }
55
56
    return $json;
57
}
58
59
function throwable_json_decode(string $json): Throwable
60
{
61 6
    return throwable_decode(json_decode($json, true));
62
}
63 6
64
/**
65 6
 * @param array{class: class-string<Throwable>, message: string, code: mixed, file: string, line: int, previous: string|null, originalTrace: array<int, mixed>, additionalProperties: array<string, string>} $json
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{class: class-strin... array<string, string>} at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array{class: class-string<Throwable>, message: string, code: mixed, file: string, line: int, previous: string|null, originalTrace: array<int, mixed>, additionalProperties: array<string, string>}.
Loading history...
66 6
 *
67 6
 * @throws ExceptionInterface
68 6
 * @throws ReflectionException
69 4
 */
70
function throwable_decode(array $json): Throwable
71
{
72 6
    $properties = [
73 6
        'class' => 'string',
74 6
        'message' => 'string',
75
        'code' => 'integer',
76
        'file' => 'string',
77 6
        'line' => 'integer',
78
        'previous' => ['string', 'NULL'],
79
        'originalTrace' => 'array',
80
        'additionalProperties' => 'array',
81
    ];
82
83
    validate_array($json, $properties, NotAnEncodedThrowableException::class);
84
85
    $additionalProperties = $json['additionalProperties'];
86
    unset($json['additionalProperties']);
87
88
    if ($json['previous'] !== null) {
89
        $json['previous'] = throwable_json_decode($json['previous']);
90
    }
91
92
    $throwable = (new Instantiator())->instantiate($json['class']);
93
    assert($throwable instanceof Throwable);
94
    $class = new ReflectionClass($json['class']);
95
    foreach ($properties as $key => $type) {
96
        if (! $class->hasProperty($key)) {
97
            continue;
98
        }
99
100
        $property = new ReflectionProperty($json['class'], $key);
101
        $property->setAccessible(true);
102
103
        /**
104
         * @psalm-suppress PossiblyInvalidArrayOffset
105
         * @psalm-suppress InvalidArrayOffset
106
         */
107
        $property->setValue($throwable, $json[$key]);
108
        $property->setAccessible(false);
109
    }
110
111
    foreach ($additionalProperties as $key => $contents) {
112
        if (! $class->hasProperty($key)) {
113
            continue;
114
        }
115
116
        $property = new ReflectionProperty($json['class'], $key);
117
        $property->setAccessible(true);
118
        $property->setValue($throwable, unserialize($additionalProperties[$key]));
119
        $property->setAccessible(false);
120
    }
121
122
    return $throwable;
123
}
124