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 ( 68f23c...d0216f )
by SignpostMarv
01:48
created

DaftObjectExportableProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
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 41
            NotPublicGetterPropertyException::class
78
        );
79
    }
80
81
    /**
82
    * {@inheritdoc}
83
    */
84 79
    public function __set(string $property, $v)
85
    {
86 79
        return $this->DoGetSet(
87 79
            $property,
88 79
            true,
89 79
            NotPublicSetterPropertyException::class,
90 79
            $v
91
        );
92
    }
93
94
    /**
95
    * {@inheritdoc}
96
    *
97
    * @see static::NudgePropertyValue()
98
    */
99 20
    public function __unset(string $property) : void
100
    {
101 20
        $this->NudgePropertyValue($property, null);
102 18
    }
103
104
    /**
105
    * {@inheritdoc}
106
    */
107 31
    public function __debugInfo() : array
108
    {
109 31
        $out = [];
110 31
        $publicGetters = static::DaftObjectPublicGetters();
111 31
        foreach (static::DaftObjectExportableProperties() as $prop) {
112 30
            $expectedMethod = 'Get' . ucfirst($prop);
113
            if (
114 30
                $this->__isset($prop) &&
115 30
                in_array($prop, $publicGetters, true)
116
            ) {
117 24
                $out[$prop] = $this->$expectedMethod();
118
            }
119
        }
120
121 31
        return $out;
122
    }
123
124
    /**
125
    * List of properties that can be defined on an implementation.
126
    *
127
    * @return string[]
128
    */
129 135
    final public static function DaftObjectProperties() : array
130
    {
131 135
        return static::PROPERTIES;
132
    }
133
134
    /**
135
    * {@inheritdoc}
136
    */
137 53
    final public static function DaftObjectNullableProperties() : array
138
    {
139 53
        return static::NULLABLE_PROPERTIES;
140
    }
141
142
    /**
143
    * {@inheritdoc}
144
    */
145 65
    final public static function DaftObjectExportableProperties() : array
146
    {
147 65
        return static::EXPORTABLE_PROPERTIES;
148
    }
149
150
    /**
151
    * {@inheritdoc}
152
    */
153 108
    final public static function DaftObjectPublicGetters() : array
154
    {
155 108
        static::CachePublicGettersAndSetters();
156
157 108
        return self::$publicGetters[static::class];
158
    }
159
160
    /**
161
    * {@inheritdoc}
162
    */
163 79
    final public static function DaftObjectPublicSetters() : array
164
    {
165 79
        static::CachePublicGettersAndSetters();
166
167 79
        return self::$publicSetters[static::class];
168
    }
169
170
    /**
171
    * {@inheritdoc}
172
    */
173 34
    final public static function DaftObjectJsonProperties() : array
174
    {
175 34
        static::ThrowIfNotDaftJson();
176
177 18
        return static::JSON_PROPERTIES;
178
    }
179
180
    /**
181
    * {@inheritdoc}
182
    */
183 18
    final public static function DaftObjectJsonPropertyNames() : array
184
    {
185 18
        $out = [];
186
187 18
        foreach (static::DaftObjectJsonProperties() as $k => $prop) {
188 18
            if (is_string($k)) {
189 12
                $prop = $k;
190
            }
191
192 18
            $out[] = $prop;
193
        }
194
195 18
        return $out;
196
    }
197
198 82
    protected static function ThrowIfNotDaftJson() : void
199
    {
200 82
        if (false === is_a(static::class, DaftJson::class, true)) {
201 64
            throw new DaftObjectNotDaftJsonBadMethodCallException(
202 64
                static::class
203
            );
204
        }
205 18
    }
206
207 13
    final protected static function HasPublicMethod(
208
        ReflectionClass $classReflection,
209
        string $method
210
    ) : bool {
211
        if (
212 13
            $classReflection->hasMethod($method)
213
        ) {
214 13
            $methodReflection = new ReflectionMethod(
215 13
                static::class,
216 13
                $method
217
            );
218
219
            return
220 13
                $methodReflection->isPublic() &&
221 13
                false === $methodReflection->isStatic();
222
        }
223
224 4
        return false;
225
    }
