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 ( 78966e...4a7e81 )
by SignpostMarv
02:12
created

DaftObjectFromJsonArray()   D

Complexity

Conditions 23
Paths 42

Size

Total Lines 119
Code Lines 74

Duplication

Lines 7
Ratio 5.88 %

Code Coverage

Tests 47
CRAP Score 35.6235

Importance

Changes 0
Metric Value
dl 7
loc 119
ccs 47
cts 66
cp 0.7121
rs 4.6303
c 0
b 0
f 0
cc 23
eloc 74
nc 42
nop 2
crap 35.6235

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 BadMethodCallException;
12
13
/**
14
* Array-backed daft objects.
15
*/
16
abstract class AbstractArrayBackedDaftObject extends AbstractDaftObject implements DaftObjectCreatedByArray
17
{
18
    /**
19
    * data for this instance.
20
    *
21
    * @var array
22
    */
23
    private $data = [];
24
25
    /**
26
    * List of changed properties.
27
    *
28
    * @var bool[]
29
    */
30
    private $changedProperties = [];
31
32
    /**
33
    * List of changed properties, for write-once read-many.
34
    *
35
    * @var bool[]
36
    */
37
    private $wormProperties = [];
38
39
    /**
40
    * {@inheritdoc}
41
    */
42 189
    public function __construct(array $data = [], bool $writeAll = false)
43
    {
44 189
        parent::__construct();
45
46 187
        if (true === $writeAll) {
47 60
            foreach ($data as $k => $v) {
48 60
                $this->__set($k, $v);
49
            }
50
        } else {
51 129
            foreach ($data as $k => $v) {
52 68
                $this->data[$k] = $v;
53
            }
54
        }
55 185
    }
56
57
    /**
58
    * {@inheritdoc}
59
    */
60 33
    public function __isset(string $property) : bool
61
    {
62
        return
63 33
            in_array($property, static::PROPERTIES, true) &&
64 33
            isset($this->data, $this->data[$property]);
65
    }
66
67
    /**
68
    * {@inheritdoc}
69
    */
70 29
    public function ChangedProperties() : array
71
    {
72
        /**
73
        * @var string[] $out
74
        */
75 29
        $out = array_keys($this->changedProperties);
76
77 29
        return $out;
78
    }
79
80
    /**
81
    * {@inheritdoc}
82
    */
83 33
    public function MakePropertiesUnchanged(string ...$properties) : void
84
    {
85 33
        foreach ($properties as $property) {
86 33
            unset($this->changedProperties[$property]);
87
        }
88 33
    }
89
90
    /**
91
    * {@inheritdoc}
92
    */
93 82
    public function HasPropertyChanged(string $property) : bool
94
    {
95
        return
96 82
            isset($this->changedProperties[$property]) &&
97 82
            true === $this->changedProperties[$property];
98
    }
99
100
    /**
101
    * {@inheritdoc}
102
    */
103 22
    public function jsonSerialize() : array
104
    {
105 22
        if (false === ($this instanceof DaftJson)) {
106 16
            throw new BadMethodCallException(
107
                static::class .
108
                ' does not implement ' .
109 16
                DaftJson::class
110
            );
111
        }
112
113 6
        $out = [];
114
115 6
        foreach (static::DaftObjectJsonProperties() as $k => $v) {
116 6
            $property = $v;
117 6
            if (is_string($k)) {
118 3
                $property = $k;
119
            }
120
121 6
            $val = $this->DoGetSet(
122 6
                $property,
123 6
                'Get' . ucfirst($property),
124 6
                PropertyNotReadableException::class,
125 6
                NotPublicGetterPropertyException::class
126
            );
127
128 6
            if (false === is_null($val)) {
129 6
                $out[$property] = $val;
130
            }
131
        }
132
133 6
        return $out;
134
    }
135
136
    /**
137
    * {@inheritdoc}
138
    */
139 22
    final public static function DaftObjectFromJsonArray(
140
        array $array,
141
        bool $writeAll = false
142
    ) : DaftJson {
143 22 View Code Duplication
        if (false === is_a(static::class, DaftJson::class, true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
144 16
            throw new BadMethodCallException(
145
                static::class .
146
                ' does not implement ' .
147 16
                DaftJson::class
148
            );
149
        }
150
151 6
        $in = [];
152
153 6
        $props = static::DaftObjectJsonProperties();
154 6
        $jsonDef = static::DaftObjectJsonProperties();
155 6
        $nullableProps = static::DaftObjectNullableProperties();
156
157 6
        $jsonProps = [];
158
159 6
        foreach ($jsonDef as $k => $v) {
160 6
            if (is_string($k)) {
161 3
                $jsonProps[] = $k;
162
            } else {
163 6
                $jsonProps[] = $v;
164
            }
165
        }
166
167 6
        foreach ($jsonProps as $prop) {
168
            if (
169
                (
170 6
                    false === isset($array[$prop]) ||
171 6
                    is_null($array[$prop])
172
                ) &&
173 2
                false === in_array($prop, $nullableProps, true)
174
            ) {
175
                throw new PropertyNotNullableException(static::class, $prop);
176
            }
177
        }
178
179 6
        foreach (array_keys($array) as $prop) {
180
            if (
181 6
                false === in_array($prop, $props, true) &&
182 3
                false === isset($jsonDef[$prop])
183
            ) {
184
                throw new UndefinedPropertyException(static::class, $prop);
185
            } elseif (
186 6
                false === in_array($prop, $jsonProps, true)
187
            ) {
188
                throw new PropertyNotJsonDecodableException(
189
                    static::class,
190
                    $prop
191
                );
192 6
            } elseif (is_null($array[$prop])) {
193
                continue;
194 6
            } elseif (isset($jsonDef[$prop])) {
195 3
                $jsonType = $jsonDef[$prop];
196 3
                $isArray = false;
197 3
                if ('[]' === mb_substr($jsonType, -2)) {
198 1
                    $isArray = true;
199 1
                    $jsonType = mb_substr($jsonType, 0, -2);
200
                }
201
202 3
                if ($isArray && false === is_array($array[$prop])) {
203
                    throw new PropertyNotJsonDecodableShouldBeArrayException(
204
                        static::class,
205
                        $prop
206
                    );
207 3
                } elseif (false === is_a($jsonType, DaftJson::class, true)) {
208
                    throw new ClassDoesNotImplementClassException(
209
                        $jsonType,
210
                        DaftJson::class
211
                    );
212
                }
213
214
                /**
215
                * @var DaftJson $jsonType
216
                */
217 3
                $jsonType = $jsonType;
218
219 3
                if ($isArray) {
220 1
                    $in[$prop] = [];
221
222 1
                    foreach ($array[$prop] as $i => $val) {
223 1
                        if (is_array($val)) {
224 1
                            $in[$prop][] = $jsonType::DaftObjectFromJsonArray(
225 1
                                $val,
226 1
                                $writeAll
227
                            );
228
                        } else {
229
                            throw new PropertyNotJsonDecodableShouldBeArrayException(
230
                                (string) $jsonType,
231 1
                                ($prop . '[' . $i . ']')
232
                            );
233
                        }
234
                    }
235 2
                } elseif (is_array($array[$prop])) {
236 2
                    $in[$prop] = $jsonType::DaftObjectFromJsonArray(
237 2
                        $array[$prop],
238 2
                        $writeAll
239
                    );
240
                } else {
241
                    throw new PropertyNotJsonDecodableShouldBeArrayException(
242
                        (string) $jsonType,
243
                        $prop
244
                    );
245
                }
246
            } else {
247 6
                $in[$prop] = $array[$prop];
248
            }
249
        }
250
251 6
        $out = new static($in, $writeAll);
252
253 6
        if ( ! ($out instanceof DaftJson)) { // here to trick phpstan
254
            exit;
255
        }
256
257 6
        return $out;
258
    }
259
260 22
    public static function DaftObjectFromJsonString(string $string) : DaftJson
261
    {
262 22 View Code Duplication
        if (false === is_a(static::class, DaftJson::class, true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
263 16
            throw new BadMethodCallException(
264
                static::class .
265
                ' does not implement ' .
266 16
                DaftJson::class
267
            );
268
        }
269
270 6
        return static::DaftObjectFromJsonArray(json_decode($string, true));
271
    }
272
273
    /**
274
    * Retrieve a property from data.
275
    *
276
    * @param string $property the property being retrieved
277
    *
278
    * @throws PropertyNotNullableException if value is not set and $property is not listed as nullabe
279
    *
280
    * @return mixed the property value
281
    */
282 44
    protected function RetrievePropertyValueFromData(string $property)
283
    {
284
        if (
285 44
            false === array_key_exists($property, $this->data) &&
286 5
            false === in_array($property, static::NULLABLE_PROPERTIES, true)
287
        ) {
288 1
            throw new PropertyNotNullableException(static::class, $property);
289
        } elseif (
290 43
            in_array($property, static::NULLABLE_PROPERTIES, true)
291
        ) {
292 33
            return $this->data[$property] ?? null;
293
        }
294
295 43
        return $this->data[$property];
296
    }
297
298
    /**
299
    * {@inheritdoc}
300
    */
301 134
    protected function NudgePropertyValue(string $property, $value) : void
302
    {
303 134
        if (true !== in_array($property, static::PROPERTIES, true)) {
304 1
            throw new UndefinedPropertyException(static::class, $property);
305
        } elseif (
306 133
            true === is_null($value) &&
307 88
            true !== in_array($property, static::NULLABLE_PROPERTIES, true)
308
        ) {
309 58
            throw new PropertyNotNullableException(static::class, $property);
310
        } elseif (
311 75
            $this instanceof DaftObjectWorm &&
312
            (
313 50
                $this->HasPropertyChanged($property) ||
314
                (
315 50
                    isset($this->wormProperties[$property]) &&
0 ignored issues
show
Bug Best Practice introduced by
The property wormProperties does not exist on SignpostMarv\DaftObject\DaftObjectWorm. Since you implemented __get, consider adding a @property annotation.
Loading history...
316 2
                    true === $this->wormProperties[$property]
317
                )
318
            )
319
        ) {
320 50
            throw new PropertyNotRewriteableException(
321 50
                static::class,
322 50
                $property
323
            );
324
        }
325
326
        $isChanged = (
327 75
            false === array_key_exists($property, $this->data) ||
328 75
            $this->data[$property] !== $value
329
        );
330
331 75
        $this->data[$property] = $value;
332
333 75
        if ($isChanged && true !== isset($this->changedProperties[$property])) {
334 75
            $this->changedProperties[$property] = true;
335 75
            $this->wormProperties[$property] = true;
336
        }
337 75
    }
338
}
339