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 ( 7ac43b...fdcfe6 )
by SignpostMarv
05:29
created

TypeUtilities::ExpectRetrievedValueIsBoolish()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 4
nop 3
dl 0
loc 20
ccs 11
cts 11
cp 1
crap 6
rs 9.2222
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 ReflectionException;
10
use ReflectionMethod;
11
12
class TypeUtilities
13
{
14
    const BOOL_EXPECTING_NON_PUBLIC_METHOD = false;
15
16
    const BOOL_EXPECTING_GETTER = false;
17
18
    const BOOL_DEFAULT_THROWIFNOTIMPLEMENTATION = false;
19
20
    const BOOL_DEFAULT_EXPECTING_NON_PUBLIC_METHOD = true;
21
22
    const BOOL_METHOD_IS_PUBLIC = true;
23
24
    const BOOL_METHOD_IS_NON_PUBLIC = false;
25
26
    const BOOL_DEFAULT_SET_NOT_GET = false;
27
28
    const BOOL_DOES_NOT_HAVE_METHOD = false;
29
30
    const SUPPORTED_INVALID_LEADING_CHARACTERS = [
31
        '@',
32
    ];
33
34
    const BOOL_METHOD_IS_NOT_STATIC = false;
35
36
    /**
37
    * @var array<string, array<string, bool>>
38
    *
39
    * @psalm-var array<class-string<DaftObject>, array<string, bool>>
40
    */
41
    private static $Getters = [];
42
43
    /**
44
    * @var array<string, array<int, string>>
45
    *
46
    * @psalm-var array<class-string<DaftObject>, array<int, string>>
47
    */
48
    private static $publicSetters = [];
49
50
    /**
51
    * @psalm-param class-string<DaftObject> $class
52
    *
53
    * @return array<int, string>
54
    */
55 556
    public static function DaftObjectPublicGetters(string $class) : array
56
    {
57 556
        static::CachePublicGettersAndSetters($class);
58
59 556
        return array_keys(array_filter(self::$Getters[$class]));
60
    }
61
62
    /**
63
    * @psalm-param class-string<DaftObject> $class
64
    *
65
    * @return array<int, string>
66
    */
67 76
    public static function DaftObjectPublicOrProtectedGetters(string $class) : array
68
    {
69 76
        static::CachePublicGettersAndSetters($class);
70
71 76
        return array_keys(self::$Getters[$class]);
72
    }
73
74
    /**
75
    * @psalm-param class-string<DaftObject> $class
76
    */
77 368
    public static function DaftObjectPublicSetters(string $class) : array
78
    {
79 368
        static::CachePublicGettersAndSetters($class);
80
81 368
        return self::$publicSetters[$class];
82
    }
83
84 901
    public static function MethodNameFromProperty(
85
        string $prop,
86
        bool $SetNotGet = self::BOOL_DEFAULT_SET_NOT_GET
87
    ) : string {
88
        if (
89 901
            in_array(
90 901
                mb_substr($prop, 0, 1),
91 901
                self::SUPPORTED_INVALID_LEADING_CHARACTERS,
92 901
                DefinitionAssistant::IN_ARRAY_STRICT_MODE
93
            )
94
        ) {
95 69
            return ($SetNotGet ? 'Alter' : 'Obtain') . ucfirst(mb_substr($prop, 1));
96
        }
97
98 890
        return ($SetNotGet ? 'Set' : 'Get') . ucfirst($prop);
99
    }
100
101 44
    public static function HasMethod(
102
        string $class,
103
        string $property,
104
        bool $SetNotGet,
105
        bool $pub = self::BOOL_DEFAULT_EXPECTING_NON_PUBLIC_METHOD
106
    ) : bool {
107 44
        $method = static::MethodNameFromProperty($property, $SetNotGet);
108
109
        try {
110 44
            $ref = new ReflectionMethod($class, $method);
111
112
            return
113 40
                ($pub ? $ref->isPublic() : $ref->isProtected()) &&
114 40
                self::BOOL_METHOD_IS_NOT_STATIC === $ref->isStatic();
115 24
        } catch (ReflectionException $e) {
116 24
            return self::BOOL_DOES_NOT_HAVE_METHOD;
117
        }
118
    }
119
120
    /**
121
    * @param scalar|array|object|null $value
122
    *
123
    * @psalm-param class-string<DaftObject> $class_name
124
    */
125 80
    public static function ExpectRetrievedValueIsString(
126
        string $property,
127
        $value,
128
        string $class_name
129
    ) : string {
130 80
        if ( ! is_string($value)) {
131 4
            throw Exceptions\Factory::PropertyValueNotOfExpectedTypeException(
132 3
                $class_name,
133 2
                $property,
134 4
                'string'
135
            );
136
        }
137
138 76
        return $value;
139
    }
140
141
    /**
142
    * @param scalar|array|object|null $value
143
    *
144
    * @psalm-param class-string<DaftObject> $class_name
145
    */
146 8
    public static function ExpectRetrievedValueIsArray(
147
        string $property,
148
        $value,
149
        string $class_name
150
    ) : array {
151 8
        if ( ! is_array($value)) {
152 4
            throw Exceptions\Factory::PropertyValueNotOfExpectedTypeException(
153 3
                $class_name,
154 2
                $property,
155 4
                'array'
156
            );
157
        }
158
159 4
        return $value;
160
    }
161
162
    /**
163
    * @param scalar|array|object|null $value
164
    *
165
    * @psalm-param class-string<DaftObject> $class_name
166
    */
167 20
    public static function ExpectRetrievedValueIsIntish(
168
        string $property,
169
        $value,
170
        string $class_name
171
    ) : int {
172 20
        if (is_string($value) && ctype_digit($value)) {
173 4
            $value = (int) $value;
174
        }
175
176 20
        if ( ! is_int($value)) {
177 4
            throw Exceptions\Factory::PropertyValueNotOfExpectedTypeException(
178 3
                $class_name,
179 2
                $property,
180 4
                'int'
181
            );
182
        }
183
184 16
        return $value;
185
    }
186
187
    /**
188
    * @param scalar|array|object|null $value
189
    *
190
    * @psalm-param class-string<DaftObject> $class_name
191
    */
192 16
    public static function ExpectRetrievedValueIsFloatish(
193
        string $property,
194
        $value,
195
        string $class_name
196
    ) : float {
197 16
        if (is_string($value) && is_numeric($value)) {
198 4
            $value = (float) $value;
199
        }
200
201 16
        if ( ! is_float($value)) {
202 4
            throw Exceptions\Factory::PropertyValueNotOfExpectedTypeException(
203 3
                $class_name,
204 2
                $property,
205 4
                'float'
206
            );
207
        }
208
209 12
        return $value;
210
    }
211
212
    /**
213
    * @param scalar|array|object|null $value
214
    *
215
    * @psalm-param class-string<DaftObject> $class_name
216
    */
217 28
    public static function ExpectRetrievedValueIsBoolish(
218
        string $property,
219
        $value,
220
        string $class_name
221
    ) : bool {
222 28
        if ('1' === $value || 1 === $value) {
223 8
            return true;
224 20
        } elseif ('0' === $value || 0 === $value) {
225 8
            return false;
226
        }
227
228 12
        if ( ! is_bool($value)) {
229 4
            throw Exceptions\Factory::PropertyValueNotOfExpectedTypeException(
230 3
                $class_name,
231 2
                $property,
232 4
                'bool'
233
            );
234
        }
235
236 8
        return $value;
237
    }
238
239
    /**
240
    * @psalm-param class-string<DaftObject> $class_name
241
    *
242
    * @param scalar|array|object|null $value
243
    */
244 540
    public static function MaybeThrowOnNudge(
245
        string $class_name,
246
        string $property,
247
        $value
248
    ) : void {
249
        if (
250 540
            ! in_array(
251 405
                $property,
252 540
                $class_name::DaftObjectProperties(),
253 540
                DefinitionAssistant::IN_ARRAY_STRICT_MODE
254
            )
255
        ) {
256 8
            throw Exceptions\Factory::UndefinedPropertyException($class_name, $property);
257
        } elseif (
258 532
            true === is_null($value) &&
259 300
            ! in_array(
260 225
                $property,
261 300
                $class_name::DaftObjectNullableProperties(),
262 474
                DefinitionAssistant::IN_ARRAY_STRICT_MODE
263
            )
264
        ) {
265 208
            throw Exceptions\Factory::PropertyNotNullableException($class_name, $property);
266
        }
267 324
    }
268
269
    /**
270
    * @template T as DaftObject
271
    *
272
    * @psalm-param class-string<T> $class
273
    */
274 44
    protected static function CachePublicGettersAndSettersProperties(string $class) : void
275
    {
276
        if (
277 44
            is_a($class, AbstractDaftObject::class, true) &&
278 44
            DefinitionAssistant::IsTypeUnregistered($class)
279
        ) {
280
            /**
281
            * @psalm-var class-string<T>
282
            */
283 43
            $class = DefinitionAssistant::RegisterAbstractDaftObjectType($class);
284
        }
285
286
        foreach (
287 44
            DefinitionAssistant::ObtainExpectedProperties($class) as $prop
288
        ) {
289 44
            static::CachePublicGettersAndSettersProperty($class, $prop);
290
        }
291 44
    }
292
293
    /**
294
    * @psalm-param class-string<DaftObject> $class
295
    */
296 44
    protected static function CachePublicGettersAndSettersProperty(
297
        string $class,
298
        string $prop
299
    ) : void {
300 44
        if (static::HasMethod($class, $prop, self::BOOL_EXPECTING_GETTER)) {
301 36
            self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_PUBLIC;
302 8
        } elseif (static::HasMethod(
303 6
            $class,
304 4
            $prop,
305 8
            self::BOOL_EXPECTING_GETTER,
306 8
            self::BOOL_EXPECTING_NON_PUBLIC_METHOD
307
        )) {
308 4
            self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_NON_PUBLIC;
309
        }
310
311 44
        if (static::HasMethod($class, $prop, self::BOOL_METHOD_IS_PUBLIC)) {
312 24
            self::$publicSetters[$class][] = $prop;
313
        }
314 44
    }
315
316
    /**
317
    * @psalm-param class-string<DaftObject> $class
318
    */
319 892
    private static function CachePublicGettersAndSetters(string $class) : void
320
    {
321 892
        if (false === isset(self::$Getters[$class])) {
322 44
            self::$Getters[$class] = [];
323 44
            self::$publicSetters[$class] = [];
324
325
            if (
326 44
                is_a(
327 33
                    $class,
328 44
                    DefinesOwnIdPropertiesInterface::class,
329 44
                    true
330
                )
331
            ) {
332 20
                self::$Getters[$class]['id'] = self::BOOL_METHOD_IS_PUBLIC;
333
            }
334
335
            /**
336
            * @psalm-var class-string<DaftObject>
337
            */
338 44
            $class = $class;
339
340 44
            static::CachePublicGettersAndSettersProperties($class);
341
        }
342 892
    }
343
}
344