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.
Passed
Push — master ( 04dd15...1f758d )
by SignpostMarv
06:31
created

TypeParanoia   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 277
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 31
eloc 93
dl 0
loc 277
ccs 96
cts 96
cp 1
rs 9.92
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A MaybeThrowIfNotArrayIntKeys() 0 18 2
A MaybeInMaybeArray() 0 5 1
A MaybeInArray() 0 3 1
A IsSubThingStrings() 0 3 1
A IsThingStrings() 0 3 1
A MaybeThrowIfValueDoesNotMatchMultiTypedArray() 0 21 3
A StringOfIsThingStrings() 0 7 2
A ThrowIfNotDaftObjectIdPropertiesType() 0 14 1
A MaybeThrowIfValueDoesNotMatchMultiTypedArrayValueArray() 0 23 3
A ThrowIfNotType() 0 23 6
A ThrowIfNotDaftObjectType() 0 14 1
A MaybeThrowIfValueArrayDoesNotMatchTypes() 0 35 5
A MaybeRemapStringsToTrimmedStrings() 0 20 4
1
<?php
2
/**
3
* @author SignpostMarv
4
*/
5
declare(strict_types=1);
6
7
namespace SignpostMarv\DaftObject;
8
9
use InvalidArgumentException;
10
11
class TypeParanoia extends TypeCertainty
12
{
13
    const INDEX_SECOND_ARG = 2;
14
15
    const INT_ARG_OFFSET = 5;
16
17
    /**
18
    * @param mixed $needle
19
    * @param mixed $haystack
20
    */
21 116
    public static function MaybeInMaybeArray($needle, $haystack) : bool
22
    {
23 116
        $haystack = self::EnsureArgumentIsArray($haystack, self::INDEX_SECOND_ARG, __METHOD__);
24
25 116
        return static::MaybeInArray($needle, $haystack);
26
    }
27
28
    /**
29
    * @param mixed $needle
30
    */
31 428
    public static function MaybeInArray($needle, array $haystack) : bool
32
    {
33 428
        return in_array($needle, $haystack, true);
34
    }
35
36
    /**
37
    * @tempalte T as class-string
38
    */
39 686
    public static function IsThingStrings(string $maybe, string $thing) : bool
40
    {
41 686
        return is_a($maybe, $thing, true);
42
    }
43
44
    /**
45
    * @template T
46
    *
47
    * @psalm-param class-string<DaftObject> $maybe
48
    * @psalm-param class-string<T> $thing
49
    *
50
    * @psalm-return class-string<T>
51
    */
52 206
    public static function StringOfIsThingStrings(string $maybe, string $thing) : string
53
    {
54 206
        if ( ! TypeParanoia::IsThingStrings($maybe, $thing)) {
55 168
            throw new DaftObjectNotDaftJsonBadMethodCallException($maybe);
56
        }
57
58 38
        return $maybe;
59
    }
60
61 2
    public static function IsSubThingStrings(string $maybe, string $thing) : bool
62
    {
63 2
        return is_subclass_of($maybe, $thing, true);
64
    }
65
66
    /**
67
    * @param mixed $value
68
    *
69
    * @return array<int, mixed> filtered $value
70
    */
71 14
    public static function MaybeThrowIfValueDoesNotMatchMultiTypedArray(
72
        bool $autoTrimStrings,
73
        bool $throwIfNotUnique,
74
        $value,
75
        string ...$types
76
    ) : array {
77 14
        if ( ! is_array($value)) {
78 2
            throw new InvalidArgumentException(
79
                'Argument 3 passed to ' .
80
                __METHOD__ .
81
                ' must be an array, ' .
82 2
                (is_object($value) ? get_class($value) : gettype($value)) .
83 2
                ' given!'
84
            );
85
        }
86
87 12
        return static::MaybeThrowIfValueDoesNotMatchMultiTypedArrayValueArray(
88 12
            $autoTrimStrings,
89 12
            $throwIfNotUnique,
90 12
            $value,
91 9
            ...$types
92
        );
93
    }
94
95
    /**
96
    * @param string|object $object
97
    *
98
    * @psalm-param class-string|object $object
99
    */
100 22
    public static function ThrowIfNotType(
101
        $object,
102
        int $argument,
103
        string $class,
104
        string $function,
105
        string ...$types
106
    ) : void {
107 22
        foreach ($types as $i => $type) {
108 22
            if ( ! interface_exists($type) && ! class_exists($type)) {
109 2
                throw new InvalidArgumentException(
110
                    'Argument ' .
111 2
                    (self::INT_ARG_OFFSET + $i) .
112 2
                    ' passed to ' .
113 2
                    __METHOD__ .
114 2
                    ' must be a class or interface!'
115
                );
116 20
            } elseif ( ! is_a($object, $type, is_string($object))) {
117 14
                throw new DaftObjectRepositoryTypeByClassMethodAndTypeException(
118 14
                    $argument,
119 14
                    $class,
120 14
                    $function,
121 14
                    $type,
122 20
                    is_string($object) ? $object : get_class($object)
123
                );
124
            }
125
        }
126 12
    }
127
128
    /**
129
    * @param string|object $object
130
    *
131
    * @psalm-param class-string|object $object
132
    */
133 12
    public static function ThrowIfNotDaftObjectType(
134
        $object,
135
        int $argument,
136
        string $class,
137
        string $function,
138
        string ...$types
139
    ) : void {
140 12
        static::ThrowIfNotType(
141 12
            $object,
142 12
            $argument,
143 12
            $class,
144 12
            $function,
145 12
            DaftObject::class,
146 6
            ...$types
147
        );
148 4
    }
149
150
    /**
151
    * @param mixed $object
152
    */
153 10
    public static function ThrowIfNotDaftObjectIdPropertiesType(
154
        $object,
155
        int $argument,
156
        string $class,
157
        string $function,
158
        string ...$types
159
    ) : void {
160 10
        static::ThrowIfNotDaftObjectType(
161 10
            $object,
162 10
            $argument,
163 10
            $class,
164 10
            $function,
165 10
            DefinesOwnIdPropertiesInterface::class,
166 5
            ...$types
167
        );
168 4
    }
169
170
    /**
171
    * @return array<int, mixed> filtered $value
172
    */
173 12
    private static function MaybeThrowIfValueDoesNotMatchMultiTypedArrayValueArray(
174
        bool $autoTrimStrings,
175
        bool $throwIfNotUnique,
176
        array $value,
177
        string ...$types
178
    ) : array {
179 12
        $value = static::MaybeThrowIfNotArrayIntKeys($value);
180 10
        $value = static::MaybeThrowIfValueArrayDoesNotMatchTypes($value, ...$types);
181 8
        $value = static::MaybeRemapStringsToTrimmedStrings($value, $autoTrimStrings, ...$types);
182
183 8
        $initialCount = count($value);
184
185 8
        $value = array_unique($value, SORT_REGULAR);
186
187 8
        if ($throwIfNotUnique && count($value) !== $initialCount) {
188 2
            throw new InvalidArgumentException(
189
                'Argument 3 passed to ' .
190
                __METHOD__ .
191 2
                ' contained non-unique values!'
192
            );
193
        }
194
195 6
        return array_values($value);
196
    }
197
198
    /**
199
    * @return array<int, mixed> filtered $value
200
    */
201 12
    private static function MaybeThrowIfNotArrayIntKeys(array $value) : array
202
    {
203 12
        $initialCount = count($value);
204
205
        /**
206
        * @var array<int, mixed>
207
        */
208 12
        $value = array_filter($value, 'is_int', ARRAY_FILTER_USE_KEY);
209
210 12
        if (count($value) !== $initialCount) {
211 2
            throw new InvalidArgumentException(
212
                'Argument 3 passed to ' .
213
                __METHOD__ .
214 2
                ' must be array<int, mixed>'
215
            );
216
        }
217
218 10
        return $value;
219
    }
220
221
    /**
222
    * @param array<int, mixed> $value
223
    *
224
    * @return array<int, mixed> filtered $value
225
    */
226 10
    private static function MaybeThrowIfValueArrayDoesNotMatchTypes(
227
        array $value,
228
        string ...$types
229
    ) : array {
230 10
        $initialCount = count($value);
231
232 10
        $value = array_filter(
233 10
            $value,
234
            /**
235
            * @param mixed $maybe
236
            */
237
            function ($maybe) use ($types) : bool {
238 10
                if (is_object($maybe)) {
239 8
                    foreach ($types as $maybeType) {
240 8
                        if (is_a($maybe, $maybeType)) {
241 8
                            return true;
242
                        }
243
                    }
244
245 2
                    return false;
246
                }
247
248 4
                return TypeParanoia::MaybeInArray(gettype($maybe), $types);
249 10
            }
250
        );
251
252 10
        if (count($value) !== $initialCount) {
253 2
            throw new InvalidArgumentException(
254
                'Argument 3 passed to ' .
255
                __METHOD__ .
256 2
                ' contained values that did not match the provided types!'
257
            );
258
        }
259
260 8
        return $value;
261
    }
262
263
    /**
264
    * @param array<int, mixed> $value
265
    *
266
    * @return array<int, mixed>
267
    */
268 8
    private static function MaybeRemapStringsToTrimmedStrings(
269
        array $value,
270
        bool $autoTrimStrings,
271
        string ...$types
272
    ) : array {
273 8
        if (TypeParanoia::MaybeInArray('string', $types) && $autoTrimStrings) {
274 2
            $value = array_map(
275
                /**
276
                * @param mixed $maybe
277
                *
278
                * @return mixed
279
                */
280
                function ($maybe) {
281 2
                    return is_string($maybe) ? trim($maybe) : $maybe;
282 2
                },
283 2
                $value
284
            );
285
        }
286
287 8
        return $value;
288
    }
289
}
290