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.
Completed
Push — master ( 9296df...c92e9b )
by SignpostMarv
06:35
created

TypeUtilities::MethodNameFromProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
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
    /**
15
    * @var array<string, array<string, bool>>
16
    */
17
    private static $Getters = [];
18
19
    /**
20
    * @var array<string, array<int, string>>
21
    */
22
    private static $publicSetters = [];
23
24 116
    public static function DaftObjectPublicGetters(string $class) : array
25
    {
26 116
        static::CachePublicGettersAndSetters($class);
27
28 116
        return array_keys(array_filter(self::$Getters[$class]));
29
    }
30
31 2
    public static function DaftObjectPublicOrProtectedGetters(string $class) : array
32
    {
33 2
        static::CachePublicGettersAndSetters($class);
34
35 2
        return array_keys(self::$Getters[$class]);
36
    }
37
38 166
    public static function DaftObjectPublicSetters(string $class) : array
39
    {
40 166
        static::CachePublicGettersAndSetters($class);
41
42 166
        return self::$publicSetters[$class];
43
    }
44
45 28
    public static function HasMethod(string $class, string $property, bool $SetNotGet, bool $public = true) : bool
46
    {
47 28
        $method = TypeUtilities::MethodNameFromProperty($property, $SetNotGet);
48
49
        try {
50 28
            $mRef = new ReflectionMethod($class, $method);
51
52 28
            return ($public ? $mRef->isPublic() : $mRef->isProtected()) && false === $mRef->isStatic();
53 8
        } catch (ReflectionException $e) {
54 8
            return false;
55
        }
56
    }
57
58 214
    public static function MethodNameFromProperty(string $prop, bool $SetNotGet = false) : string
59
    {
60 214
        return ($SetNotGet ? 'Set' : 'Get') . ucfirst($prop);
61
    }
62
63
    /**
64
    * Checks if a type correctly defines it's own id.
65
    *
66
    * @throws ClassDoesNotImplementClassException if $class is not an implementation of DefinesOwnIdPropertiesInterface
67
    * @throws ClassMethodReturnHasZeroArrayCountException if $class::DaftObjectIdProperties() does not contain at least one property
68
    * @throws ClassMethodReturnIsNotArrayOfStringsException if $class::DaftObjectIdProperties() is not string[]
69
    * @throws UndefinedPropertyException if an id property is not in $class::DaftObjectIdProperties()
70
    */
71 468
    public static function CheckTypeDefinesOwnIdProperties(
72
        string $class,
73
        bool $throwIfNotImplementation = false
74
    ) : void {
75 468
        if (is_a($class, DefinesOwnIdPropertiesInterface::class, true)) {
76 300
            self::CheckTypeDefinesOwnIdPropertiesIsImplementation($class);
77 174
        } elseif ($throwIfNotImplementation) {
78 2
            throw new ClassDoesNotImplementClassException(
79 2
                $class,
80 2
                DefinesOwnIdPropertiesInterface::class
81
            );
82
        }
83 464
    }
84
85 226
    final protected static function CachePublicGettersAndSetters(string $class) : void
86
    {
87 226
        if (false === isset(self::$Getters[$class])) {
88 28
            self::$Getters[$class] = [];
89 28
            self::$publicSetters[$class] = [];
90
91 28
            if (is_a($class, DefinesOwnIdPropertiesInterface::class, true)) {
92 16
                self::$Getters[$class]['id'] = true;
93
            }
94
95 28
            self::CachePublicGettersAndSettersProperties($class);
96
        }
97 226
    }
98
99 28
    final protected static function CachePublicGettersAndSettersProperties(string $class) : void
100
    {
101
        /**
102
        * @var string[] $props
103
        */
104 28
        $props = $class::DaftObjectProperties();
105
106 28
        foreach ($props as $prop) {
107 28
            if (TypeUtilities::HasMethod($class, $prop, false)) {
108 24
                self::$Getters[$class][$prop] = true;
109 6
            } elseif (TypeUtilities::HasMethod($class, $prop, false, false)) {
110 2
                self::$Getters[$class][$prop] = false;
111
            }
112
113 28
            if (TypeUtilities::HasMethod($class, $prop, true)) {
114 28
                self::$publicSetters[$class][] = $prop;
115
            }
116
        }
117 28
    }
118
119 300
    final protected static function CheckTypeDefinesOwnIdPropertiesIsImplementation(
120
        string $class
121
    ) : void {
122 300
        $properties = (array) $class::DaftObjectIdProperties();
123
124 300
        if (count($properties) < 1) {
125 2
            throw new ClassMethodReturnHasZeroArrayCountException(
126 2
                $class,
127 2
                'DaftObjectIdProperties'
128
            );
129 298
        } elseif (count($properties) !== count(array_filter($properties, 'is_string'))) {
130 2
            throw new ClassMethodReturnIsNotArrayOfStringsException(
131 2
                $class,
132 2
                'DaftObjectIdProperties'
133
            );
134
        }
135 296
    }
136
}
137