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 ( ec11b4...1b9cd3 )
by SignpostMarv
01:57
created

ThrowBecauseArrayJsonTypeNotValid()   A

Complexity

Conditions 2
Paths 0

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 0
nop 2
crap 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
/**
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 188
    public function __construct(array $data = [], bool $writeAll = false)
41
    {
42 188
        parent::__construct();
43
44 186
        if (true === $writeAll) {
45 61
            foreach ($data as $k => $v) {
46 61
                $this->__set($k, $v);
47
            }
48
        } else {
49 127
            foreach ($data as $k => $v) {
50 66
                $this->data[$k] = $v;
51
            }
52
        }
53 184
    }
54
55
    /**
56
    * {@inheritdoc}
57
    */
58 31
    public function __isset(string $property) : bool
59
    {
60
        return
61 31
            in_array($property, static::PROPERTIES, true) &&
62 31
            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::DaftObjectJsonPropertyNames() as $property) {
108 6
            $val = $this->DoGetSet(
109 6
                $property,
110 6
                false,
111 6
                NotPublicGetterPropertyException::class
112
            );
113
114 6
            if (false === is_null($val)) {
115 6
                $out[$property] = $val;
116
            }
117
        }
118
119 6
        return $out;
120
    }
121
122
    /**
123
    * {@inheritdoc}
124
    */
125 28
    final public static function DaftObjectFromJsonArray(
126
        array $array,
127
        bool $writeAll = false
128
    ) : DaftJson {
129 28
        static::ThrowIfNotDaftJson();
130 12
        $array = static::ThrowIfJsonDefNotValid($array);
131 9
        $in = [];
132
133 9
        $jsonDef = static::DaftObjectJsonProperties();
134
135 9
        foreach (array_keys($array) as $prop) {
136 8
            if (isset($jsonDef[$prop])) {
137 5
                $in[$prop] = static::DaftObjectFromJsonArrayTyped(
138 5
                    $jsonDef[$prop],
139 5
                    $prop,
140 5
                    $array,
141 5
                    $writeAll
142
                );
143
            } else {
144 6
                $in[$prop] = $array[$prop];
145
            }
146
        }
147
148
        /**
149
        * @var DaftJson $out
150
        */
151 7
        $out = new static($in, $writeAll);
152
153 7
        return $out;
154
    }
155
156 22
    public static function DaftObjectFromJsonString(string $string) : DaftJson
157
    {
158 22
        static::ThrowIfNotDaftJson();
159
160 6
        return static::DaftObjectFromJsonArray(json_decode($string, true));
161
    }
162
163 75
    public function DaftObjectWormPropertyWritten(string $property) : bool
164
    {
165 75
        $wormProperties = $this->wormProperties;
166
167
        return
168 75
            ($this instanceof DaftObjectWorm) &&
169
            (
170 50
                $this->HasPropertyChanged($property) ||
171 75
                false === empty($wormProperties[$property])
172
            );
173
    }
174
175
    /**
176
    * @return DaftJson|DaftJson[]
177
    */
178 5
    final protected static function DaftObjectFromJsonArrayTyped(
179
        string $jsonType,
180
        string $prop,
181
        array $array,
182
        bool $writeAll = false
183
    ) {
184 5
        if ('[]' === mb_substr($jsonType, -2)) {
185 3
            return static::DaftObjectFromJsonTypeArray(
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::DaftObjec...rray[$prop], $writeAll) returns the type array which is incompatible with the documented return type SignpostMarv\DaftObject\...arv\DaftObject\DaftJson.
Loading history...
186 3
                mb_substr($jsonType, 0, -2),
187 3
                $prop,
188 3
                $array[$prop],
189 3
                $writeAll
190
            );
191
        }
192
193 2
        return static::DaftObjectFromJsonType(
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::DaftObjec...rray[$prop], $writeAll) returns the type SignpostMarv\DaftObject\DaftJson which is incompatible with the documented return type SignpostMarv\DaftObject\...arv\DaftObject\DaftJson.
Loading history...
194 2
            $jsonType,
195 2
            $array[$prop],
196 2
            $writeAll
197
        );
198
    }
199
200
    /**
201
    * @param mixed $propVal
202
    *
203
    * @return DaftJson
204
    */
205 2
    protected static function DaftObjectFromJsonType(
206
        string $jsonType,
207
        array $propVal,
208
        bool $writeAll
209
    ) {
210 2
        static::ThrowIfNotJsonType($jsonType);
211
212 2
        return static::ArrayToJsonType($jsonType, $propVal, $writeAll);
213
    }
214
215
    /**
216
    * @return DaftJson[]
217
    */
