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 ( fae6e1...9e9455 )
by SignpostMarv
05:58
created

MaybeRemapStringsToTrimmedStrings()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 23
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 23
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 2
nop 3
crap 4
1
<?php
2
/**
3
* @author SignpostMarv
4
*/
5
declare(strict_types=1);
6
7
namespace SignpostMarv\DaftObject;
8
9
use Closure;
10
use InvalidArgumentException;
11
use SignpostMarv\DaftMagicPropertyAnalysis\DefinitionAssistant as Base;
12
13
/**
14
* @template-extends Base<DaftObject>
15
*/
16
class DefinitionAssistant extends Base
17
{
18
    const BOOL_EXPECTING_GETTER = false;
19
20
    const BOOL_EXPECTING_SETTER = true;
21
22
    const INT_ARRAY_INDEX_TYPE = 0;
23
24
    const INT_ARRAY_INDEX_GETTER = 1;
25
26
    const INT_ARRAY_INDEX_SETTER = 2;
27
28
    const IS_A_STRINGS = true;
29
30
    /**
31
    * @psalm-param class-string<AbstractDaftObject> $maybe
32
    */
33 36
    public static function RegisterAbstractDaftObjectType(string $maybe) : void
34
    {
35
        /**
36
        * @var array<int, string>
37
        */
38 36
        $props = $maybe::PROPERTIES;
39
40 36
        static::RegisterDaftObjectTypeFromTypeAndProps($maybe, ...$props);
41 36
    }
42
43
    /**
44
    * {@inheritdoc}
45
    *
46
    * @psalm-param class-string<DaftObject>|DaftObject $maybe
47
    */
48 36
    public static function ObtainExpectedProperties($maybe) : array
49
    {
50
        /**
51
        * @psalm-var class-string<DaftObject>
52
        */
53 36
        $maybe = is_object($maybe) ? get_class($maybe) : $maybe;
54
55 36
        if (static::IsTypeUnregistered($maybe)) {
56 34
            if (is_a($maybe, AbstractDaftObject::class, true)) {
57 34
                static::RegisterAbstractDaftObjectType($maybe);
58
            }
59
        }
60
61 36
        $maybe = self::MaybeRegisterAdditionalTypes($maybe);
62
63 36
        return parent::ObtainExpectedProperties($maybe);
64
    }
65
66
    /**
67
    * @psalm-param class-string<DaftObject> $type
68
    */
69 4
    public static function AutoRegisterType(string $type, string ...$properties) : void
70
    {
71 4
        static::RegisterDaftObjectTypeFromTypeAndProps($type, ...$properties);
72 4
    }
73
74
    /**
75
    * @psalm-return Closure(string):?string
76
    */
77 38
    public static function SetterOrGetterClosure(
78
        string $type,
79
        bool $SetNotGet,
80
        string ...$props
81
    ) : Closure {
82
        return function (string $property) use ($type, $props, $SetNotGet) : ? string {
83 4
            if (in_array($property, $props, self::IN_ARRAY_STRICT_MODE)) {
84
                /**
85
                * @var string
86
                */
87 4
                $method = TypeUtilities::MethodNameFromProperty($property, $SetNotGet);
88
89 4
                if (method_exists($type, $method)) {
90 4
                    return $method;
91
                }
92
            }
93
94 2
            return null;
95 38
        };
96
    }
97
98
    /**
99
    * @param mixed $value
100
    *
101
    * @return array<int, mixed> filtered $value
102
    */
103 14
    public static function MaybeThrowIfValueDoesNotMatchMultiTypedArray(
104
        bool $autoTrimStrings,
105
        bool $throwIfNotUnique,
106
        $value,
107
        string ...$types
108
    ) : array {
109 14
        if ( ! is_array($value)) {
110 2
            throw new InvalidArgumentException(
111
                'Argument 3 passed to ' .
112
                __METHOD__ .
113
                ' must be an array, ' .
114 2
                (is_object($value) ? get_class($value) : gettype($value)) .
115 2
                ' given!'
116
            );
117
        }
118
119 12
        return static::MaybeThrowIfValueDoesNotMatchMultiTypedArrayValueArray(
120 12
            $autoTrimStrings,
121 12
            $throwIfNotUnique,
122 12
            $value,
123 9
            ...$types
124
        );
125
    }
126
127
    /**
128
    * @psalm-param class-string<DaftObject> $type
129
    *
130
    * @psalm-return array{0:class-string<DaftObject>, 1:null|Closure(string):?string, 2:null|Closure(string):?string, 4:string}
131
    */
132 38
    private static function TypeAndGetterAndSetterClosureWithProps(
133
        string $type,
134
        string ...$props
135
    ) : array {
136
        /**
137
        * @psalm-var array{0:class-string<DaftObject>, 1:null|Closure(string):?string, 2:null|Closure(string):?string, 4:string}
138
        */
139 38
        $out = array_merge(
140
            [
141 38
                $type,
142 38
                static::SetterOrGetterClosure($type, self::BOOL_EXPECTING_GETTER, ...$props),
143 38
                static::SetterOrGetterClosure($type, self::BOOL_EXPECTING_SETTER, ...$props),
144
            ],
145 38
            $props
146
        );
147
148 38
        return $out;
149
    }
150
151
    /**
152
    * @psalm-param class-string<DaftObject> $maybe
153
    *
154
    * @psalm-return class-string<DaftObject>
155
    */
156 38
    private static function RegisterDaftObjectTypeFromTypeAndProps(
157
        string $maybe,
158
        string ...$props
159
    ) : string {
160 38
        $args = static::TypeAndGetterAndSetterClosureWithProps($maybe, ...$props);
161
162
        /**
163
        * @var array<int, string>
164
        */
165 38
        $props = array_slice($args, 3);
166
167 38
        static::RegisterType(
168 38
            $args[self::INT_ARRAY_INDEX_TYPE],
169 38
            $args[self::INT_ARRAY_INDEX_GETTER],
170 38
            $args[self::INT_ARRAY_INDEX_SETTER],
171 19
            ...$props
172
        );
173
174 38
        return self::MaybeRegisterAdditionalTypes($args[self::INT_ARRAY_INDEX_TYPE]);
175
    }
176
177
    /**
178
    * @psalm-param class-string<DaftObject> $maybe
179
    *
180
    * @psalm-return class-string<DaftObject>
181
    */
182 38
    private static function MaybeRegisterAdditionalTypes(string $maybe) : string
183
    {
184 38
        return array_reduce(
185 38
            array_filter(
186
                [
187 38
                    DefinesOwnArrayIdInterface::class,
188
                    DefinesOwnIntegerIdInterface::class,
189
                    DefinesOwnStringIdInterface::class,
190
                ],
191
                function (string $otherType) use ($maybe) : bool {
192 38
                    return $otherType !== $maybe;
193 38
                }
194
            ),
195
            /**
196
            * @psalm-param class-string<DaftObject> $maybe
197
            * @psolm-param class-string<DaftObject> $otherType
198
            */
199
            function (string $maybe, string $otherType) : string {
200
                /**
201
                * @psalm-var class-string<DaftObject>
202
                */
203 38
                $otherType = $otherType;
204
205 38
                if (self::IsTypeUnregistered($otherType)) {
206 4
                    self::RegisterDaftObjectTypeFromTypeAndProps($otherType, 'id');
207
                }
208
209 38
                return $maybe;
210 38
            },
211 38
            $maybe
212
        );
213
    }
214
215
    /**
216
    * @return array<int, mixed> filtered $value
217
    */
218 12
    private static function MaybeThrowIfValueDoesNotMatchMultiTypedArrayValueArray(
219
        bool $autoTrimStrings,
220
        bool $throwIfNotUnique,
221
        array $value,
222
        string ...$types
223
    ) : array {
224 12
        $value = static::MaybeThrowIfNotArrayIntKeys($value);
225 10
        $value = static::MaybeThrowIfValueArrayDoesNotMatchTypes($value, ...$types);
226 8
        $value = static::MaybeRemapStringsToTrimmedStrings($value, $autoTrimStrings, ...$types);
227
228 8
        $initialCount = count($value);
229
230 8
        $value = array_unique($value, SORT_REGULAR);
231
232 8
        if ($throwIfNotUnique && count($value) !== $initialCount) {
233 2
            throw new InvalidArgumentException(
234
                'Argument 3 passed to ' .
235
                __METHOD__ .
236 2
                ' contained non-unique values!'
237
            );
238
        }
239
240 6
        return array_values($value);
241
    }
242
243
    /**
244
    * @return array<int, mixed> filtered $value
245
    */
246 12
    private static function MaybeThrowIfNotArrayIntKeys(array $value) : array
247
    {
248 12
        $initialCount = count($value);
249
250
        /**
251
        * @var array<int, mixed>
252
        */
253 12
        $value = array_filter($value, 'is_int', ARRAY_FILTER_USE_KEY);
254
255 12
        if (count($value) !== $initialCount) {
256 2
            throw new InvalidArgumentException(
257
                'Argument 3 passed to ' .
258
                __METHOD__ .
259 2
                ' must be array<int, mixed>'
260
            );
261
        }
262
263 10
        return $value;
264
    }
265
266
    /**
267
    * @param array<int, mixed> $value
268
    *
269
    * @return array<int, mixed> filtered $value
270
    */
271 10
    private static function MaybeThrowIfValueArrayDoesNotMatchTypes(
272
        array $value,
273
        string ...$types
274
    ) : array {
275 10
        $initialCount = count($value);
276
277 10
        $value = array_filter(
278 10
            $value,
279
            /**
280
            * @param mixed $maybe
281
            */
282
            function ($maybe) use ($types) : bool {
283 10
                if (is_object($maybe)) {
284 8
                    foreach ($types as $maybeType) {
285 8
                        if (is_a($maybe, $maybeType)) {
286 8
                            return true;
287
                        }
288
                    }
289
290 2
                    return false;
291
                }
292
293 4
                return in_array(
294 4
                    gettype($maybe),
295 4
                    $types,
296 4
                    DefinitionAssistant::IN_ARRAY_STRICT_MODE
297
                );
298 10
            }
299
        );
300
301 10
        if (count($value) !== $initialCount) {
302 2
            throw new InvalidArgumentException(
303
                'Argument 3 passed to ' .
304
                __METHOD__ .
305 2
                ' contained values that did not match the provided types!'
306
            );
307
        }
308
309 8
        return $value;
310
    }
311
312
    /**
313
    * @param array<int, mixed> $value
314
    *
315
    * @return array<int, mixed>
316
    */
317 8
    private static function MaybeRemapStringsToTrimmedStrings(
318
        array $value,
319
        bool $autoTrimStrings,
320
        string ...$types
321
    ) : array {
322
        if (
323 8
            $autoTrimStrings &&
324 8
            in_array('string', $types, DefinitionAssistant::IN_ARRAY_STRICT_MODE)
325
        ) {
326 2
            $value = array_map(
327
                /**
328
                * @param mixed $maybe
329
                *
330
                * @return mixed
331
                */
332
                function ($maybe) {
333 2
                    return is_string($maybe) ? trim($maybe) : $maybe;
334 2
                },
335 2
                $value
336
            );
337
        }
338
339 8
        return $value;
340
    }
341
}
342