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 ( 44466c...0d3b72 )
by SignpostMarv
02:43
created

CachePublicGettersAndSettersProperties()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 5
nop 1
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 4
rs 9.2
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<int, string>>
16
    */
17
    private static $publicGetters = [];
18
19
    /**
20
    * @var array<string, array<int, string>>
21
    */
22
    private static $publicSetters = [];
23
24 108
    public static function DaftObjectPublicGetters(string $class) : array
25
    {
26 108
        static::CachePublicGettersAndSetters($class);
27
28 108
        return self::$publicGetters[$class];
29
    }
30
31 158
    public static function DaftObjectPublicSetters(string $class) : array
32
    {
33 158
        static::CachePublicGettersAndSetters($class);
34
35 158
        return self::$publicSetters[$class];
36
    }
37
38 164
    public static function ThrowIfNotDaftJson(string $class) : void
39
    {
40 164
        if (false === is_a($class, DaftJson::class, true)) {
41 128
            throw new DaftObjectNotDaftJsonBadMethodCallException($class);
42
        }
43 36
    }
44
45 26
    public static function HasPublicMethod(string $class, string $property, bool $SetNotGet) : bool
46
    {
47 26
        $method = TypeUtilities::MethodNameFromProperty($property, $SetNotGet);
48
49
        try {
50 26
            $mRef = new ReflectionMethod($class, $method);
51
52 26
            return $mRef->isPublic() && false === $mRef->isStatic();
53 8
        } catch (ReflectionException $e) {
54 8
            return false;
55
        }
56
    }
57
58 206
    public static function MethodNameFromProperty(string $prop, bool $SetNotGet = false) : string
59
    {
60 206
        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 376
    public static function CheckTypeDefinesOwnIdProperties(
72
        string $class,
73
        bool $throwIfNotImplementation = false
74
    ) : void {
75 376
        if (is_a($class, DefinesOwnIdPropertiesInterface::class, true)) {
76 232
            self::CheckTypeDefinesOwnIdPropertiesIsImplementation($class);
77 150
        } elseif ($throwIfNotImplementation) {
78 2
            throw new ClassDoesNotImplementClassException(
79 2
                $class,
80 2
                DefinesOwnIdPropertiesInterface::class
81
            );
82
        }
83 372
    }
84
85 216
    final protected static function CachePublicGettersAndSetters(string $class) : void
86
    {
87 216
        if (false === isset(self::$publicGetters[$class])) {
88 26
            self::$publicGetters[$class] = [];
89 26
            self::$publicSetters[$class] = [];
90
91 26
            if (is_a($class, DefinesOwnIdPropertiesInterface::class, true)) {
92 14
                self::$publicGetters[$class][] = 'id';
93
            }
94
95 26
            self::CachePublicGettersAndSettersProperties($class);
96
        }
97 216
    }
98
99 26
    final protected static function CachePublicGettersAndSettersProperties(string $class) : void
100
    {
101 26
        foreach ($class::DaftObjectProperties() as $prop) {
102 26
            if (TypeUtilities::HasPublicMethod($class, $prop, false)) {
103 22
                self::$publicGetters[$class][] = $prop;
104
            }
105
106 26
            if (TypeUtilities::HasPublicMethod($class, $prop, true)) {
107 26
                self::$publicSetters[$class][] = $prop;
108
            }
109
        }
110 26
    }
111
112 232
    final protected static function CheckTypeDefinesOwnIdPropertiesIsImplementation(
113
        string $class
114
    ) : void {
115 232
        $properties = $class::DaftObjectIdProperties();
116
117 232
        if (count($properties) < 1) {
118 2
            throw new ClassMethodReturnHasZeroArrayCountException(
119 2
                $class,
120 2
                'DaftObjectIdProperties'
121
            );
122 230
        } elseif (count($properties) !== count(array_filter($properties, 'is_string'))) {
123 2
            throw new ClassMethodReturnIsNotArrayOfStringsException(
124 2
                $class,
125 2
                'DaftObjectIdProperties'
126
            );
127
        }
128 228
    }
129
}
130