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 ( 1f758d...b0ea7f )
by SignpostMarv
06:30
created

JsonTypeUtilities   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Test Coverage

Coverage 98%

Importance

Changes 0
Metric Value
wmc 16
eloc 43
dl 0
loc 158
ccs 49
cts 50
cp 0.98
rs 10
c 0
b 0
f 0

9 Methods

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