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 ( bed791...1e55d9 )
by SignpostMarv
02:49
created

MaybeThrowIfValueDoesNotMatchMultiTypedArray()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

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