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.
Test Failed
Push — master ( a8e98e...bc0512 )
by SignpostMarv
04:44
created

eFromDataExpectFloatish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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