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 ( 317cba...ec11b4 )
by SignpostMarv
01:55
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
                $jsonType = $jsonDef[$prop];
138
139 5
                if ('[]' === mb_substr($jsonType, -2)) {
140 3
                    $in[$prop] = static::DaftObjectFromJsonTypeArray(
141 3
                        mb_substr($jsonType, 0, -2),
142 3
                        $prop,
143 3
                        $array[$prop],
144 3
                        $writeAll
145
                    );
146
                } else {
147 2
                    $in[$prop] = static::DaftObjectFromJsonType(
148 2
                        $jsonType,
149 2
                        $array[$prop],
150 2
                        $writeAll
151
                    );
152
                }
153
            } else {
154 6
                $in[$prop] = $array[$prop];
155
            }
156
        }
157
158
        /**
159
        * @var DaftJson $out
160
        */
161 7
        $out = new static($in, $writeAll);
162
163 7
        return $out;
164
    }
165
166 22
    public static function DaftObjectFromJsonString(string $string) : DaftJson
167
    {
168 22
        static::ThrowIfNotDaftJson();
169
170 6
        return static::DaftObjectFromJsonArray(json_decode($string, true));
171
    }
172
173 75
    public function DaftObjectWormPropertyWritten(string $property) : bool
174
    {
175 75
        $wormProperties = $this->wormProperties;
176
177
        return
178 75
            ($this instanceof DaftObjectWorm) &&
179
            (
180 50
                $this->HasPropertyChanged($property) ||
181 75
                false === empty($wormProperties[$property])
182
            );
183
    }
184
185
    /**
186
    * @param mixed $propVal
187
    *
188
    * @return DaftJson
189
    */
190 2
    protected static function DaftObjectFromJsonType(
191
        string $jsonType,
192
        array $propVal,
193
        bool $writeAll
194
    ) {
195 2
        static::ThrowIfNotJsonType($jsonType);
196
197 2
        return static::ArrayToJsonType($jsonType, $propVal, $writeAll);
198
    }
199
200
    /**
201
    * @return DaftJson[]
202
    */
203 3
    protected static function DaftObjectFromJsonTypeArray(
204
        string $jsonType,
205
        string $prop,
206
        array $propVal,
207
        bool $writeAll
208
    ) : array {
209 3
        static::ThrowIfNotJsonType($jsonType);
210
211 2
        $out = [];
212
213 2
        foreach ($propVal as $val) {
214 2
            if (false === is_array($val)) {
215 1
                throw new PropertyNotJsonDecodableShouldBeArrayException(
216 1
                    $jsonType,
217 1
                    $prop
218
                );
219
            }
220 1
            $out[] = static::ArrayToJsonType(
221 1
                $jsonType,
222 1
                $val,
223 1
                $writeAll
224
            );
225
        }
226
227 1
        return $out;
228
    }
229
230
    /**
231
    * Retrieve a property from data.
232
    *
233
    * @param string $property the property being retrieved
234
    *
235
    * @throws PropertyNotNullableException if value is not set and $property is not listed as nullabe
236
    *
237
    * @return mixed the property value
238
    */
239 45
    protected function RetrievePropertyValueFromData(string $property)
240
    {
241
        if (
242 45
            false === array_key_exists($property, $this->data) &&
243 6
            false === in_array($property, static::NULLABLE_PROPERTIES, true)
244
        ) {
245 2
            throw new PropertyNotNullableException(static::class, $property);
246
        } elseif (
247 43
            in_array($property, static::NULLABLE_PROPERTIES, true)
248
        ) {
249 33
            return $this->data[$property] ?? null;
250
        }
251
252 43
        return $this->data[$property];
253
    }
254
255
    /**
256
    * {@inheritdoc}
257
    */
258 134
    protected function NudgePropertyValue(string $property, $value) : void
