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 — php7.3 ( a4fbeb...9ddd99 )
by SignpostMarv
06:05
created

NudgePropertyValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

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