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 — master ( c98808...85fe9f )
by SignpostMarv
02:33
created

AbstractArrayBackedDaftObject::ChangedProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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