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 ( a6fddc...bed791 )
by SignpostMarv
02:44
created

AbstractArrayBackedDaftObject::__construct()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

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