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.
Test Failed
Push — master ( 89a0dd...669559 )
by SignpostMarv
02:34
created

ThrowIfDaftObjectObjectNotDaftJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 15
rs 9.9332
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
/**
3
* @author SignpostMarv
4
*/
5
declare(strict_types=1);
6
7
namespace SignpostMarv\DaftObject;
8
9
use Closure;
10
use InvalidArgumentException;
11
12
class JsonTypeUtilities
13 188
{
14
    public static function ThrowIfNotDaftJson(string $class) : void
15 188
    {
16 152
        if (false === is_a($class, DaftJson::class, true)) {
17
            throw new DaftObjectNotDaftJsonBadMethodCallException($class);
18 36
        }
19
    }
20 10
21
    public static function ThrowIfNotJsonType(string $jsonType) : void
22 10
    {
23 2
        if (false === is_a($jsonType, DaftJson::class, true)) {
24
            throw new ClassDoesNotImplementClassException($jsonType, DaftJson::class);
25 8
        }
26
    }
27 22
28
    public static function MakeMapperThrowIfJsonDefNotValid(
29
        string $class,
30
        array $jsonDef,
31
        array $array
32
    ) : Closure {
33
        $mapper =
34
            /**
35
            * @return mixed
36
            */
37 20
            function (string $prop) use ($jsonDef, $array, $class) {
38 4
                if (isset($jsonDef[$prop]) && false === is_array($array[$prop])) {
39 4
                    static::ThrowBecauseArrayJsonTypeNotValid(
40
                        $class,
41 4
                        (string) $jsonDef[$prop],
42 4
                        $prop
43
                    );
44
                }
45
46 16
                return $array[$prop];
47 22
            };
48
49 22
        return $mapper;
50
    }
51
52 24
    public static function FilterThrowIfJsonDefNotValid(
53
        string $class,
54
        array $jsonProps,
55
        array $array
56
    ) : array {
57
        $filter = function (string $prop) use ($jsonProps, $array, $class) : bool {
58 24
            if (false === in_array($prop, $jsonProps, true)) {
59 2
                throw new PropertyNotJsonDecodableException($class, $prop);
60
            }
61
62 24
            return false === is_null($array[$prop]);
63 24
        };
64
65 24
        return array_filter($array, $filter, ARRAY_FILTER_USE_KEY);
66
    }
67
68 6
    public static function ArrayToJsonType(string $type, array $value, bool $writeAll) : DaftJson
69
    {
70 6
        self::ThrowIfNotJsonType($type);
71
72
        /**
73
        * @var DaftJson
74
        */
75 6
        $type = $type;
76
77 6
        return $type::DaftObjectFromJsonArray($value, $writeAll);
78
    }
79
80
    /**
81
    * @return array<int, DaftJson>|DaftJson
82
    */
83 10
    final public static function DaftJsonFromJsonType(
84
        string $jsonType,
85
        string $prop,
86
        array $propVal,
87
        bool $writeAll
88
    ) {
89 10
        if ('[]' === mb_substr($jsonType, -2)) {
90 6
            $jsonType = mb_substr($jsonType, 0, -2);
91
92 6
            self::ThrowIfNotJsonType($jsonType);
93
94 4
            return self::DaftObjectFromJsonTypeArray($jsonType, $prop, $propVal, $writeAll);
95
        }
96
97 4
        return JsonTypeUtilities::ArrayToJsonType($jsonType, $propVal, $writeAll);
98
    }
99
100
    /**
101
    * @param array<int|string, mixed> $array
102
    */
103 62
    public static function ThrowIfJsonDefNotValid(string $type, array $array) : array
104
    {
105 62
        self::ThrowIfNotDaftJson($type);
106 24
        $jsonProps = (array) $type::DaftObjectJsonPropertyNames();
107 24
        $array = JsonTypeUtilities::FilterThrowIfJsonDefNotValid($type, $jsonProps, $array);
108 22
        $jsonDef = (array) $type::DaftObjectJsonProperties();
109
110 22
        $keys = array_keys($array);
111
112 22
        /**
113 22
        * @var array<int|string, mixed>
114 22
        */
115
        $out = array_combine($keys, array_map(
116
            JsonTypeUtilities::MakeMapperThrowIfJsonDefNotValid($type, $jsonDef, $array),
117
            $keys
118
        ));
119
120
        return $out;
121
    }
122
123 4
    public static function ThrowIfDaftObjectObjectNotDaftJson(DaftObject $object) : DaftJson
124
    {
125
        if ( ! ($object instanceof DaftJson)) {
126
            throw new InvalidArgumentException(
127
                'Argument 1 passed to ' .
128
                __METHOD__ .
129 4
                ' must be an instance of ' .
130
                DaftJson::class .
131 4
                ', ' .
132
                get_class($object) .
133
                ' given.'
134
            );
135
        }
136 4
137 2
        return $object;
138
    }
139
140 2
    /**
141 4
    * @param mixed[] $propVal
142 4
    *
143
    * @return array<int, DaftJson>
144
    */
145
    protected static function DaftObjectFromJsonTypeArray(
146 4
        string $jsonType,
147
        string $prop,
148
        array $propVal,
149
        bool $writeAll
150
    ) : array {
151 4
        JsonTypeUtilities::ThrowIfNotJsonType($jsonType);
152 2
153
        return array_map(
154 2
            /**
155
            * @param mixed $val
156
            */
157
            function ($val) use ($jsonType, $writeAll, $prop) : DaftJson {
158
                if (false === is_array($val)) {
159
                    throw new PropertyNotJsonDecodableShouldBeArrayException($jsonType, $prop);
160
                }
161
162
                return JsonTypeUtilities::ArrayToJsonType($jsonType, $val, $writeAll);
163
            },
164
            array_values($propVal)
165
        );
166
    }
167
168
    private static function ThrowBecauseArrayJsonTypeNotValid(
169
        string $class,
170
        string $type,
171
        string $prop
172
    ) : void {
173
        if ('[]' === mb_substr($type, -2)) {
174
            throw new PropertyNotJsonDecodableShouldBeArrayException($class, $prop);
175
        }
176
        throw new PropertyNotJsonDecodableShouldBeArrayException($type, $prop);
177
    }
178
}
179