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 ( 6098c5...3efc04 )
by SignpostMarv
02:38
created

DaftObjectFromJsonTypeArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 4
dl 0
loc 18
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
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<string, mixed>
20
    */
21
    private $data = [];
22
23
    /**
24
    * List of changed properties.
25
    *
26
    * @var array<string, bool>
27
    */
28
    private $changedProperties = [];
29
30
    /**
31
    * List of changed properties, for write-once read-many.
32
    *
33
    * @var array<string, bool>
34
    */
35
    private $wormProperties = [];
36
37 188
    public function __construct(array $data = [], bool $writeAll = false)
38
    {
39 188
        parent::__construct();
40
41 186
        if (true === $writeAll) {
42 61
            foreach ($data as $k => $v) {
43 61
                $this->__set($k, $v);
44
            }
45
        } else {
46 127
            foreach ($data as $k => $v) {
47 66
                $this->data[$k] = $v;
48
            }
49
        }
50 184
    }
51
52 31
    public function __isset(string $property) : bool
53
    {
54
        return
55 31
            in_array($property, static::PROPERTIES, true) &&
56 31
            isset($this->data, $this->data[$property]);
57
    }
58
59 29
    public function ChangedProperties() : array
60
    {
61 29
        return array_keys($this->changedProperties);
62
    }
63
64 33
    public function MakePropertiesUnchanged(string ...$properties) : void
65
    {
66 33
        foreach ($properties as $property) {
67 33
            unset($this->changedProperties[$property]);
68
        }
69 33
    }
70
71 82
    public function HasPropertyChanged(string $property) : bool
72
    {
73
        return
74 82
            isset($this->changedProperties[$property]) &&
75 82
            true === $this->changedProperties[$property];
76
    }
77
78 22
    public function jsonSerialize() : array
79
    {
80 22
        static::ThrowIfNotDaftJson();
81
82 6
        $out = [];
83
84 6
        foreach (static::DaftObjectJsonPropertyNames() as $property) {
85 6
            $val = $this->DoGetSet($property, false);
86
87 6
            if (false === is_null($val)) {
88 6
                $out[$property] = $val;
89
            }
90
        }
91
92 6
        return $out;
93
    }
94
95 28
    final public static function DaftObjectFromJsonArray(
96
        array $array,
97
        bool $writeAll = false
98
    ) : DaftJson {
99 28
        static::ThrowIfNotDaftJson();
100 12
        $array = static::ThrowIfJsonDefNotValid($array);
101 9
        $in = [];
102
103 9
        $jsonDef = static::DaftObjectJsonProperties();
104
105 9
        foreach (array_keys($array) as $prop) {
106 8
            if (isset($jsonDef[$prop])) {
107 5
                $jsonType = $jsonDef[$prop];
108
109 5
                if ('[]' === mb_substr($jsonType, -2)) {
110 3
                    $in[$prop] = static::DaftObjectFromJsonTypeArray(
111 3
                        mb_substr($jsonType, 0, -2),
112 3
                        $prop,
113 3
                        $array[$prop],
114 3
                        $writeAll
115
                    );
116
                } else {
117 2
                    $in[$prop] = static::DaftObjectFromJsonType(
118 2
                        $jsonType,
119 2
                        $array[$prop],
120 3
                        $writeAll
121
                    );
122
                }
123
            } else {
124 6
                $in[$prop] = $array[$prop];
125
            }
126
        }
127
128
        /**
129
        * @var DaftJson $out
130
        */
131 7
        $out = new static($in, $writeAll);
132
133 7
        return $out;
134
    }
135
136 22
    public static function DaftObjectFromJsonString(string $string) : DaftJson
137
    {
138 22
        static::ThrowIfNotDaftJson();
139
140 6
        return static::DaftObjectFromJsonArray(json_decode($string, true));
141
    }
142
143 75
    public function DaftObjectWormPropertyWritten(string $property) : bool
144
    {
145 75
        $wormProperties = $this->wormProperties;
146
147
        return
148 75
            ($this instanceof DaftObjectWorm) &&
149
            (
150 50
                $this->HasPropertyChanged($property) ||
151 75
                false === empty($wormProperties[$property])
152
            );
153
    }
154
155
    /**
156
    * @param array<string, mixed> $propVal
157
    *
158
    * @return DaftJson
159
    */
160 2
    protected static function DaftObjectFromJsonType(
161
        string $jsonType,
162
        array $propVal,
163
        bool $writeAll
164
    ) {
165 2
        static::ThrowIfNotJsonType($jsonType);
166
167 2
        return static::ArrayToJsonType($jsonType, $propVal, $writeAll);
168
    }
169
170
    /**
171
    * @return array<int, DaftJson>
172
    */
