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 ( da55b1...7ac43b )
by SignpostMarv
04:31
created

TypeUtilities::ExpectRetrievedValueIsFloatish()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 3
dl 0
loc 18
ccs 1
cts 1
cp 1
crap 4
rs 10
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 890
    public static function MethodNameFromProperty(
85
        string $prop,
86
        bool $SetNotGet = self::BOOL_DEFAULT_SET_NOT_GET
87
    ) : string {
88
        if (
89 890
            in_array(
90 890
                mb_substr($prop, 0, 1),
91 890
                self::SUPPORTED_INVALID_LEADING_CHARACTERS,
92 890
                DefinitionAssistant::IN_ARRAY_STRICT_MODE
93
            )
94
        ) {
95 69
            return ($SetNotGet ? 'Alter' : 'Obtain') . ucfirst(mb_substr($prop, 1));
96
        }
97
98 878
        return ($SetNotGet ? 'Set' : 'Get') . ucfirst($prop);
99
    }
100
101 40
    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 40
        $method = static::MethodNameFromProperty($property, $SetNotGet);
108
109
        try {
110 40
            $ref = new ReflectionMethod($class, $method);
111
112
            return
113 36
                ($pub ? $ref->isPublic() : $ref->isProtected()) &&
114 36
                self::BOOL_METHOD_IS_NOT_STATIC === $ref->isStatic();
115 20
        } catch (ReflectionException $e) {
116 20
            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 40
    public static function ExpectRetrievedValueIsString(
126
        string $property,
127
        $value,
128 40
        string $class_name
129 40
    ) : string {
130
        if ( ! is_string($value)) {
131
            throw Exceptions\Factory::PropertyValueNotOfExpectedTypeException(
132
                $class_name,
133
                $property,
134 40
                'string'
135
            );
136
        }
137
138 40
        return $value;
139
    }
140 40
141
    /**
142 40
    * @param scalar|array|object|null $value
143
    *
144
    * @psalm-param class-string<DaftObject> $class_name
145
    */
146
    public static function ExpectRetrievedValueIsArray(
147 40
        string $property,
148
        $value,
149
        string $class_name
150
    ) : array {
151 40
        if ( ! is_array($value)) {
152 32
            throw Exceptions\Factory::PropertyValueNotOfExpectedTypeException(
153 8
                $class_name,
154 6
                $property,
155 4
                'array'
156 8
            );
157 8
        }
158
159 4
        return $value;
160
    }
161
162 40
    /**
163 24
    * @param scalar|array|object|null $value
164
    *
165 40
    * @psalm-param class-string<DaftObject> $class_name
166
    */
167
    public static function ExpectRetrievedValueIsIntish(
168
        string $property,
169
        $value,
170 892
        string $class_name
171
    ) : int {
172 892
        if (is_string($value) && ctype_digit($value)) {
173 40
            $value = (int) $value;
174 40
        }
175
176
        if ( ! is_int($value)) {
177 40
            throw Exceptions\Factory::PropertyValueNotOfExpectedTypeException(
178 29
                $class_name,
179 40
                $property,
180 40
                'int'
181
            );
182
        }
183 16
184
        return $value;
185
    }
186
187
    /**
188
    * @param scalar|array|object|null $value
189 40
    *
190
    * @psalm-param class-string<DaftObject> $class_name
191 40
    */
192
    public static function ExpectRetrievedValueIsFloatish(
193 892
        string $property,
194
        $value,
195
        string $class_name
196
    ) : float {
197
        if (is_string($value) && is_numeric($value)) {
198
            $value = (float) $value;
199
        }
200
201
        if ( ! is_float($value)) {
202
            throw Exceptions\Factory::PropertyValueNotOfExpectedTypeException(
203
                $class_name,
204
                $property,
205
                'float'
206
            );
207
        }
208
209
        return $value;
210
    }
211
212
    /**
213
    * @param scalar|array|object|null $value
214
    *
215
    * @psalm-param class-string<DaftObject> $class_name
216
    */
217
    public static function ExpectRetrievedValueIsBoolish(
218
        string $property,
219
        $value,
220
        string $class_name
221
    ) : bool {
222
        if ('1' === $value || 1 === $value) {
223
            return true;
224
        } elseif ('0' === $value || 0 === $value) {
225
            return false;
226
        }
227
228
        if ( ! is_bool($value)) {
229
            throw Exceptions\Factory::PropertyValueNotOfExpectedTypeException(
230
                $class_name,
231
                $property,
232
                'bool'
233
            );
234
        }
235
236
        return $value;
237
    }
238
239
    /**
240
    * @template T as DaftObject
241
    *
242
    * @psalm-param class-string<T> $class
243
    */
244
    protected static function CachePublicGettersAndSettersProperties(string $class) : void
245
    {
246
        if (
247
            is_a($class, AbstractDaftObject::class, true) &&
248
            DefinitionAssistant::IsTypeUnregistered($class)
249
        ) {
250
            /**
251
            * @psalm-var class-string<T>
252
            */
253
            $class = DefinitionAssistant::RegisterAbstractDaftObjectType($class);
254
        }
255
256
        foreach (
257
            DefinitionAssistant::ObtainExpectedProperties($class) as $prop
258
        ) {
259
            static::CachePublicGettersAndSettersProperty($class, $prop);
260
        }
261
    }
262
263
    /**
264
    * @psalm-param class-string<DaftObject> $class
265
    */
266
    protected static function CachePublicGettersAndSettersProperty(
267
        string $class,
268
        string $prop
269
    ) : void {
270
        if (static::HasMethod($class, $prop, self::BOOL_EXPECTING_GETTER)) {
271
            self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_PUBLIC;
272
        } elseif (static::HasMethod(
273
            $class,
274
            $prop,
275
            self::BOOL_EXPECTING_GETTER,
276
            self::BOOL_EXPECTING_NON_PUBLIC_METHOD
277
        )) {
278
            self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_NON_PUBLIC;
279
        }
280
281
        if (static::HasMethod($class, $prop, self::BOOL_METHOD_IS_PUBLIC)) {
282
            self::$publicSetters[$class][] = $prop;
283
        }
284
    }
285
286
    /**
287
    * @psalm-param class-string<DaftObject> $class
288
    */
289
    private static function CachePublicGettersAndSetters(string $class) : void
290
    {
291
        if (false === isset(self::$Getters[$class])) {
292
            self::$Getters[$class] = [];
293
            self::$publicSetters[$class] = [];
294
295
            if (
296
                is_a(
297
                    $class,
298
                    DefinesOwnIdPropertiesInterface::class,
299
                    true
300
                )
301
            ) {
302
                self::$Getters[$class]['id'] = self::BOOL_METHOD_IS_PUBLIC;
303
            }
304
305
            /**
306
            * @psalm-var class-string<DaftObject>
307
            */
308
            $class = $class;
309
310
            static::CachePublicGettersAndSettersProperties($class);
311
        }
312
    }
313
}
314