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 ( 225710...a5c6de )
by SignpostMarv
05:59
created

TypeUtilities::CheckTypeDefinesOwnIdPropertiesIsImplementation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 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
    private static function HasMethod(
98
        string $class,
99
        string $property,
100
        bool $SetNotGet,
101
        bool $pub = self::BOOL_DEFAULT_EXPECTING_NON_PUBLIC_METHOD
102
    ) : bool {
103
        $method = static::MethodNameFromProperty($property, $SetNotGet);
104
105
        try {
106
            $ref = new ReflectionMethod($class, $method);
107 488
108
            return ($pub ? $ref->isPublic() : $ref->isProtected()) && false === $ref->isStatic();
109
        } catch (ReflectionException $e) {
110
            return false;
111
        }
112 488
    }
113 488
114 488
    /**
115 488
    * @psalm-param class-string<DaftObject> $class
116
    */
117
    private static function CachePublicGettersAndSetters(string $class) : void
118
    {
119
        if (false === isset(self::$Getters[$class])) {
120
            self::$Getters[$class] = [];
121 284
            self::$publicSetters[$class] = [];
122
123 284
            if (
124 210
                is_a(
125 2
                    $class,
126 2
                    DefinesOwnIdPropertiesInterface::class,
127 2
                    true
128
                )
129
            ) {
130 482
                /**
131
                * @psalm-var class-string<DefinesOwnIdPropertiesInterface>
132 34
                */
133
                $class = $class;
134
135
                self::$Getters[$class]['id'] = self::BOOL_METHOD_IS_PUBLIC;
136
            }
137
138 34
            static::CachePublicGettersAndSettersProperties($class);
139
        }
140
    }
141 34
142
    /**
143 34
    * @psalm-param class-string<DaftObject> $class
144 20
    */
145 20
    private static function CachePublicGettersAndSettersProperties(string $class) : void
146
    {
147
        foreach (
148
            DefinitionAssistant::ObtainExpectedProperties($class) as $prop
149
        ) {
150
            if (static::HasMethod($class, $prop, self::BOOL_EXPECTING_GETTER)) {
151
                self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_PUBLIC;
152 240
            } elseif (static::HasMethod(
153
                $class,
154 240
                $prop,
155 34
                self::BOOL_EXPECTING_GETTER,
156 34
                self::BOOL_EXPECTING_NON_PUBLIC_METHOD
157
            )) {
158
                self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_NON_PUBLIC;
159 34
            }
160 34
161 34
            if (static::HasMethod($class, $prop, true)) {
162 34
                self::$publicSetters[$class][] = $prop;
163
            }
164
        }
165
    }
166
}
167