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...b0f090 )
by SignpostMarv
03:44
created

TypeUtilities::MaybeThrowOnNudge()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 4
dl 0
loc 8
ccs 3
cts 3
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 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
    /**
130
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
131
    */
132 268
    public static function MaybeThrowForPropertyOnNudge(
133
        DaftObject $object,
134
        array $properties,
135
        string $property
136
    ) : void {
137 268
        $class = get_class($object);
138 268
        if (true !== in_array($property, $properties, true)) {
139 2
            throw new UndefinedPropertyException($class, $property);
140
        } elseif (
141 266
            ($object instanceof DaftObjectWorm) &&
142 266
            $object->DaftObjectWormPropertyWritten($property)
143
        ) {
144 100
            throw new PropertyNotRewriteableException($class, $property);
145
        }
146 266
    }
147
148
    /**
149
    * @param mixed $value
150
    *
151
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
152
    */
153 266
    public static function MaybeThrowOnNudge(
154
        string $class,
155
        string $property,
156
        $value,
157
        array $properties
158
    ) : void {
159 266
        if (true === is_null($value) && true !== in_array($property, $properties, true)) {
160 116
            throw new PropertyNotNullableException($class, $property);
161
        }
162 150
    }
163
164 216
    final protected static function CachePublicGettersAndSetters(string $class) : void
165
    {
166 216
        if (false === isset(self::$publicGetters[$class])) {
167 26
            self::$publicGetters[$class] = [];
168 26
            self::$publicSetters[$class] = [];
169
170 26
            if (is_a($class, DefinesOwnIdPropertiesInterface::class, true)) {
171 14
                self::$publicGetters[$class][] = 'id';
172
            }
173
174 26
            self::CachePublicGettersAndSettersProperties($class);
175
        }
176 216
    }
177
178 26
    final protected static function CachePublicGettersAndSettersProperties(string $class) : void
179
    {
180 26
        foreach ($class::DaftObjectProperties() as $prop) {
181 26
            if (TypeUtilities::HasPublicMethod($class, $prop, false)) {
182 22
                self::$publicGetters[$class][] = $prop;
183
            }
184
185 26
            if (TypeUtilities::HasPublicMethod($class, $prop, true)) {
186 26
                self::$publicSetters[$class][] = $prop;
187
            }
188
        }
189 26
    }
190
191 232
    final protected static function CheckTypeDefinesOwnIdPropertiesIsImplementation(
192
        string $class
193
    ) : void {
194 232
        $properties = $class::DaftObjectIdProperties();
195
196 232
        if (count($properties) < 1) {
197 2
            throw new ClassMethodReturnHasZeroArrayCountException(
198 2
                $class,
199 2
                'DaftObjectIdProperties'
200
            );
201 230
        } elseif (count($properties) !== count(array_filter($properties, 'is_string'))) {
202 2
            throw new ClassMethodReturnIsNotArrayOfStringsException(
203 2
                $class,
204 2
                'DaftObjectIdProperties'
205
            );
206
        }
207 228
    }
208
209 4
    private static function ThrowBecauseArrayJsonTypeNotValid(
210
        string $class,
211
        string $type,
212
        string $prop
213
    ) : void {
214 4
        if ('[]' === mb_substr($type, -2)) {
215 2
            throw new PropertyNotJsonDecodableShouldBeArrayException($class, $prop);
216
        }
217 2
        throw new PropertyNotJsonDecodableShouldBeArrayException($type, $prop);
218
    }
219
}
220