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.
Passed
Push — master ( 5bd487...8b455a )
by SignpostMarv
02:47
created

TypeUtilities::ThrowBecauseArrayJsonTypeNotValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 3
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
rs 9.6666
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 Closure;
10
use ReflectionException;
11
use ReflectionMethod;
12
13
class TypeUtilities
14
{
15
    /**
16
    * @var array<string, array<int, string>>
17
    */
18
    private static $publicGetters = [];
19
20
    /**
21
    * @var array<string, array<int, string>>
22
    */
23
    private static $publicSetters = [];
24
25 108
    public static function DaftObjectPublicGetters(string $class) : array
26
    {
27 108
        static::CachePublicGettersAndSetters($class);
28
29 108
        return self::$publicGetters[$class];
30
    }
31
32 158
    public static function DaftObjectPublicSetters(string $class) : array
33
    {
34 158
        static::CachePublicGettersAndSetters($class);
35
36 158
        return self::$publicSetters[$class];
37
    }
38
39 164
    public static function ThrowIfNotDaftJson(string $class) : void
40
    {
41 164
        if (false === is_a($class, DaftJson::class, true)) {
42 128
            throw new DaftObjectNotDaftJsonBadMethodCallException($class);
43
        }
44 36
    }
45
46 26
    public static function HasPublicMethod(string $class, string $property, bool $SetNotGet) : bool
47
    {
48 26
        $method = TypeUtilities::MethodNameFromProperty($property, $SetNotGet);
49
50
        try {
51 26
            $mRef = new ReflectionMethod($class, $method);
52
53 26
            return $mRef->isPublic() && false === $mRef->isStatic();
54 8
        } catch (ReflectionException $e) {
55 8
            return false;
56
        }
57
    }
58
59 206
    public static function MethodNameFromProperty(string $prop, bool $SetNotGet = false) : string
60
    {
61 206
        return ($SetNotGet ? 'Set' : 'Get') . ucfirst($prop);
62
    }
63
64
    /**
65
    * Checks if a type correctly defines it's own id.
66
    *
67
    * @throws ClassDoesNotImplementClassException if $class is not an implementation of DefinesOwnIdPropertiesInterface
68
    * @throws ClassMethodReturnHasZeroArrayCountException if $class::DaftObjectIdProperties() does not contain at least one property
69
    * @throws ClassMethodReturnIsNotArrayOfStringsException if $class::DaftObjectIdProperties() is not string[]
70
    * @throws UndefinedPropertyException if an id property is not in $class::DaftObjectIdProperties()
71
    */
72 376
    public static function CheckTypeDefinesOwnIdProperties(
73
        string $class,
74
        bool $throwIfNotImplementation = false
75
    ) : void {
76 376
        if (is_a($class, DefinesOwnIdPropertiesInterface::class, true)) {
77 232
            self::CheckTypeDefinesOwnIdPropertiesIsImplementation($class);
78 150
        } elseif ($throwIfNotImplementation) {
79 2
            throw new ClassDoesNotImplementClassException(
80 2
                $class,
81 2
                DefinesOwnIdPropertiesInterface::class
82
            );
83
        }
84 372
    }
85
86 10
    public static function ThrowIfNotJsonType(string $jsonType) : void
87
    {
88 10
        if (false === is_a($jsonType, DaftJson::class, true)) {
89 2
            throw new ClassDoesNotImplementClassException($jsonType, DaftJson::class);
90
        }
91 8
    }
92
93 22
    public static function MakeMapperThrowIfJsonDefNotValid(
94
        string $class,
95
        array $jsonDef,
96
        array $array
97
    ) : Closure {
98
        $mapper =
99
            /**
100
            * @return mixed
101
            */
102
            function (string $prop) use ($jsonDef, $array, $class) {
103 20
                if (isset($jsonDef[$prop]) && false === is_array($array[$prop])) {
104 4
                    static::ThrowBecauseArrayJsonTypeNotValid($class, $jsonDef[$prop], $prop);
105
                }
106
107 16
                return $array[$prop];
108 22
            };
109
110 22
        return $mapper;
111
    }
112
113 216
    final protected static function CachePublicGettersAndSetters(string $class) : void
114
    {
115 216
        if (false === isset(self::$publicGetters[$class])) {
116 26
            self::$publicGetters[$class] = [];
117 26
            self::$publicSetters[$class] = [];
118
119 26
            if (is_a($class, DefinesOwnIdPropertiesInterface::class, true)) {
120 14
                self::$publicGetters[$class][] = 'id';
121
            }
122
123 26
            self::CachePublicGettersAndSettersProperties($class);
124
        }
125 216
    }
126
127 26
    final protected static function CachePublicGettersAndSettersProperties(string $class) : void
128
    {
129 26
        foreach ($class::DaftObjectProperties() as $prop) {
130 26
            if (TypeUtilities::HasPublicMethod($class, $prop, false)) {
131 22
                self::$publicGetters[$class][] = $prop;
132
            }
133
134 26
            if (TypeUtilities::HasPublicMethod($class, $prop, true)) {
135 26
                self::$publicSetters[$class][] = $prop;
136
            }
137
        }
138 26
    }
139
140 232
    final protected static function CheckTypeDefinesOwnIdPropertiesIsImplementation(
141
        string $class
142
    ) : void {
143 232
        $properties = $class::DaftObjectIdProperties();
144
145 232
        if (count($properties) < 1) {
146 2
            throw new ClassMethodReturnHasZeroArrayCountException(
147 2
                $class,
148 2
                'DaftObjectIdProperties'
149
            );
150 230
        } elseif (count($properties) !== count(array_filter($properties, 'is_string'))) {
151 2
            throw new ClassMethodReturnIsNotArrayOfStringsException(
152 2
                $class,
153 2
                'DaftObjectIdProperties'
154
            );
155
        }
156 228
    }
157
158 4
    private static function ThrowBecauseArrayJsonTypeNotValid(
159
        string $class,
160
        string $type,
161
        string $prop
162
    ) : void {
163 4
        if ('[]' === mb_substr($type, -2)) {
164 2
            throw new PropertyNotJsonDecodableShouldBeArrayException($class, $prop);
165
        }
166 2
        throw new PropertyNotJsonDecodableShouldBeArrayException($type, $prop);
167
    }
168
}
169