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 ( 937611...8005d7 )
by SignpostMarv
01:48
created

AbstractDaftObject::DaftObjectJsonPropertyNames()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
crap 3
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 187
    public function __construct()
62
    {
63
        if (
64 187
            ($this instanceof DefinesOwnIdPropertiesInterface)
65
        ) {
66 116
            self::CheckTypeDefinesOwnIdProperties($this);
67
        }
68 185
    }
69
70
    /**
71
    * {@inheritdoc}
72
    */
73 40
    public function __get(string $property)
74
    {
75 40
        return $this->DoGetSet(
76 40
            $property,
77 40
            false,
78 40
            NotPublicGetterPropertyException::class
79
        );
80
    }
81
82
    /**
83
    * {@inheritdoc}
84
    */
85 79
    public function __set(string $property, $v)
86
    {
87 79
        return $this->DoGetSet(
88 79
            $property,
89 79
            true,
90 79
            NotPublicSetterPropertyException::class,
91 79
            $v
92
        );
93
    }
94
95
    /**
96
    * {@inheritdoc}
97
    *
98
    * @see static::NudgePropertyValue()
99
    */
100 20
    public function __unset(string $property) : void
101
    {
102 20
        $this->NudgePropertyValue($property, null);
103 18
    }
104
105
    /**
106
    * {@inheritdoc}
107
    */
108 31
    public function __debugInfo() : array
109
    {
110 31
        $out = [];
111 31
        $publicGetters = static::DaftObjectPublicGetters();
112 31
        foreach (static::DaftObjectExportableProperties() as $prop) {
113 30
            $expectedMethod = 'Get' . ucfirst($prop);
114
            if (
115 30
                $this->__isset($prop) &&
116 30
                in_array($prop, $publicGetters, true)
117
            ) {
118 24
                $out[$prop] = $this->$expectedMethod();
119
            }
120
        }
121
122 31
        return $out;
123
    }
124
125
    /**
126
    * List of properties that can be defined on an implementation.
127
    *
128
    * @return string[]
129
    */
130 221
    final public static function DaftObjectProperties() : array
131
    {
132 221
        return static::PROPERTIES;
133
    }
134
135
    /**
136
    * {@inheritdoc}
137
    */
138 65
    final public static function DaftObjectNullableProperties() : array
139
    {
140 65
        return static::NULLABLE_PROPERTIES;
141
    }
142
143
    /**
144
    * {@inheritdoc}
145
    */
146 65
    final public static function DaftObjectExportableProperties() : array
147
    {
148 65
        return static::EXPORTABLE_PROPERTIES;
149
    }
150
151
    /**
152
    * {@inheritdoc}
153
    */
154 104
    final public static function DaftObjectPublicGetters() : array
155
    {
156 104
        static::CachePublicGettersAndSetters();
157
158 104
        return self::$publicGetters[static::class];
159
    }
160
161
    /**
162
    * {@inheritdoc}
163
    */
164 77
    final public static function DaftObjectPublicSetters() : array
165
    {
166 77
        static::CachePublicGettersAndSetters();
167
168 77
        return self::$publicSetters[static::class];
169
    }
170
171
    /**
172
    * {@inheritdoc}
173
    */
174 34 View Code Duplication
    final public static function DaftObjectJsonProperties() : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176 34
        if (false === is_a(static::class, DaftJson::class, true)) {
177 16
            throw new DaftObjectNotDaftJsonBadMethodCallException(
178 16
                static::class
179
            );
180
        }
181
182 18
        return static::JSON_PROPERTIES;
183
    }
184
185
    /**
186
    * {@inheritdoc}
187
    */
188 18
    final public static function DaftObjectJsonPropertyNames() : array
189
    {
190 18
        $out = [];
191
192 18
        foreach (static::DaftObjectJsonProperties() as $k => $prop) {
193 18
            if (is_string($k)) {
194 12
                $prop = $k;
195
            }
196
197 18
            $out[] = $prop;
198
        }
199
200 18
        return $out;
201
    }
202
203 12
    final protected static function HasPublicMethod(
204
        ReflectionClass $classReflection,
205
        string $method
206
    ) : bool {
207
        if (
208 12
            $classReflection->hasMethod($method)
209
        ) {
210 12
            $methodReflection = new ReflectionMethod(
211 12
                static::class,
212 12
                $method
213
            );
214
215
            return
216 12
                $methodReflection->isPublic() &&
217 12
                false === $methodReflection->isStatic();
218
        }
219
220 4
        return false;
221
    }
222
223 104
    final protected static function CachePublicGettersAndSetters() : void
