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 ( 1a4436...5d2a83 )
by SignpostMarv
02:54
created

TypeUtilities::ThrowIfNotJsonType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
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<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 26
    public static function HasPublicMethod(string $class, string $property, bool $SetNotGet) : bool
39
    {
40 26
        $method = TypeUtilities::MethodNameFromProperty($property, $SetNotGet);
41
42
        try {
43 26
            $mRef = new ReflectionMethod($class, $method);
44
45 26
            return $mRef->isPublic() && false === $mRef->isStatic();
46 8
        } catch (ReflectionException $e) {
47 8
            return false;
48
        }
49
    }
50
51 206
    public static function MethodNameFromProperty(string $prop, bool $SetNotGet = false) : string
52
    {
53 206
        return ($SetNotGet ? 'Set' : 'Get') . ucfirst($prop);
54
    }
55
56
    /**
57
    * Checks if a type correctly defines it's own id.
58
    *
59
    * @throws ClassDoesNotImplementClassException if $class is not an implementation of DefinesOwnIdPropertiesInterface
60
    * @throws ClassMethodReturnHasZeroArrayCountException if $class::DaftObjectIdProperties() does not contain at least one property
61
    * @throws ClassMethodReturnIsNotArrayOfStringsException if $class::DaftObjectIdProperties() is not string[]
62
    * @throws UndefinedPropertyException if an id property is not in $class::DaftObjectIdProperties()
63
    */
64 376
    public static function CheckTypeDefinesOwnIdProperties(
65
        string $class,
66
        bool $throwIfNotImplementation = false
67
    ) : void {
68 376
        if (is_a($class, DefinesOwnIdPropertiesInterface::class, true)) {
69 232
            self::CheckTypeDefinesOwnIdPropertiesIsImplementation($class);
70 150
        } elseif ($throwIfNotImplementation) {
71 2
            throw new ClassDoesNotImplementClassException(
72 2
                $class,
73 2
                DefinesOwnIdPropertiesInterface::class
74
            );
75
        }
76 372
    }
77
78 216
    final protected static function CachePublicGettersAndSetters(string $class) : void
79
    {
80 216
        if (false === isset(self::$publicGetters[$class])) {
81 26
            self::$publicGetters[$class] = [];
82 26
            self::$publicSetters[$class] = [];
83
84 26
            if (is_a($class, DefinesOwnIdPropertiesInterface::class, true)) {
85 14
                self::$publicGetters[$class][] = 'id';
86
            }
87
88 26
            self::CachePublicGettersAndSettersProperties($class);
89
        }
90 216
    }
91
92 26
    final protected static function CachePublicGettersAndSettersProperties(string $class) : void
93
    {
94 26
        foreach ($class::DaftObjectProperties() as $prop) {
95 26
            if (TypeUtilities::HasPublicMethod($class, $prop, false)) {
96 22
                self::$publicGetters[$class][] = $prop;
97
            }
98
99 26
            if (TypeUtilities::HasPublicMethod($class, $prop, true)) {
100 26
                self::$publicSetters[$class][] = $prop;
101
            }
102
        }
103 26
    }
104
105 232
    final protected static function CheckTypeDefinesOwnIdPropertiesIsImplementation(
106
        string $class
107
    ) : void {
108 232
        $properties = $class::DaftObjectIdProperties();
109
110 232
        if (count($properties) < 1) {
111 2
            throw new ClassMethodReturnHasZeroArrayCountException(
112 2
                $class,
113 2
                'DaftObjectIdProperties'
114
            );
115 230
        } elseif (count($properties) !== count(array_filter($properties, 'is_string'))) {
116 2
            throw new ClassMethodReturnIsNotArrayOfStringsException(
117 2
                $class,
118 2
                'DaftObjectIdProperties'
119
            );
120
        }
121 228
    }
122
}
123