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 ( c94bb6...1278e2 )
by SignpostMarv
02:03
created

ThrowIfNotDaftObjectIdPropertiesType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

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