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 ( bed791...1e55d9 )
by SignpostMarv
02:49
created

TypeUtilities::MaybeThrowIfValueDoesNotMatchMultiTypedArrayValueArray()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 2
nop 4
dl 0
loc 23
ccs 10
cts 10
cp 1
crap 3
rs 9.9
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 SUPPORTED_INVALID_LEADING_CHARACTERS = [
27
        '@',
28
    ];
29
30
    /**
31
    * @var array<string, array<string, bool>>
32
    */
33
    private static $Getters = [];
34
35
    /**
36
    * @var array<string, array<int, string>>
37
    */
38
    private static $publicSetters = [];
39
40 118
    public static function DaftObjectPublicGetters(string $class) : array
41
    {
42 118
        static::CachePublicGettersAndSetters($class);
43
44 118
        return array_keys(array_filter(self::$Getters[$class]));
45
    }
46
47 2
    public static function DaftObjectPublicOrProtectedGetters(string $class) : array
48
    {
49 2
        static::CachePublicGettersAndSetters($class);
50
51 2
        return array_keys(self::$Getters[$class]);
52
    }
53
54 186
    public static function DaftObjectPublicSetters(string $class) : array
55
    {
56 186
        static::CachePublicGettersAndSetters($class);
57
58 186
        return self::$publicSetters[$class];
59
    }
60
61 262
    public static function MethodNameFromProperty(string $prop, bool $SetNotGet = false) : string
62
    {
63 262
        if (TypeParanoia::MaybeInArray(mb_substr($prop, 0, 1), self::SUPPORTED_INVALID_LEADING_CHARACTERS)) {
64 2
            return ($SetNotGet ? 'Alter' : 'Obtain') . ucfirst(mb_substr($prop, 1));
65
        }
66
67 260
        return ($SetNotGet ? 'Set' : 'Get') . ucfirst($prop);
68
    }
69
70
    /**
71
    * Checks if a type correctly defines it's own id.
72
    *
73
    * @throws ClassDoesNotImplementClassException if $class is not an implementation of DefinesOwnIdPropertiesInterface
74
    * @throws ClassMethodReturnHasZeroArrayCountException if $class::DaftObjectIdProperties() does not contain at least one property
75
    * @throws ClassMethodReturnIsNotArrayOfStringsException if $class::DaftObjectIdProperties() is not string[]
76
    * @throws UndefinedPropertyException if an id property is not in $class::DaftObjectIdProperties()
77
    */
78 528
    public static function CheckTypeDefinesOwnIdProperties(
79
        string $class,
80
        bool $throwIfNotImplementation = self::BOOL_DEFAULT_THROWIFNOTIMPLEMENTATION
81
    ) : void {
82 528
        if (TypeParanoia::IsThingStrings($class, DefinesOwnIdPropertiesInterface::class)) {
83 314
            self::CheckTypeDefinesOwnIdPropertiesIsImplementation($class);
84 220
        } elseif ($throwIfNotImplementation) {
85 2
            throw new ClassDoesNotImplementClassException(
86 2
                $class,
87 2
                DefinesOwnIdPropertiesInterface::class
88
            );
89
        }
90 524
    }
91
92 32
    private static function HasMethod(
93
        string $class,
94
        string $property,
95
        bool $SetNotGet,
96
        bool $pub = self::BOOL_DEFAULT_EXPECTING_NON_PUBLIC_METHOD
97
    ) : bool {
98 32
        $method = static::MethodNameFromProperty($property, $SetNotGet);
99
100
        try {
101 32
            $ref = new ReflectionMethod($class, $method);
102
103 32
            return ($pub ? $ref->isPublic() : $ref->isProtected()) && false === $ref->isStatic();
104 8
        } catch (ReflectionException $e) {
105 8
            return false;
106
        }
107
    }
108
109
    /**
110
    * @param mixed $maybe
111
    */
112 312
    private static function FilterMaybeArray($maybe, callable $filter) : array
113
    {
114 312
        return array_filter(
115 312
            TypeParanoia::EnsureArgumentIsArray($maybe, TypeParanoia::INDEX_FIRST_ARG, __METHOD__),
116 312
            $filter
117
        );
118
    }
119
120
    /**
121
    * @param mixed $maybe
122
    */
123 314
    private static function CountMaybeArray($maybe) : int
124
    {
125 314
        return count(TypeParanoia::EnsureArgumentIsArray(
126 314
            $maybe,
127 314
            TypeParanoia::INDEX_FIRST_ARG,
128 314
            __METHOD__
129
        ));
130
    }
131
132 236
    private static function CachePublicGettersAndSetters(string $class) : void
133
    {
134 236
        if (false === isset(self::$Getters[$class])) {
135 32
            self::$Getters[$class] = [];
136 32
            self::$publicSetters[$class] = [];
137
138 32
            if (TypeParanoia::IsThingStrings($class, DefinesOwnIdPropertiesInterface::class)) {
139 16
                self::$Getters[$class]['id'] = true;
140
            }
141
142 32
            self::CachePublicGettersAndSettersProperties($class);
143
        }
144 236
    }
145
146
    /**
147
    * @psalm-suppress InvalidStringClass
148
    */
149 32
    private static function CachePublicGettersAndSettersProperties(string $class) : void
150
    {
151
        /**
152
        * @var string[]
153
        */
154 32
        $props = $class::DaftObjectProperties();
155
156 32
        foreach ($props as $prop) {
157 32
            if (static::HasMethod($class, $prop, self::BOOL_EXPECTING_GETTER)) {
158 28
                self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_PUBLIC;
159 6
            } elseif (static::HasMethod(
160 6
                $class,
161 6
                $prop,
162 6
                self::BOOL_EXPECTING_GETTER,
163 6
                self::BOOL_EXPECTING_NON_PUBLIC_METHOD
164
            )) {
165 2
                self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_NON_PUBLIC;
166
            }
167
168 32
            if (static::HasMethod($class, $prop, true)) {
169 32
                self::$publicSetters[$class][] = $prop;
170
            }
171
        }
172 32
    }
173
174
    /**
175
    * @psalm-suppress InvalidStringClass
176
    */
177 314
    private static function CheckTypeDefinesOwnIdPropertiesIsImplementation(
178
        string $class
179
    ) : void {
180
        /**
181
        * @var scalar|array|object|null
182
        */
183 314
        $properties = $class::DaftObjectIdProperties();
184
185 314
        if (self::CountMaybeArray($properties) < 1) {
186 2
            throw new ClassMethodReturnHasZeroArrayCountException(
187 2
                $class,
188 2
                'DaftObjectIdProperties'
189
            );
190
        } elseif (
191 312
            self::CountMaybeArray($properties) !==
192 312
            count(self::FilterMaybeArray($properties, 'is_string'))
193
        ) {
194 2
            throw new ClassMethodReturnIsNotArrayOfStringsException(
195 2
                $class,
196 2
                'DaftObjectIdProperties'
197
            );
198
        }
199 310
    }
200
}
201