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 ( c54964...3e25d4 )
by SignpostMarv
04:14
created

TypeParanoia   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 36
eloc 94
dl 0
loc 266
ccs 91
cts 91
cp 1
rs 9.52
c 0
b 0
f 0

14 Methods

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