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 ( 3bc013...b16dc9 )
by SignpostMarv
02:15
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
        /**
232
        * @var array<int, string>|null
233
        */
234 178
        $spec = null;
235
236
        if (
237 178
            is_a(
238 178
                static::class,
239 178
                DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues::class,
240 178
                true
241
            )
242
        ) {
243
            $spec = (
244 16
                static::DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues()[$property] ?? null
245
            );
246
        }
247
248 178
        if (is_array($spec)) {
249 14
            $value = TypeUtilities::MaybeThrowIfValueDoesNotMatchMultiTypedArray(
250 14
                $autoTrimStrings,
251 14
                $throwIfNotUnique,
252 14
                $value,
253 14
                ...$spec
254 10
            );
255
        }
256
257
        if (is_string($value) && $autoTrimStrings) {
258 170
            $value = trim($value);
259 2
        }
260
261
        $isChanged = (
262
            ! array_key_exists($property, $this->data) ||
263 170
            $this->data[$property] !== $value
264 170
        );
265
266
        $this->data[$property] = $value;
267 170
268
        if ($isChanged && true !== isset($this->changedProperties[$property])) {
269 170
            $this->changedProperties[$property] = $this->wormProperties[$property] = true;
270 170
        }
271
    }
272 170
273
    /**
274
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
275
    */
276
    protected function MaybeThrowForPropertyOnNudge(string $property) : void
277 318
    {
278
        /**
279
        * @var array<int, string>
280
        */
281
        $properties = static::PROPERTIES;
282 318
283
        if (true !== in_array($property, $properties, true)) {
284 318
            throw new UndefinedPropertyException(static::class, $property);
285 2
        } elseif ($this->DaftObjectWormPropertyWritten($property)) {
286 316
            throw new PropertyNotRewriteableException(static::class, $property);
287 100
        }
288
    }
289 316
290
    /**
291
    * @param mixed $value
292
    *
293
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
294
    */
295
    protected function MaybeThrowOnNudge(string $property, $value, array $properties) : void
296 316
    {
297
        if (true === is_null($value) && true !== in_array($property, $properties, true)) {
298 316
            throw new PropertyNotNullableException(static::class, $property);
299 138
        }
300
    }
301
}
302