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 — php7.3 ( a4fbeb...9ddd99 )
by SignpostMarv
06:05
created

TypeUtilities::CountMaybeArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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