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 ( d78347...3406b9 )
by SignpostMarv
06:25
created

TypeParanoia::MaybeInArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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