226
227 108
    final protected static function CachePublicGettersAndSetters() : void
228
    {
229 108
        if (false === isset(self::$publicGetters[static::class])) {
230 13
            self::$publicGetters[static::class] = [];
231 13
            self::$publicSetters[static::class] = [];
232
233
            if (
234 13
                is_a(
235 13
                    static::class,
236 13
                    DefinesOwnIdPropertiesInterface::class,
237 13
                    true
238
                )
239
            ) {
240 7
                self::$publicGetters[static::class][] = 'id';
241
            }
242
243 13
            $classReflection = new ReflectionClass(static::class);
244
245 13
            foreach (static::DaftObjectProperties() as $property) {
246
                if (
247 13
                    static::HasPublicMethod(
248 13
                        $classReflection,
249 13
                        static::DaftObjectMethodNameFromProperty($property)
250
                    )
251
                ) {
252 11
                    self::$publicGetters[static::class][] = $property;
253
                }
254
255
                if (
256 13
                    static::HasPublicMethod(
257 13
                        $classReflection,
258 13
                        static::DaftObjectMethodNameFromProperty(
259 13
                            $property,
260 13
                            true
261
                        )
262
                    )
263
                ) {
264 11
                    self::$publicSetters[static::class][] = $property;
265
                }
266
            }
267
        }
268 108
    }
269
270
    /**
271
    * Nudge the state of a given property, marking it as dirty.
272
    *
273
    * @param string $property property being nudged
274
    * @param mixed $value value to nudge property with
275
    *
276
    * @throws UndefinedPropertyException if $property is not in static::DaftObjectProperties()
277
    * @throws PropertyNotNullableException if $property is not in static::DaftObjectNullableProperties()
278
    * @throws PropertyNotRewriteableException if class is write-once read-many and $property was already changed
279
    */
280
    abstract protected function NudgePropertyValue(
281
        string $property,
282
        $value
283
    ) : void;
284
285
    /**
286
    * Checks if a type correctly defines it's own id.
287
    *
288
    * @throws ClassDoesNotImplementClassException if $class is not an implementation of DefinesOwnIdPropertiesInterface
289
    * @throws ClassMethodReturnHasZeroArrayCountException if $class::DaftObjectIdProperties() does not contain at least one property
290
    * @throws ClassMethodReturnIsNotArrayOfStringsException if $class::DaftObjectIdProperties() is not string[]
291
    * @throws UndefinedPropertyException if an id property is not in $class::DaftObjectIdProperties()
292
    */
293 188
    final protected static function CheckTypeDefinesOwnIdProperties(
294
        string $class,
295
        bool $throwIfNotImplementation = false
296
    ) : void {
297 188
        if (is_a($class, 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
        string $notPublic,
339
        $v = null
340
    ) {
341 108
        $expectedMethod = static::DaftObjectMethodNameFromProperty(
342 108
            $property,
343 108
            $SetNotGet
344
        );
345 108
        $thingers = static::DaftObjectPublicGetters();
346
347 108
        if ($SetNotGet) {
348 79
            $thingers = static::DaftObjectPublicSetters();
349
        }
350
351
        if (
352
            false === (
353 108
                in_array(
354 108
                    $property,
355 108
                    $thingers,
356 108
                    true
357
                )
358
            )
359
        ) {
360
            if (
361 5
                false === in_array(
362 5
                    $property,
363 5
                    static::DaftObjectProperties(),
364 5
                    true
365
                )
366
            ) {
367 3
                throw new UndefinedPropertyException(static::class, $property);
368
            }
369 2
            throw new $notPublic(
370 2
                static::class,
371 2
                $property
372
            );
373
        }
374
375 103
        return $this->$expectedMethod($v);
376
    }
377
}
378