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 ( 112aaa...868380 )
by SignpostMarv
02:15
created

AbstractDaftObject::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
* Base daft objects.
4
*
5
* @author SignpostMarv
6
*/
7
declare(strict_types=1);
8
9
namespace SignpostMarv\DaftObject;
10
11
use ReflectionClass;
12
use ReflectionMethod;
13
14
/**
15
* Base daft object.
16
*/
17
abstract class AbstractDaftObject implements DaftObject
18
{
19
    /**
20
    * List of properties that can be defined on an implementation.
21
    *
22
    * @var string[]
23
    */
24
    const PROPERTIES = [];
25
26
    /**
27
    * List of nullable properties that can be defined on an implementation.
28
    *
29
    * @var string[]
30
    */
31
    const NULLABLE_PROPERTIES = [];
32
33
    /**
34
    * List of exportable properties that can be defined on an implementation.
35
    *
36
    * @var string[]
37
    */
38
    const EXPORTABLE_PROPERTIES = [];
39
40
    /**
41
    * import/export definition for DaftJson.
42
    */
43
    const JSON_PROPERTIES = [];
44
45
    /**
46
    * @var string[][]
47
    */
48
    private static $publicGetters = [];
49
50
    /**
51
    * @var string[][]
52
    */
53
    private static $publicSetters = [];
54
55
    /**
56
    * Does some sanity checking.
57
    *
58
    * @see DefinesOwnIdPropertiesInterface
59
    * @see self::CheckTypeDefinesOwnIdProperties()
60
    */
61 188
    public function __construct()
62
    {
63 188
        self::CheckTypeDefinesOwnIdProperties(
64 188
            static::class,
65 188
            ($this instanceof DefinesOwnIdPropertiesInterface)
66
        );
67 186
    }
68
69
    /**
70
    * {@inheritdoc}
71
    */
72 41
    public function __get(string $property)
73
    {
74 41
        return $this->DoGetSet(
75 41
            $property,
76 41
            false
77
        );
78
    }
79
80
    /**
81
    * {@inheritdoc}
82
    */
83 79
    public function __set(string $property, $v)
84
    {
85 79
        return $this->DoGetSet(
86 79
            $property,
87 79
            true,
88 79
            $v
89
        );
90
    }
91
92
    /**
93
    * {@inheritdoc}
94
    *
95
    * @see static::NudgePropertyValue()
96
    */
97 20
    public function __unset(string $property) : void
98
    {
99 20
        $this->NudgePropertyValue($property, null);
100 18
    }
101
102
    /**
103
    * {@inheritdoc}
104
    */
105 31
    public function __debugInfo() : array
106
    {
107 31
        $out = [];
108 31
        $publicGetters = static::DaftObjectPublicGetters();
109 31
        foreach (static::DaftObjectExportableProperties() as $prop) {
110 30
            $expectedMethod = 'Get' . ucfirst($prop);
111
            if (
112 30
                $this->__isset($prop) &&
113 30
                in_array($prop, $publicGetters, true)
114
            ) {
115 30
                $out[$prop] = $this->$expectedMethod();
116
            }
117
        }
118
119 31
        return $out;
120
    }
121
122
    /**
123
    * List of properties that can be defined on an implementation.
124
    *
125
    * @return string[]
126
    */
127 135
    final public static function DaftObjectProperties() : array
128
    {
129 135
        return static::PROPERTIES;
130
    }
131
132
    /**
133
    * {@inheritdoc}
134
    */
135 53
    final public static function DaftObjectNullableProperties() : array
136
    {
137 53
        return static::NULLABLE_PROPERTIES;
138
    }
139
140
    /**
141
    * {@inheritdoc}
142
    */
143 65
    final public static function DaftObjectExportableProperties() : array
144
    {
145 65
        return static::EXPORTABLE_PROPERTIES;
146
    }
147
148
    /**
149
    * {@inheritdoc}
150
    */
151 108
    final public static function DaftObjectPublicGetters() : array
152
    {
153 108
        static::CachePublicGettersAndSetters();
154
155 108
        return self::$publicGetters[static::class];
156
    }
157
158
    /**
159
    * {@inheritdoc}
160
    */
161 79
    final public static function DaftObjectPublicSetters() : array
162
    {
163 79
        static::CachePublicGettersAndSetters();
164
165 79
        return self::$publicSetters[static::class];
166
    }
167
168
    /**
169
    * {@inheritdoc}
170
    */
171 34
    final public static function DaftObjectJsonProperties() : array
172
    {
173 34
        static::ThrowIfNotDaftJson();
174
175 18
        return static::JSON_PROPERTIES;
176
    }
177
178
    /**
179
    * {@inheritdoc}
180
    */
181 18
    final public static function DaftObjectJsonPropertyNames() : array
182
    {
183 18
        $out = [];
184
185 18
        foreach (static::DaftObjectJsonProperties() as $k => $prop) {
186 18
            if (is_string($k)) {
187 12
                $prop = $k;
188
            }
189
190 18
            $out[] = $prop;
191
        }
192
193 18
        return $out;
194
    }
195
196 82
    protected static function ThrowIfNotDaftJson() : void
197
    {
198 82
        if (false === is_a(static::class, DaftJson::class, true)) {
199 64
            throw new DaftObjectNotDaftJsonBadMethodCallException(
200 64
                static::class
201
            );
202
        }
203 18
    }
204
205 13
    final protected static function HasPublicMethod(
206
        ReflectionClass $classReflection,
207
        string $method
208
    ) : bool {
209
        if (
210 13
            $classReflection->hasMethod($method)
211
        ) {
212 13
            $methodReflection = new ReflectionMethod(
213 13
                static::class,
214 13
                $method
215
            );
216
217
            return
218 13
                $methodReflection->isPublic() &&
219 13
                false === $methodReflection->isStatic();
220
        }
221
222 4
        return false;
223
    }
224
225 108
    final protected static function CachePublicGettersAndSetters() : void
226
    {
227 108
        if (false === isset(self::$publicGetters[static::class])) {
228 13
            self::$publicGetters[static::class] = [];
229 13
            self::$publicSetters[static::class] = [];
230
231
            if (
232 13
                is_a(
233 13
                    static::class,
234 13
                    DefinesOwnIdPropertiesInterface::class,
235 13
                    true
236
                )
237
            ) {
238 7
                self::$publicGetters[static::class][] = 'id';
239
            }
240
241 13
            $classReflection = new ReflectionClass(static::class);
242
243 13
            foreach (static::DaftObjectProperties() as $property) {
244
                if (
245 13
                    static::HasPublicMethod(
246 13
                        $classReflection,
247 13
                        static::DaftObjectMethodNameFromProperty($property)
248
                    )
249
                ) {
250 11
                    self::$publicGetters[static::class][] = $property;
251
                }
252
253
                if (
254 13
                    static::HasPublicMethod(
255 13
                        $classReflection,
256 13
                        static::DaftObjectMethodNameFromProperty(
257 13
                            $property,
258 13
                            true
259
                        )
260
                    )
261
                ) {
262 13
                    self::$publicSetters[static::class][] = $property;
263
                }
264
            }
265
        }
266 108
    }
267
268
    /**
269
    * Nudge the state of a given property, marking it as dirty.
270
    *
271
    * @param string $property property being nudged
272
    * @param mixed $value value to nudge property with
273
    *
274
    * @throws UndefinedPropertyException if $property is not in static::DaftObjectProperties()
275
    * @throws PropertyNotNullableException if $property is not in static::DaftObjectNullableProperties()
276
    * @throws PropertyNotRewriteableException if class is write-once read-many and $property was already changed
277
    */
278
    abstract protected function NudgePropertyValue(
279
        string $property,
280
        $value
281
    ) : void;
282
283
    /**
284
    * Checks if a type correctly defines it's own id.
285
    *
286
    * @throws ClassDoesNotImplementClassException if $class is not an implementation of DefinesOwnIdPropertiesInterface
287
    * @throws ClassMethodReturnHasZeroArrayCountException if $class::DaftObjectIdProperties() does not contain at least one property
288
    * @throws ClassMethodReturnIsNotArrayOfStringsException if $class::DaftObjectIdProperties() is not string[]
289
    * @throws UndefinedPropertyException if an id property is not in $class::DaftObjectIdProperties()
290
    */
291 188
    final protected static function CheckTypeDefinesOwnIdProperties(
292
        string $class,
293
        bool $throwIfNotImplementation = false
294
    ) : void {
295 188
        $interfaceCheck = $class;
296
297 188
        if (is_a($interfaceCheck, DefinesOwnIdPropertiesInterface::class, true)) {
298 116
            $properties = $class::DaftObjectIdProperties();
299
300 116
            if (count($properties) < 1) {
301 1
                throw new ClassMethodReturnHasZeroArrayCountException(
302 1
                    $class,
303 1
                    'DaftObjectIdProperties'
304
                );
305
            }
306
307 115
            foreach ($properties as $property) {
308 115
                if (false === is_string($property)) {
309 1
                    throw new ClassMethodReturnIsNotArrayOfStringsException(
310 1
                        $class,
311 115
                        'DaftObjectIdProperties'
312
                    );
313
                }
314
            }
315 75
        } elseif ($throwIfNotImplementation) {
316 1
            throw new ClassDoesNotImplementClassException(
317 1
                $class,
318 1
                DefinesOwnIdPropertiesInterface::class
319
            );
320
        }
321 186
    }
322
323 108
    protected static function DaftObjectMethodNameFromProperty(
324
        string $property,
325
        bool $SetNotGet = false
326
    ) : string {
327 108
        return ($SetNotGet ? 'Set' : 'Get') . ucfirst($property);
328
    }
329
330
    /**
331
    * @param mixed $v
332
    *
333
    * @return mixed
334
    */
335 108
    protected function DoGetSet(
336
        string $property,
337
        bool $SetNotGet,
338
        $v = null
339
    ) {
340 108
        $expectedMethod = static::DaftObjectMethodNameFromProperty(
341 108
            $property,
342 108
            $SetNotGet
343
        );
344 108
        $thingers = static::DaftObjectPublicGetters();
345
346 108
        if ($SetNotGet) {
347 79
            $thingers = static::DaftObjectPublicSetters();
348
        }
349
350
        if (
351
            false === (
352 108
                in_array(
353 108
                    $property,
354 108
                    $thingers,
355 108
                    true
356
                )
357
            )
358
        ) {
359
            if (
360 5
                false === in_array(
361 5
                    $property,
362 5
                    static::DaftObjectProperties(),
363 5
                    true
364
                )
365
            ) {
366 3
                throw new UndefinedPropertyException(static::class, $property);
367 2
            } elseif ($SetNotGet) {
368 1
                throw new NotPublicSetterPropertyException(
369 1
                    static::class,
370 1
                    $property
371
                );
372
            }
373
374 1
            throw new NotPublicGetterPropertyException(
375 1
                static::class,
376 1
                $property
377
            );
378
        }
379
380 103
        return $this->$expectedMethod($v);
381
    }
382
}
383