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 — php7.0 ( a83e6e...c94bb6 )
by SignpostMarv
02:15
created

TypeParanoia::ThrowIfNotType()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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