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 — object-only ( 98fa5f...0d8579 )
by SignpostMarv
10:19
created

AbstractArrayBackedDaftObject::DaftJsonClosure()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 34
ccs 15
cts 15
cp 1
rs 9.7333
c 0
b 0
f 0
cc 4
nc 1
nop 2
crap 4
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 332
    public function __construct(array $data = [], bool $writeAll = false)
49
    {
50 332
        if (true === $writeAll) {
51 124
            foreach ($data as $k => $v) {
52 124
                $this->__set($k, $v);
53
            }
54
        } else {
55 208
            foreach ($data as $k => $v) {
56 100
                $this->data[$k] = $v;
57
            }
58
        }
59 324
    }
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 44
    public function jsonSerialize() : array
90
    {
91
        /**
92
        * @var array<int, string>
93
        */
94 44
        $properties = static::DaftObjectJsonPropertyNames();
95
96
        /**
97
        * @var array<string, string>
98
        */
99 8
        $properties = array_combine($properties, $properties);
100
101 8
        return array_filter(
102 6
            array_map(
103
                /**
104
                * @return mixed
105
                */
106
                function (string $property) {
107 8
                    return $this->DoGetSet($property, false);
108 8
                },
109 4
                $properties
110
            ),
111
            /**
112
            * @param mixed $maybe
113
            */
114
            function ($maybe) : bool {
115 8
                return ! is_null($maybe);
116 8
            }
117
        );
118
    }
119
120 56
    final public static function DaftObjectFromJsonArray(
121
        array $array,
122
        bool $writeAll = self::BOOL_DEFAULT_WRITEALL
123
    ) : DaftJson {
124 56
        $type = JsonTypeUtilities::ThrowIfNotDaftJson(static::class);
125
126 20
        $array = JsonTypeUtilities::ThrowIfJsonDefNotValid($type, $array);
127
128
        /**
129
        * @var array<int, string>
130
        */
131 20
        $props = array_keys($array);
132 20
        $mapper = static::DaftJsonClosure($array, $writeAll);
133
134
        /**
135
        * @var array<int, scalar|object|array|null>
136
        */
137 20
        $vals = array_map($mapper, $props);
138
139 16
        return new $type(array_combine($props, $vals), $writeAll);
140
    }
141
142 84
    public static function DaftObjectFromJsonString(string $string) : DaftJson
143
    {
144
        /**
145
        * @var scalar|array|object|null
146
        */
147 84
        $decoded = json_decode($string, true);
148
149 84
        return JsonTypeUtilities::ThrowIfNotDaftJson(static::class)::DaftObjectFromJsonArray(
150 12
            is_array($decoded) ? $decoded : [$decoded]
151
        );
152
    }
153
154 264
    public function DaftObjectWormPropertyWritten(string $property) : bool
