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 ( 405c3e...910b0e )
by SignpostMarv
04:10
created

TypeParanoia::MaybeRemapStringsToTrimmedStrings()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 20
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

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