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 ( 7ee225...04dd15 )
by SignpostMarv
06:22
created

WithAutoRegister()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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