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 ( 28f884...3bc013 )
by SignpostMarv
03:45
created

DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.108

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 21
rs 9.9332
c 0
b 0
f 0
ccs 7
cts 10
cp 0.7
crap 2.108
1
<?php
2
/**
3
* Base daft objects.
4
*
5
* @author SignpostMarv
6
*/
7
declare(strict_types=1);
8
9
namespace SignpostMarv\DaftObject;
10
11
/**
12
* Base daft object.
13
*/
14
abstract class AbstractDaftObject implements DaftObject
15
{
16
    /**
17
    * List of properties that can be defined on an implementation.
18
    *
19
    * @var array<int, string>
20
    */
21
    const PROPERTIES = [];
22
23
    /**
24
    * List of nullable properties that can be defined on an implementation.
25
    *
26
    * @var array<int, string>
27
    */
28
    const NULLABLE_PROPERTIES = [];
29
30
    /**
31
    * List of exportable properties that can be defined on an implementation.
32
    *
33
    * @var array<int, string>
34
    */
35
    const EXPORTABLE_PROPERTIES = [];
36
37
    /**
38
    * import/export definition for DaftJson.
39
    *
40
    * @var array<int, string>
41
    */
42
    const JSON_PROPERTIES = [];
43
44
    /**
45
    * List of sortable properties for DaftSortableObject.
46
    *
47
    * @var array<int, string>
48
    */
49
    const SORTABLE_PROPERTIES = [];
50
51
    /**
52
    * @var array<string, array<int, string>>
53
    */
54
    const CHANGE_OTHER_PROPERTIES = [];
55
56
    /**
57
    * @var array<string, array<int, string>>
58
    */
59
    const PROPERTIES_WITH_MULTI_TYPED_ARRAYS = [];
60
61
    /**
62
    * Does some sanity checking.
63
    *
64
    * @see DefinesOwnIdPropertiesInterface
65
    * @see TypeUtilities::CheckTypeDefinesOwnIdProperties()
66
    */
67 528
    public function __construct()
68
    {
69 528
        TypeUtilities::CheckTypeDefinesOwnIdProperties(
70 528
            static::class,
71 528
            ($this instanceof DefinesOwnIdPropertiesInterface)
72
        );
73 524
    }
74
75
    /**
76
    * @return mixed
77
    */
78 92
    public function __get(string $property)
79
    {
80 92
        return $this->DoGetSet($property, false);
81
    }
82
83
    /**
84
    * @param mixed $v
85
    */
86 186
    public function __set(string $property, $v) : void
87
    {
88 186
        $this->DoGetSet($property, true, $v);
89 170
    }
90
91
    /**
92
    * @see static::NudgePropertyValue()
93
    */
94 46
    public function __unset(string $property) : void
95
    {
96 46
        $this->NudgePropertyValue($property, null);
97 42
    }
98
99
    /**
100
    * @return array<string, mixed>
101
    */
102 72
    public function __debugInfo() : array
103
    {
104 72
        $getters = static::DaftObjectPublicGetters();
105 72
        $exportables = static::DaftObjectExportableProperties();
106
        /**
107
        * @var array<int, string>
108
        */
109
        $properties = array_filter($exportables, function (string $prop) use ($getters) : bool {
110 70
            return $this->__isset($prop) && in_array($prop, $getters, true);
111 72
        });
112
113
        /**
114
        * @var array<string, mixed>
115
        */
116 72
        $out = array_combine($properties, array_map(
117
            /**
118
            * @return mixed
119
            */
120
            function (string $prop) {
121 58
                $expectedMethod = TypeUtilities::MethodNameFromProperty($prop);
122
123 58
                return $this->$expectedMethod();
124 72
            },
125 72
            $properties
126
        ));
127
128 72
        return $out;
129
    }
130
131 4
    public function CompareToDaftSortableObject(DaftSortableObject $otherObject) : int
132
    {
133 4
        if ( ! is_a(static::class, DaftSortableObject::class, true)) {
134 2
            throw new ClassDoesNotImplementClassException(
135 2
                static::class,
136 2
                DaftSortableObject::class
137
            );
138
        }
139
140 2
        foreach (static::DaftSortableObjectProperties() as $property) {
141 2
            $method = TypeUtilities::MethodNameFromProperty($property);
142 2
            $sort = $this->$method() <=> $otherObject->$method();
143
144 2
            if (0 !== $sort) {
145 2
                return $sort;
146
            }
147
        }
148
149 2
        return 0;
150
    }
151
152
    /**
153
    * List of properties that can be defined on an implementation.
154
    *
155
    * @return array<int, string>
156
    */
157 390
    public static function DaftObjectProperties() : array
158
    {
159
        /**
160
        * @var array<int, string>
161
        */
162 390
        $out = static::PROPERTIES;
163
164 390
        return $out;
165
    }
166
167 112
    public static function DaftObjectNullableProperties() : array
168
    {
169
        /**
170
        * @var array<int, string>
171
        */
172 112
        $out = static::NULLABLE_PROPERTIES;
173
174 112
        return $out;
175
    }
176
177 148
    public static function DaftObjectExportableProperties() : array
178
    {
179
        /**
180
        * @var array<int, string>
181
        */
182 148
        $out = static::EXPORTABLE_PROPERTIES;
183
184 148
        return $out;
185
    }
186
187 118
    final public static function DaftObjectPublicGetters() : array
188
    {
189 118
        return TypeUtilities::DaftObjectPublicGetters(static::class);
190
    }
191
192 2
    final public static function DaftObjectPublicOrProtectedGetters() : array
193
    {
194 2
        return TypeUtilities::DaftObjectPublicOrProtectedGetters(static::class);
195
    }
196
197 186
    final public static function DaftObjectPublicSetters() : array
198
    {
199 186
        return TypeUtilities::DaftObjectPublicSetters(static::class);
200
    }
201
202 78
    public static function DaftObjectJsonProperties() : array
203
    {
204 78
        JsonTypeUtilities::ThrowIfNotDaftJson(static::class);
205
206
        /**
207
        * @var array<int|string, string>
208
        */
209 36
        $out = static::JSON_PROPERTIES;
210
211 36
        return $out;
212
    }
213
214 36
    final public static function DaftObjectJsonPropertyNames() : array
215
    {
216 36
        $out = [];
217
218
        /**
219
        * @var array<int|string, string>
220
        */
221 36
        $jsonProperties = static::DaftObjectJsonProperties();
222
223 36
        foreach ($jsonProperties as $k => $prop) {
224 36
            if (is_string($k)) {
225 24
                $prop = $k;
226
            }
227
228 36
            $out[] = $prop;
229
        }
230
231 36
        return $out;
232
    }
233
234
    /**
235
    * @return array<int, string>
236
    */
237 40
    public static function DaftSortableObjectProperties() : array
238
    {
239 40
        if ( ! is_a(static::class, DaftSortableObject::class, true)) {
240 36
            throw new ClassDoesNotImplementClassException(
241 36
                static::class,
242 36
                DaftSortableObject::class
243
            );
244
        }
245
246
        /**
247
        * @var array<int, string>
248
        */
249 4
        $out = static::SORTABLE_PROPERTIES;
250
251 4
        return $out;
252
    }
253
254
    /**
255
    * @return array<string, array<int, string>>
256
    */
257 76
    public static function DaftObjectPropertiesChangeOtherProperties() : array
258
    {
259
        /**
260
        * @var array<string, array<int, string>>
261
        */
262 76
        $out = static::CHANGE_OTHER_PROPERTIES;
263
264 76
        return $out;
265
    }
266
267
    /**
268
    * @return array<string, array<int, string>>
269
    */
270 16
    public static function DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues() : array
271
    {
272
        if (
273 16
            ! is_a(
274 16
                static::class,
275 16
                DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues::class,
276 16
                true
277
            )
278
        ) {
279
            throw new ClassDoesNotImplementClassException(
280
                static::class,
281
                DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues::class
282
            );
283
        }
284
285
        /**
286
        * @var array<string, array<int, string>>
287
        */
288 16
        $out = static::PROPERTIES_WITH_MULTI_TYPED_ARRAYS;
289
290 16
        return $out;
291
    }
292
293
    /**
294
    * Nudge the state of a given property, marking it as dirty.
295
    *
296
    * @param string $property property being nudged
297
    * @param scalar|array|object|null $value value to nudge property with
298
    *
299
    * @throws UndefinedPropertyException if $property is not in static::DaftObjectProperties()
300
    * @throws PropertyNotNullableException if $property is not in static::DaftObjectNullableProperties()
301
    * @throws PropertyNotRewriteableException if class is write-once read-many and $property was already changed
302
    */
303
    abstract protected function NudgePropertyValue(string $property, $value) : void;
304
305 234
    protected function MaybeThrowOnDoGetSet(string $property, bool $setter, array $props) : void
306
    {
307 234
        if (false === in_array($property, $props, true)) {
308 10
            if (false === in_array($property, static::DaftObjectProperties(), true)) {
309 6
                throw new UndefinedPropertyException(static::class, $property);
310 4
            } elseif ($setter) {
311 2
                throw new NotPublicSetterPropertyException(static::class, $property);
312
            }
313
314 2
            throw new NotPublicGetterPropertyException(static::class, $property);
315
        }
316 224
    }
317
318
    /**
319
    * @param mixed $v
320
    *
321
    * @return mixed
322
    */
323 234
    protected function DoGetSet(string $property, bool $setter, $v = null)
324
    {
325 234
        $props = $setter ? static::DaftObjectPublicSetters() : static::DaftObjectPublicGetters();
326
327 234
        $this->MaybeThrowOnDoGetSet($property, $setter, $props);
328
329 224
        return $this->{TypeUtilities::MethodNameFromProperty($property, $setter)}($v);
330
    }
331
}
332