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

TypeUtilities::ThrowIfNotDaftJson()   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 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 24
    public static function FilterThrowIfJsonDefNotValid(
114
        string $class,
115
        array $jsonProps,
116
        array $array
117
    ) : array {
118
        $filter = function (string $prop) use ($jsonProps, $array, $class) : bool {
119 24
            if (false === in_array($prop, $jsonProps, true)) {
120 2
                throw new PropertyNotJsonDecodableException($class, $prop);
121
            }
122
123 24
            return false === is_null($array[$prop]);
124 24
        };
125
126 24
        return array_filter($array, $filter, ARRAY_FILTER_USE_KEY);
127
    }
128
129 216
    final protected static function CachePublicGettersAndSetters(string $class) : void
130
    {
131 216
        if (false === isset(self::$publicGetters[$class])) {
132 26
            self::$publicGetters[$class] = [];
133 26
            self::$publicSetters[$class] = [];
134
135 26
            if (is_a($class, DefinesOwnIdPropertiesInterface::class, true)) {
136 14
                self::$publicGetters[$class][] = 'id';
137
            }
138
139 26
            self::CachePublicGettersAndSettersProperties($class);
140
        }
141 216
    }
142
143 26
    final protected static function CachePublicGettersAndSettersProperties(string $class) : void
144
    {
145 26
        foreach ($class::DaftObjectProperties() as $prop) {
146 26
            if (TypeUtilities::HasPublicMethod($class, $prop, false)) {
147 22
                self::$publicGetters[$class][] = $prop;
148
            }
149
150 26
            if (TypeUtilities::HasPublicMethod($class, $prop, true)) {
151 26
                self::$publicSetters[$class][] = $prop;
152
            }
153
        }
154 26
    }
155
156 232
    final protected static function CheckTypeDefinesOwnIdPropertiesIsImplementation(
157
        string $class
158
    ) : void {
159 232
        $properties = $class::DaftObjectIdProperties();
160
161 232
        if (count($properties) < 1) {
162 2
            throw new ClassMethodReturnHasZeroArrayCountException(
163 2
                $class,
164 2
                'DaftObjectIdProperties'
165
            );
166 230
        } elseif (count($properties) !== count(array_filter($properties, 'is_string'))) {
167 2
            throw new ClassMethodReturnIsNotArrayOfStringsException(
168 2
                $class,
169 2
                'DaftObjectIdProperties'
170
            );
171
        }
172 228
    }
173
174 4
    private static function ThrowBecauseArrayJsonTypeNotValid(
175
        string $class,
176
        string $type,
177
        string $prop
178
    ) : void {
179 4
        if ('[]' === mb_substr($type, -2)) {
180 2
            throw new PropertyNotJsonDecodableShouldBeArrayException($class, $prop);
181
        }
182 2
        throw new PropertyNotJsonDecodableShouldBeArrayException($type, $prop);
183
    }
184
}
185