224
    {
225 104
        if (false === isset(self::$publicGetters[static::class])) {
226 12
            self::$publicGetters[static::class] = [];
227 12
            self::$publicSetters[static::class] = [];
228
229
            if (
230 12
                is_a(
231 12
                    static::class,
232 12
                    DefinesOwnIdPropertiesInterface::class,
233 12
                    true
234
                )
235
            ) {
236 7
                self::$publicGetters[static::class][] = 'id';
237
            }
238
239 12
            $classReflection = new ReflectionClass(static::class);
240
241 12
            foreach (static::DaftObjectProperties() as $property) {
242
                if (
243 12
                    static::HasPublicMethod(
244 12
                        $classReflection,
245 12
                        static::DaftObjectMethodNameFromProperty($property)
246
                    )
247
                ) {
248 10
                    self::$publicGetters[static::class][] = $property;
249
                }
250
251
                if (
252 12
                    static::HasPublicMethod(
253 12
                        $classReflection,
254 12
                        static::DaftObjectMethodNameFromProperty(
255 12
                            $property,
256 12
                            true
257
                        )
258
                    )
259
                ) {
260 10
                    self::$publicSetters[static::class][] = $property;
261
                }
262
            }
263
        }
264 104
    }
265
266
    /**
267
    * Nudge the state of a given property, marking it as dirty.
268
    *
269
    * @param string $property property being nudged
270
    * @param mixed $value value to nudge property with
271
    *
272
    * @throws UndefinedPropertyException if $property is not in static::DaftObjectProperties()
273
    * @throws PropertyNotNullableException if $property is not in static::DaftObjectNullableProperties()
274
    * @throws PropertyNotRewriteableException if class is write-once read-many and $property was already changed
275
    */
276
    abstract protected function NudgePropertyValue(
277
        string $property,
278
        $value
279
    ) : void;
280
281
    /**
282
    * Checks if a type correctly defines it's own id.
283
    *
284
    * @param DaftObject $object
285
    *
286
    * @throws ClassDoesNotImplementClassException if $object is not an implementation of DefinesOwnIdPropertiesInterface
287
    * @throws ClassMethodReturnHasZeroArrayCountException if $object::DaftObjectIdProperties() does not contain at least one property
288
    * @throws ClassMethodReturnIsNotArrayOfStringsException if $object::DaftObjectIdProperties() is not string[]
289
    * @throws UndefinedPropertyException if an id property is not in $object::DaftObjectIdProperties()
290
    */
291 117
    final protected static function CheckTypeDefinesOwnIdProperties(
292
        DaftObject $object
293
    ) : void {
294 117
        $class = get_class($object);
295 117
        if (false === ($object instanceof DefinesOwnIdPropertiesInterface)) {
296 1
            throw new ClassDoesNotImplementClassException(
297 1
                $class,
298 1
                DefinesOwnIdPropertiesInterface::class
299
            );
300
        }
301
302
        /**
303
        * @var DefinesOwnIdPropertiesInterface $object
304
        */
305 116
        $object = $object;
306
307 116
        $properties = $object::DaftObjectIdProperties();
308
309 116
        if (count($properties) < 1) {
310 1
            throw new ClassMethodReturnHasZeroArrayCountException(
311 1
                $class,
312 1
                'DaftObjectIdProperties'
313
            );
314
        }
315
316 115
        foreach ($properties as $property) {
317 115
            if (false === is_string($property)) {
318 1
                throw new ClassMethodReturnIsNotArrayOfStringsException(
319 1
                    $class,
320 1
                    'DaftObjectIdProperties'
321
                );
322
            }
323
        }
324 114
    }
325
326 104
    protected static function DaftObjectMethodNameFromProperty(
327
        string $property,
328
        bool $SetNotGet = false
329
    ) : string {
330 104
        return ($SetNotGet ? 'Set' : 'Get') . ucfirst($property);
331
    }
332
333
    /**
334
    * @param mixed $v
335
    *
336
    * @return mixed
337
    */
338 107
    protected function DoGetSet(
339
        string $property,
340
        bool $SetNotGet,
341
        string $notPublic,
342
        $v = null
343
    ) {
344
        if (
345
            (
346 107
                'id' !== $property ||
347 9
                false === ($this instanceof DefinesOwnIdPropertiesInterface)
348
            ) &&
349 107
            false === in_array($property, static::DaftObjectProperties(), true)
350
        ) {
351 3
            throw new UndefinedPropertyException(static::class, $property);
352
        }
353
354 104
        $expectedMethod = static::DaftObjectMethodNameFromProperty(
355 104
            $property,
356 104
            $SetNotGet
357
        );
358 104
        $thingers = static::DaftObjectPublicGetters();
359
360 104
        if ($SetNotGet) {
361 77
            $thingers = static::DaftObjectPublicSetters();
362
        }
363
364
        if (
365
            false === (
366 104
                in_array(
367 104
                    $property,
368 104
                    $thingers,
369 104
                    true
370
                )
371
            )
372
        ) {
373 2
            throw new $notPublic(
374 2
                static::class,
375 2
                $property
376
            );
377
        }
378
379 102
        return $this->$expectedMethod($v);
380
    }
381
}
382