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 — php7.3 ( a4fbeb...9ddd99 )
by SignpostMarv
06:05
created

TypeParanoia::IsThingStrings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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