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.
Completed
Push — master ( 57520b...ec2097 )
by SignpostMarv
01:48
created

DaftObjectWormPropertyWritten()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
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
/**
12
* Array-backed daft objects.
13
*/
14
abstract class AbstractArrayBackedDaftObject extends AbstractDaftObject implements DaftObjectCreatedByArray
15
{
16
    /**
17
    * data for this instance.
18
    *
19
    * @var array
20
    */
21
    private $data = [];
22
23
    /**
24
    * List of changed properties.
25
    *
26
    * @var bool[]
27
    */
28
    private $changedProperties = [];
29
30
    /**
31
    * List of changed properties, for write-once read-many.
32
    *
33
    * @var bool[]
34
    */
35
    private $wormProperties = [];
36
37
    /**
38
    * {@inheritdoc}
39
    */
40 189
    public function __construct(array $data = [], bool $writeAll = false)
41
    {
42 189
        parent::__construct();
43
44 187
        if (true === $writeAll) {
45 60
            foreach ($data as $k => $v) {
46 60
                $this->__set($k, $v);
47
            }
48
        } else {
49 129
            foreach ($data as $k => $v) {
50 68
                $this->data[$k] = $v;
51
            }
52
        }
53 185
    }
54
55
    /**
56
    * {@inheritdoc}
57
    */
58 33
    public function __isset(string $property) : bool
59
    {
60
        return
61 33
            in_array($property, static::PROPERTIES, true) &&
62 33
            isset($this->data, $this->data[$property]);
63
    }
64
65
    /**
66
    * {@inheritdoc}
67
    */
68 29
    public function ChangedProperties() : array
69
    {
70
        /**
71
        * @var string[] $out
72
        */
73 29
        $out = array_keys($this->changedProperties);
74
75 29
        return $out;
76
    }
77
78
    /**
79
    * {@inheritdoc}
80
    */
81 33
    public function MakePropertiesUnchanged(string ...$properties) : void
82
    {
83 33
        foreach ($properties as $property) {
84 33
            unset($this->changedProperties[$property]);
85
        }
86 33
    }
87
88
    /**
89
    * {@inheritdoc}
90
    */
91 82
    public function HasPropertyChanged(string $property) : bool
92
    {
93
        return
94 82
            isset($this->changedProperties[$property]) &&
95 82
            true === $this->changedProperties[$property];
96
    }
97
98
    /**
99
    * {@inheritdoc}
100
    */
101 22
    public function jsonSerialize() : array
102
    {
103 22
        static::ThrowIfNotDaftJson();
104
105 6
        $out = [];
106
107 6
        foreach (static::DaftObjectJsonProperties() as $k => $v) {
108 6
            $property = $v;
109 6
            if (is_string($k)) {
110 3
                $property = $k;
111
            }
112
113 6
            $val = $this->DoGetSet(
114 6
                $property,
115 6
                'Get' . ucfirst($property),
116 6
                PropertyNotReadableException::class,
117 6
                NotPublicGetterPropertyException::class
118
            );
119
120 6
            if (false === is_null($val)) {
121 6
                $out[$property] = $val;
122
            }
123
        }
124
125 6
        return $out;
126
    }
127
128
    /**
129
    * {@inheritdoc}
130
    */
131 28
    final public static function DaftObjectFromJsonArray(
132
        array $array,
133
        bool $writeAll = false
134
    ) : DaftJson {
135 28
        static::ThrowIfNotDaftJson();
136 12
        $array = static::ThrowIfJsonDefNotValid($array);
137 8
        $in = [];
138
139 8
        $jsonDef = static::DaftObjectJsonProperties();
140
141 8
        foreach (array_keys($array) as $prop) {
142 8
            if (isset($jsonDef[$prop])) {
143 5
                $jsonType = $jsonDef[$prop];
144
145 5
                if ('[]' === mb_substr($jsonType, -2)) {
146 3
                    $in[$prop] = static::DaftObjectFromJsonTypeArray(
147 3
                        mb_substr($jsonType, 0, -2),
148 3
                        $prop,
149 3
                        $array[$prop],
150 3
                        $writeAll
151
                    );
152
                } else {
153 2
                    $in[$prop] = static::DaftObjectFromJsonType(
154 2
                        $jsonType,
155 2
                        $array[$prop],
156 2
                        $writeAll
157
                    );
158
                }
159
            } else {
160 6
                $in[$prop] = $array[$prop];
161
            }
162
        }
163
164
        /**
165
        * @var DaftJson $out
166
        */
167 6
        $out = new static($in, $writeAll);
168
169 6
        return $out;
170
    }
171
172 22
    public static function DaftObjectFromJsonString(string $string) : DaftJson
173
    {
174 22
        static::ThrowIfNotDaftJson();
175
176 6
        return static::DaftObjectFromJsonArray(json_decode($string, true));
177
    }
178
179 75
    public function DaftObjectWormPropertyWritten(string $property) : bool
180
    {
181 75
        $wormProperties = $this->wormProperties;
182
183
        return
184 75
            ($this instanceof DaftObjectWorm) &&
185
            (
186 50
                $this->HasPropertyChanged($property) ||
187 75
                false === empty($wormProperties[$property])
188
            );
189
    }
190
191
    /**
192
    * @param mixed $propVal
193
    *
194
    * @return DaftJson
195
    */
196 2
    protected static function DaftObjectFromJsonType(
197
        string $jsonType,
198
        array $propVal,
199
        bool $writeAll
200
    ) {
201 2
        static::ThrowIfNotJsonType($jsonType);
202
203 2
        return static::ArrayToJsonType($jsonType, $propVal, $writeAll);
204
    }
205
206
    /**
207
    * @return DaftJson[]
208
    */
209 3
    protected static function DaftObjectFromJsonTypeArray(
210
        string $jsonType,
211
        string $prop,
212
        array $propVal,
213
        bool $writeAll
214
    ) : array {
215 3
        static::ThrowIfNotJsonType($jsonType);
216
217 2
        $out = [];
218
219 2
        foreach ($propVal as $val) {
220 2
            if (false === is_array($val)) {
221 1
                throw new PropertyNotJsonDecodableShouldBeArrayException(
222 1
                    $jsonType,
223 1
                    $prop
224
                );
225
            }
226 1
            $out[] = static::ArrayToJsonType(
227 1
                $jsonType,
228 1
                $val,
229 1
                $writeAll
230
            );
231
        }
232
233 1
        return $out;
234
    }
235
236
    /**
237
    * Retrieve a property from data.
238
    *
239
    * @param string $property the property being retrieved
240
    *
241
    * @throws PropertyNotNullableException if value is not set and $property is not listed as nullabe
242
    *
243
    * @return mixed the property value
244
    */
245 44
    protected function RetrievePropertyValueFromData(string $property)
246
    {
247
        if (
248 44
            false === array_key_exists($property, $this->data) &&
249 5
            false === in_array($property, static::NULLABLE_PROPERTIES, true)
250
        ) {
251 1
            throw new PropertyNotNullableException(static::class, $property);
252
        } elseif (
253 43
            in_array($property, static::NULLABLE_PROPERTIES, true)
254
        ) {
255 33
            return $this->data[$property] ?? null;
256
        }
257
258 43
        return $this->data[$property];
259
    }
260
261
    /**
262
    * {@inheritdoc}
263
    */
264 134
    protected function NudgePropertyValue(string $property, $value) : void
265
    {
266 134
        $this->MaybeThrowOnNudge($property, $value);
267
268
        $isChanged = (
269 75
            false === array_key_exists($property, $this->data) ||
270 75
            $this->data[$property] !== $value
271
        );
272
273 75
        $this->data[$property] = $value;
274
275 75
        if ($isChanged && true !== isset($this->changedProperties[$property])) {
276 75
            $this->changedProperties[$property] = true;
277 75
            $this->wormProperties[$property] = true;
278
        }
279 75
    }
280
281
    /**
282
    * @param mixed $value
283
    *
284
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
285
    */
286 134
    private function MaybeThrowOnNudge(string $property, $value) : void
287
    {
288 134
        if (true !== in_array($property, static::PROPERTIES, true)) {
289 1
            throw new UndefinedPropertyException(static::class, $property);
290
        } elseif (
291 133
            true === is_null($value) &&
292 88
            true !== in_array($property, static::NULLABLE_PROPERTIES, true)
293
        ) {
294 58
            throw new PropertyNotNullableException(static::class, $property);
295 75
        } elseif ($this->DaftObjectWormPropertyWritten($property)) {
296 50
            throw new PropertyNotRewriteableException(
297 50
                static::class,
298 50
                $property
299
            );
300
        }
301 75
    }
302
303 12
    private static function ThrowIfJsonDefNotValid(array $array) : array
304
    {
305 12
        $jsonProps = [];
306
307 12
        $jsonDef = static::DaftObjectJsonProperties();
308 12
        $nullableProps = static::DaftObjectNullableProperties();
309
310 12
        foreach ($jsonDef as $k => $v) {
311 12
            $prop = $v;
312 12
            if (is_string($k)) {
313 9
                $prop = $k;
314
            }
315
            if (
316
                (
317 12
                    false === isset($array[$prop]) ||
318 11
                    is_null($array[$prop])
319
                ) &&
320 3
                false === in_array($prop, $nullableProps, true)
321
            ) {
322 1
                throw new PropertyNotNullableException(static::class, $prop);
323
            }
324
325 11
            $jsonProps[] = $prop;
326
        }
327
328 11
        $out = [];
329
330 11
        foreach ($array as $prop => $propVal) {
331
            if (
332 11
                false === in_array($prop, $jsonProps, true)
333
            ) {
334 1
                throw new PropertyNotJsonDecodableException(
335 1
                    static::class,
336 1
                    $prop
337
                );
338 11
            } elseif (false === is_null($propVal)) {
339 11
                if (isset($jsonDef[$prop])) {
340 8
                    $jsonType = $jsonDef[$prop];
341
342 8
                    if (false === is_array($propVal)) {
343 2
                        if ('[]' === mb_substr($jsonType, -2)) {
344 1
                            throw new PropertyNotJsonDecodableShouldBeArrayException(
345 1
                                static::class,
346 1
                                $prop
347
                            );
348
                        }
349 1
                        throw new PropertyNotJsonDecodableShouldBeArrayException(
350 1
                            $jsonType,
351 1
                            $prop
352
                        );
353
                    }
354
                }
355 9
                $out[$prop] = $propVal;
356
            }
357
        }
358
359 8
        return $out;
360
    }
361
362 5
    private static function ThrowIfNotJsonType(string $jsonType) : void
363
    {
364 5
        if (false === is_a($jsonType, DaftJson::class, true)) {
365 1
            throw new ClassDoesNotImplementClassException(
366 1
                $jsonType,
367 1
                DaftJson::class
368
            );
369
        }
370 4
    }
371
372 3
    private static function ArrayToJsonType(
373
        string $jsonType,
374
        array $propVal,
375
        bool $writeAll
376
    ) : DaftJson {
377
        /**
378
        * @var DaftJson $jsonType
379
        */
380 3
        $jsonType = $jsonType;
381
382 3
        return $jsonType::DaftObjectFromJsonArray(
383 3
            $propVal,
384 3
            $writeAll
385
        );
386
    }
387
388 60 View Code Duplication
    private static function ThrowIfNotDaftJson() : void
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...
389
    {
390 60
        if (false === is_a(static::class, DaftJson::class, true)) {
391 48
            throw new DaftObjectNotDaftJsonBadMethodCallException(
392 48
                static::class
393
            );
394
        }
395 12
    }
396
}
397