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 ( a5c6de...fae6e1 )
by SignpostMarv
06:10
created

AbstractDaftObject   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 314
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 30
eloc 85
dl 0
loc 314
ccs 95
cts 95
cp 1
rs 10
c 0
b 0
f 0

18 Methods

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