173 3
    protected static function DaftObjectFromJsonTypeArray(
174
        string $jsonType,
175
        string $prop,
176
        array $propVal,
177
        bool $writeAll
178
    ) : array {
179 3
        static::ThrowIfNotJsonType($jsonType);
180
181 2
        $out = [];
182
183 2
        foreach ($propVal as $val) {
184 2
            if (false === is_array($val)) {
185 1
                throw new PropertyNotJsonDecodableShouldBeArrayException($jsonType, $prop);
186
            }
187 1
            $out[] = static::ArrayToJsonType($jsonType, $val, $writeAll);
188
        }
189
190 1
        return $out;
191
    }
192
193
    /**
194
    * Retrieve a property from data.
195
    *
196
    * @param string $property the property being retrieved
197
    *
198
    * @throws PropertyNotNullableException if value is not set and $property is not listed as nullabe
199
    *
200
    * @return mixed the property value
201
    */
202 45
    protected function RetrievePropertyValueFromData(string $property)
203
    {
204
        if (
205 45
            false === array_key_exists($property, $this->data) &&
206 45
            false === in_array($property, static::NULLABLE_PROPERTIES, true)
207
        ) {
208 2
            throw new PropertyNotNullableException(static::class, $property);
209
        } elseif (
210 43
            in_array($property, static::NULLABLE_PROPERTIES, true)
211
        ) {
212 33
            return $this->data[$property] ?? null;
213
        }
214
215 43
        return $this->data[$property];
216
    }
217
218 134
    protected function NudgePropertyValue(string $property, $value) : void
219
    {
220 134
        $this->MaybeThrowOnNudge($property, $value);
221
222
        $isChanged = (
223 75
            false === array_key_exists($property, $this->data) ||
224 75
            $this->data[$property] !== $value
225
        );
226
227 75
        $this->data[$property] = $value;
228
229 75
        if ($isChanged && true !== isset($this->changedProperties[$property])) {
230 75
            $this->changedProperties[$property] = true;
231 75
            $this->wormProperties[$property] = true;
232
        }
233 75
    }
234
235
    /**
236
    * @param mixed $value
237
    *
238
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
239
    */
240 134
    private function MaybeThrowOnNudge(string $property, $value) : void
241
    {
242 134
        if (true !== in_array($property, static::PROPERTIES, true)) {
243 1
            throw new UndefinedPropertyException(static::class, $property);
244
        } elseif (
245 133
            true === is_null($value) &&
246 133
            true !== in_array($property, static::NULLABLE_PROPERTIES, true)
247
        ) {
248 58
            throw new PropertyNotNullableException(static::class, $property);
249 75
        } elseif ($this->DaftObjectWormPropertyWritten($property)) {
250 50
            throw new PropertyNotRewriteableException(static::class, $property);
251
        }
252 75
    }
253
254 12
    private static function ThrowIfJsonDefNotValid(array $array) : array
255
    {
256 12
        $jsonProps = static::DaftObjectJsonPropertyNames();
257
258 12
        $jsonDef = static::DaftObjectJsonProperties();
259
260 12
        $out = [];
261
262 12
        foreach ($array as $prop => $propVal) {
263
            if (
264 12
                false === in_array($prop, $jsonProps, true)
265
            ) {
266 1
                throw new PropertyNotJsonDecodableException(static::class, $prop);
267 12
            } elseif (false === is_null($propVal)) {
268 11
                if (isset($jsonDef[$prop])) {
269 8
                    $jsonType = $jsonDef[$prop];
270
271 8
                    if (false === is_array($propVal)) {
272 2
                        static::ThrowBecauseArrayJsonTypeNotValid($jsonType, $prop);
273
                    }
274
                }
275 10
                $out[$prop] = $propVal;
276
            }
277
        }
278
279 9
        return $out;
280
    }
281
282 2
    private static function ThrowBecauseArrayJsonTypeNotValid(
283
        string $jsonType,
284
        string $prop
285
    ) : void {
286 2
        if ('[]' === mb_substr($jsonType, -2)) {
287 1
            throw new PropertyNotJsonDecodableShouldBeArrayException(static::class, $prop);
288
        }
289 1
        throw new PropertyNotJsonDecodableShouldBeArrayException($jsonType, $prop);
290
    }
291
292 5
    private static function ThrowIfNotJsonType(string $jsonType) : void
293
    {
294 5
        if (false === is_a($jsonType, DaftJson::class, true)) {
295 1
            throw new ClassDoesNotImplementClassException($jsonType, DaftJson::class);
296
        }
297 4
    }
298
299 3
    private static function ArrayToJsonType(
300
        string $jsonType,
301
        array $propVal,
302
        bool $writeAll
303
    ) : DaftJson {
304
        /**
305
        * @var DaftJson $jsonType
306
        */
307 3
        $jsonType = $jsonType;
308
309 3
        return $jsonType::DaftObjectFromJsonArray($propVal, $writeAll);
310
    }
311
}
312