259
    {
260 134
        $this->MaybeThrowOnNudge($property, $value);
261
262
        $isChanged = (
263 75
            false === array_key_exists($property, $this->data) ||
264 75
            $this->data[$property] !== $value
265
        );
266
267 75
        $this->data[$property] = $value;
268
269 75
        if ($isChanged && true !== isset($this->changedProperties[$property])) {
270 75
            $this->changedProperties[$property] = true;
271 75
            $this->wormProperties[$property] = true;
272
        }
273 75
    }
274
275
    /**
276
    * @param mixed $value
277
    *
278
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
279
    */
280 134
    private function MaybeThrowOnNudge(string $property, $value) : void
281
    {
282 134
        if (true !== in_array($property, static::PROPERTIES, true)) {
283 1
            throw new UndefinedPropertyException(static::class, $property);
284
        } elseif (
285 133
            true === is_null($value) &&
286 88
            true !== in_array($property, static::NULLABLE_PROPERTIES, true)
287
        ) {
288 58
            throw new PropertyNotNullableException(static::class, $property);
289 75
        } elseif ($this->DaftObjectWormPropertyWritten($property)) {
290 50
            throw new PropertyNotRewriteableException(
291 50
                static::class,
292 50
                $property
293
            );
294
        }
295 75
    }
296
297 12
    private static function ThrowIfJsonDefNotValid(array $array) : array
298
    {
299 12
        $jsonProps = static::DaftObjectJsonPropertyNames();
300
301 12
        $jsonDef = static::DaftObjectJsonProperties();
302
303 12
        $out = [];
304
305 12
        foreach ($array as $prop => $propVal) {
306
            if (
307 12
                false === in_array($prop, $jsonProps, true)
308
            ) {
309 1
                throw new PropertyNotJsonDecodableException(
310 1
                    static::class,
311 1
                    $prop
312
                );
313 12
            } elseif (false === is_null($propVal)) {
314 11
                if (isset($jsonDef[$prop])) {
315 8
                    $jsonType = $jsonDef[$prop];
316
317 8
                    if (false === is_array($propVal)) {
318 2
                        static::ThrowBecauseArrayJsonTypeNotValid(
319 2
                            $jsonType,
320 2
                            $prop
321
                        );
322
                    }
323
                }
324 9
                $out[$prop] = $propVal;
325
            }
326
        }
327
328 9
        return $out;
329
    }
330
331 2
    private static function ThrowBecauseArrayJsonTypeNotValid(
332
        string $jsonType,
333
        string $prop
334
    ) : void {
335 2
        if ('[]' === mb_substr($jsonType, -2)) {
336 1
            throw new PropertyNotJsonDecodableShouldBeArrayException(
337 1
                static::class,
338 1
                $prop
339
            );
340
        }
341 1
        throw new PropertyNotJsonDecodableShouldBeArrayException(
342 1
            $jsonType,
343 1
            $prop
344
        );
345
    }
346
347 5
    private static function ThrowIfNotJsonType(string $jsonType) : void
348
    {
349 5
        if (false === is_a($jsonType, DaftJson::class, true)) {
350 1
            throw new ClassDoesNotImplementClassException(
351 1
                $jsonType,
352 1
                DaftJson::class
353
            );
354
        }
355 4
    }
356
357 3
    private static function ArrayToJsonType(
358
        string $jsonType,
359
        array $propVal,
360
        bool $writeAll
361
    ) : DaftJson {
362
        /**
363
        * @var DaftJson $jsonType
364
        */
365 3
        $jsonType = $jsonType;
366
367 3
        return $jsonType::DaftObjectFromJsonArray(
368 3
            $propVal,
369 3
            $writeAll
370
        );
371
    }
372
373 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...
374
    {
375 60
        if (false === is_a(static::class, DaftJson::class, true)) {
376 48
            throw new DaftObjectNotDaftJsonBadMethodCallException(
377 48
                static::class
378
            );
379
        }
380 12
    }
381
}
382