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 ( d17a44...e59319 )
by SignpostMarv
06:30
created

TypeUtilities   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 25
eloc 76
dl 0
loc 210
ccs 70
cts 70
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A DaftObjectPublicSetters() 0 5 1
A CachePublicGettersAndSettersProperties() 0 18 5
A DaftObjectPublicOrProtectedGetters() 0 5 1
A DaftObjectPublicGetters() 0 5 1
A MethodNameFromProperty() 0 15 4
A HasMethod() 0 14 4
A CheckTypeDefinesOwnIdProperties() 0 21 3
A CachePublicGettersAndSetters() 0 22 3
A CheckTypeDefinesOwnIdPropertiesIsImplementation() 0 17 3
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 SUPPORTED_INVALID_LEADING_CHARACTERS = [
29
        '@',
30
    ];
31
32
    /**
33
    * @var array<string, array<string, bool>>
34
    *
35
    * @psalm-var array<class-string<DaftObject>, array<string, bool>>
36
    */
37
    private static $Getters = [];
38
39
    /**
40
    * @var array<string, array<int, string>>
41
    *
42
    * @psalm-var array<class-string<DaftObject>, array<int, string>>
43
    */
44
    private static $publicSetters = [];
45
46
    /**
47
    * @psalm-param class-string<DaftObject> $class
48
    *
49
    * @return array<int, string>
50
    */
51 122
    public static function DaftObjectPublicGetters(string $class) : array
52
    {
53 122
        static::CachePublicGettersAndSetters($class);
54
55 122
        return array_keys(array_filter(self::$Getters[$class]));
56
    }
57
58
    /**
59
    * @psalm-param class-string<DaftObject> $class
60
    *
61
    * @return array<int, string>
62
    */
63 2
    public static function DaftObjectPublicOrProtectedGetters(string $class) : array
64
    {
65 2
        static::CachePublicGettersAndSetters($class);
66
67 2
        return array_keys(self::$Getters[$class]);
68
    }
69
70
    /**
71
    * @psalm-param class-string<DaftObject> $class
72
    */
73 188
    public static function DaftObjectPublicSetters(string $class) : array
74
    {
75 188
        static::CachePublicGettersAndSetters($class);
76
77 188
        return self::$publicSetters[$class];
78
    }
79
80 268
    public static function MethodNameFromProperty(
81
        string $prop,
82
        bool $SetNotGet = self::BOOL_DEFAULT_SET_NOT_GET
83
    ) : string {
84
        if (
85 268
            in_array(
86 268
                mb_substr($prop, 0, 1),
87 268
                self::SUPPORTED_INVALID_LEADING_CHARACTERS,
88 268
                DefinitionAssistant::IN_ARRAY_STRICT_MODE
89
            )
90
        ) {
91 4
            return ($SetNotGet ? 'Alter' : 'Obtain') . ucfirst(mb_substr($prop, 1));
92
        }
93
94 264
        return ($SetNotGet ? 'Set' : 'Get') . ucfirst($prop);
95
    }
96
97
    /**
98
    * Checks if a type correctly defines it's own id.
99
    *
100
    * @psalm-param class-string<DaftObject> $class
101
    *
102
    * @throws ClassDoesNotImplementClassException if $class is not an implementation of DefinesOwnIdPropertiesInterface
103
    * @throws ClassMethodReturnHasZeroArrayCountException if $class::DaftObjectIdProperties() does not contain at least one property
104
    * @throws ClassMethodReturnIsNotArrayOfStringsException if $class::DaftObjectIdProperties() is not string[]
105
    * @throws UndefinedPropertyException if an id property is not in $class::DaftObjectIdProperties()
106
    */
