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
Branch object-only (2b4c07)
by SignpostMarv
14:54
created

DaftObjectFromJsonArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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