218 3
    protected static function DaftObjectFromJsonTypeArray(
219
        string $jsonType,
220
        string $prop,
221
        array $propVal,
222
        bool $writeAll
223
    ) : array {
224 3
        static::ThrowIfNotJsonType($jsonType);
225
226 2
        $out = [];
227
228 2
        foreach ($propVal as $val) {
229 2
            if (false === is_array($val)) {
230 1
                throw new PropertyNotJsonDecodableShouldBeArrayException(
231 1
                    $jsonType,
232 1
                    $prop
233
                );
234
            }
235 1
            $out[] = static::ArrayToJsonType(
236 1
                $jsonType,
237 1
                $val,
238 1
                $writeAll
239
            );
240
        }
241
242 1
        return $out;
243
    }
244
245
    /**
246
    * Retrieve a property from data.
247
    *
248
    * @param string $property the property being retrieved
249
    *
250
    * @throws PropertyNotNullableException if value is not set and $property is not listed as nullabe
251
    *
252
    * @return mixed the property value
253
    */
254 45
    protected function RetrievePropertyValueFromData(string $property)
255
    {
256
        if (
257 45
            false === array_key_exists($property, $this->data) &&
258 6
            false === in_array($property, static::NULLABLE_PROPERTIES, true)
259
        ) {
260 2
            throw new PropertyNotNullableException(static::class, $property);
261
        } elseif (
262 43
            in_array($property, static::NULLABLE_PROPERTIES, true)
263
        ) {
264 33
            return $this->data[$property] ?? null;
265
        }
266
267 43
        return $this->data[$property];
268
    }
269
270
    /**
271
    * {@inheritdoc}
272
    */
273 134
    protected function NudgePropertyValue(string $property, $value) : void
274
    {
275 134
        $this->MaybeThrowOnNudge($property, $value);
276
277
        $isChanged = (
278 75
            false === array_key_exists($property, $this->data) ||
279 75
            $this->data[$property] !== $value
280
        );
281
282 75
        $this->data[$property] = $value;
283
284 75
        if ($isChanged && true !== isset($this->changedProperties[$property])) {
285 75
            $this->changedProperties[$property] = true;
286 75
            $this->wormProperties[$property] = true;
287
        }
288 75
    }
289
290
    /**
291
    * @param mixed $value
292
    *
293
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
294
    */
295 134
    private function MaybeThrowOnNudge(string $property, $value) : void
296
    {
297 134
        if (true !== in_array($property, static::PROPERTIES, true)) {
298 1
            throw new UndefinedPropertyException(static::class, $property);
299
        } elseif (
300 133
            true === is_null($value) &&
301 88
            true !== in_array($property, static::NULLABLE_PROPERTIES, true)
302
        ) {
303 58
            throw new PropertyNotNullableException(static::class, $property);
304 75
        } elseif ($this->DaftObjectWormPropertyWritten($property)) {
305 50
            throw new PropertyNotRewriteableException(
306 50
                static::class,
307 50
                $property
308
            );
309
        }
310 75
    }
311
312 12
    private static function ThrowIfJsonDefNotValid(array $array) : array
313
    {
314 12
        $jsonProps = static::DaftObjectJsonPropertyNames();
315
316 12
        $jsonDef = static::DaftObjectJsonProperties();
317
318 12
        $out = [];
319
320 12
        foreach ($array as $prop => $propVal) {
321
            if (
322 12
                false === in_array($prop, $jsonProps, true)
323
            ) {
324 1
                throw new PropertyNotJsonDecodableException(
325 1
                    static::class,
326 1
                    $prop
327
                );
328 12
            } elseif (false === is_null($propVal)) {
329 11
                if (isset($jsonDef[$prop])) {
330 8
                    $jsonType = $jsonDef[$prop];
331
332 8
                    if (false === is_array($propVal)) {
333 2
                        static::ThrowBecauseArrayJsonTypeNotValid(
334 2
                            $jsonType,
335 2
                            $prop
336
                        );
337
                    }
338
                }
339 9
                $out[$prop] = $propVal;
340
            }
341
        }
342
343 9
        return $out;
344
    }
345
346 2
    private static function ThrowBecauseArrayJsonTypeNotValid(
347
        string $jsonType,
348
        string $prop
349
    ) : void {
350 2
        if ('[]' === mb_substr($jsonType, -2)) {
351 1
            throw new PropertyNotJsonDecodableShouldBeArrayException(
352 1
                static::class,
353 1
                $prop
354
            );
355
        }
356 1
        throw new PropertyNotJsonDecodableShouldBeArrayException(
357 1
            $jsonType,
358 1
            $prop
359
        );
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