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 ( 0d3b72...5bd487 )
by SignpostMarv
02:53
created

TypeUtilities::CachePublicGettersAndSetters()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
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 10
    public static function ThrowIfNotJsonType(string $jsonType) : void
86
    {
87 10
        if (false === is_a($jsonType, DaftJson::class, true)) {
88 2
            throw new ClassDoesNotImplementClassException($jsonType, DaftJson::class);
89
        }
90 8
    }
91
92 216
    final protected static function CachePublicGettersAndSetters(string $class) : void
93
    {
94 216
        if (false === isset(self::$publicGetters[$class])) {
95 26
            self::$publicGetters[$class] = [];
96 26
            self::$publicSetters[$class] = [];
97
98 26
            if (is_a($class, DefinesOwnIdPropertiesInterface::class, true)) {
99 14
                self::$publicGetters[$class][] = 'id';
100
            }
101
102 26
            self::CachePublicGettersAndSettersProperties($class);
103
        }
104 216
    }
105
106 26
    final protected static function CachePublicGettersAndSettersProperties(string $class) : void
107
    {
108 26
        foreach ($class::DaftObjectProperties() as $prop) {
109 26
            if (TypeUtilities::HasPublicMethod($class, $prop, false)) {
110 22
                self::$publicGetters[$class][] = $prop;
111
            }
112
113 26
            if (TypeUtilities::HasPublicMethod($class, $prop, true)) {
114 26
                self::$publicSetters[$class][] = $prop;
115
            }
116
        }
117 26
    }
118
119 232
    final protected static function CheckTypeDefinesOwnIdPropertiesIsImplementation(
120
        string $class
121
    ) : void {
122 232
        $properties = $class::DaftObjectIdProperties();
123
124 232
        if (count($properties) < 1) {
125 2
            throw new ClassMethodReturnHasZeroArrayCountException(
126 2
                $class,
127 2
                'DaftObjectIdProperties'
128
            );
129 230
        } elseif (count($properties) !== count(array_filter($properties, 'is_string'))) {
130 2
            throw new ClassMethodReturnIsNotArrayOfStringsException(
131 2
                $class,
132 2
                'DaftObjectIdProperties'
133
            );
134
        }
135 228
    }
136
}
137