155
    {
156 264
        $wormProperties = $this->wormProperties;
157
158
        return
159 264
            ($this instanceof DaftObjectWorm) &&
160
            (
161 136
                $this->HasPropertyChanged($property) ||
162 264
                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 60
    protected function RetrievePropertyValueFromData(string $property)
176
    {
177 60
        $isNullable = in_array(
178 45
            $property,
179 60
            static::DaftObjectNullableProperties(),
180 60
            DefinitionAssistant::IN_ARRAY_STRICT_MODE
181
        );
182
183 60
        if ( ! array_key_exists($property, $this->data) && ! $isNullable) {
184 4
            throw new PropertyNotNullableException(static::class, $property);
185 56
        } elseif ($isNullable) {
186 16
            return $this->data[$property] ?? null;
187
        }
188
189 56
        return $this->data[$property];
190
    }
191
192
    /**
193
    * @param scalar|array|object|null $value
194
    */
195 268
    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 268
        $nullables = static::DaftObjectNullableProperties();
205
206 268
        $this->MaybeThrowOnNudge($property, $value, $nullables);
207
208 180
        $value = $this->MaybeModifyValueBeforeNudge(
209 135
            $property,
210 90
            $value,
211 90
            $autoTrimStrings,
212 90
            $throwIfNotUnique
213
        );
214
215
        $isChanged = (
216 164
            ! array_key_exists($property, $this->data) ||
217 164
            $this->data[$property] !== $value
218
        );
219
220 164
        $this->data[$property] = $value;
221
222 164
        if ($isChanged && true !== isset($this->changedProperties[$property])) {
223 164
            $this->changedProperties[$property] = $this->wormProperties[$property] = true;
224
        }
225 164
    }
226
227 20
    private static function DaftJsonClosure(array $array, bool $writeAll) : Closure
228
    {
229 20
        $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 16
                $jsonType = $jsonDef[$prop] ?? null;
240
241 16
                if ( ! is_string($jsonType)) {
242 12
                    return $array[$prop];
243
                }
244
245 8
                $propVal = (is_array($array[$prop]) ? $array[$prop] : [$array[$prop]]);
246
247 8
                if ('[]' === mb_substr($jsonType, -2)) {
248 8
                    $jsonType = JsonTypeUtilities::ThrowIfNotJsonType(mb_substr($jsonType, 0, -2));
249
250 4
                    return JsonTypeUtilities::DaftObjectFromJsonTypeArray(
251 3
                        $jsonType,
252 2
                        $prop,
253 2
                        $propVal,
254 2
                        $writeAll
255
                    );
256
                }
257
258 4
                $jsonType = JsonTypeUtilities::ThrowIfNotJsonType($jsonType);
259
260 4
                return JsonTypeUtilities::DaftJsonFromJsonType($jsonType, $propVal, $writeAll);
261 20
            };
262
    }
263
264
    /**
265
    * @param scalar|array|object|null $value
266
    *
267
    * @return scalar|array|object|null
268
    */
269 180
    private function MaybeModifyValueBeforeNudge(
270
        string $property,
271
        $value,
272
        bool $autoTrimStrings = self::BOOL_DEFAULT_AUTOTRIMSTRINGS,
273
        bool $throwIfNotUnique = self::BOOL_DEFAULT_THROWIFNOTUNIQUE
274
    ) {
275 180
        $spec = null;
276
277
        if (
278 180
            is_a(
279 180
                static::class,
280 180
                DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues::class,
281 180
                true
282
            )
283
        ) {
284
            $spec = (
285 32
                static::DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues()[$property] ?? null
286
            );
287
        }
288
289 180
        if (is_array($spec)) {
290 28
            $value = DefinitionAssistant::MaybeThrowIfValueDoesNotMatchMultiTypedArray(
291 21
                $autoTrimStrings,
292 21
                $throwIfNotUnique,
293 21
                $value,
294 16
                ...$spec
295
            );
296
        }
297
298 164
        if (is_string($value) && $autoTrimStrings) {
299 4
            $value = trim($value);
300
        }
301
302 164
        return $value;
303
    }
304
305
    /**
306
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
307
    */
308 268
    private function MaybeThrowForPropertyOnNudge(string $property) : string
309
    {
310 268
        $properties = static::DaftObjectProperties();
311
312 268
        if ( ! in_array($property, $properties, DefinitionAssistant::IN_ARRAY_STRICT_MODE)) {
313 4
            throw new UndefinedPropertyException(static::class, $property);
314 264
        } elseif ($this->DaftObjectWormPropertyWritten($property)) {
315 100
            throw new PropertyNotRewriteableException(static::class, $property);
316
        }
317
318 264
        return $property;
319
    }
320
321
    /**
322
    * @param mixed $value
323
    *
324
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
325
    */
326 268
    private function MaybeThrowOnNudge(string $property, $value, array $properties) : void
327
    {
328 268
        $property = $this->MaybeThrowForPropertyOnNudge($property);
329
330
        if (
331 264
            true === is_null($value) &&
332 230
            ! in_array($property, $properties, DefinitionAssistant::IN_ARRAY_STRICT_MODE)
333
        ) {
334 84
            throw new PropertyNotNullableException(static::class, $property);
335
        }
336 180
    }
337
}
338