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.
Test Failed
Push — master ( 3406b9...925ec5 )
by SignpostMarv
06:30
created

TypeParanoia::StringOfIsThingStrings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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