107 494
    public static function CheckTypeDefinesOwnIdProperties(
108
        string $class,
109
        bool $throwIfNotImplementation = self::BOOL_DEFAULT_THROWIFNOTIMPLEMENTATION
110
    ) : void {
111
        if (
112 494
            is_a(
113 494
                $class,
114 494
                DefinesOwnIdPropertiesInterface::class,
115 494
                true
116
            )
117
        ) {
118
            /**
119
            * @psalm-var class-string<DefinesOwnIdPropertiesInterface>
120
            */
121 290
            $class = $class;
122
123 290
            static::CheckTypeDefinesOwnIdPropertiesIsImplementation($class);
124 210
        } elseif ($throwIfNotImplementation) {
125 2
            throw new ClassDoesNotImplementClassException(
126 2
                $class,
127 2
                DefinesOwnIdPropertiesInterface::class
128
            );
129
        }
130 488
    }
131
132 34
    protected static function HasMethod(
133
        string $class,
134
        string $property,
135
        bool $SetNotGet,
136
        bool $pub = self::BOOL_DEFAULT_EXPECTING_NON_PUBLIC_METHOD
137
    ) : bool {
138 34
        $method = static::MethodNameFromProperty($property, $SetNotGet);
139
140
        try {
141 34
            $ref = new ReflectionMethod($class, $method);
142
143 34
            return ($pub ? $ref->isPublic() : $ref->isProtected()) && false === $ref->isStatic();
144 20
        } catch (ReflectionException $e) {
145 20
            return false;
146
        }
147
    }
148
149
    /**
150
    * @psalm-param class-string<DaftObject> $class
151
    */
152 240
    protected static function CachePublicGettersAndSetters(string $class) : void
153
    {
154 240
        if (false === isset(self::$Getters[$class])) {
155 34
            self::$Getters[$class] = [];
156 34
            self::$publicSetters[$class] = [];
157
158
            if (
159 34
                is_a(
160 34
                    $class,
161 34
                    DefinesOwnIdPropertiesInterface::class,
162 34
                    true
163
                )
164
            ) {
165
                /**
166
                * @psalm-var class-string<DefinesOwnIdPropertiesInterface>
167
                */
168 16
                $class = $class;
169
170 16
                self::$Getters[$class]['id'] = self::BOOL_METHOD_IS_PUBLIC;
171
            }
172
173 34
            static::CachePublicGettersAndSettersProperties($class);
174
        }
175 240
    }
176
177
    /**
178
    * @psalm-param class-string<DaftObject> $class
179
    */
180 34
    protected static function CachePublicGettersAndSettersProperties(string $class) : void
181
    {
182
        foreach (
183 34
            DefinitionAssistant::ObtainExpectedProperties($class) as $prop
184
        ) {
185 34
            if (static::HasMethod($class, $prop, self::BOOL_EXPECTING_GETTER)) {
186 30
                self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_PUBLIC;
187 6
            } elseif (static::HasMethod(
188 6
                $class,
189 6
                $prop,
190 6
                self::BOOL_EXPECTING_GETTER,
191 6
                self::BOOL_EXPECTING_NON_PUBLIC_METHOD
192
            )) {
193 2
                self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_NON_PUBLIC;
194
            }
195
196 34
            if (static::HasMethod($class, $prop, true)) {
197 34
                self::$publicSetters[$class][] = $prop;
198
            }
199
        }
200 34
    }
201
202
    /**
203
    * @psalm-param class-string<DefinesOwnIdPropertiesInterface> $class
204
    */
205 290
    protected static function CheckTypeDefinesOwnIdPropertiesIsImplementation(
206
        string $class
207
    ) : void {
208 290
        $properties = $class::DaftObjectIdProperties();
209
210 290
        if (count($properties) < 1) {
211 2
            throw new ClassMethodReturnHasZeroArrayCountException(
212 2
                $class,
213 2
                'DaftObjectIdProperties'
214
            );
215
        } elseif (
216 288
            count($properties) !==
217 288
            count(array_filter($properties, 'is_string'))
218
        ) {
219 2
            throw new ClassMethodReturnIsNotArrayOfStringsException(
220 2
                $class,
221 2
                'DaftObjectIdProperties'
222
            );
223
        }
224 286
    }
225
}
226