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.
Completed
Push — master ( be4cfb...0ac302 )
by SignpostMarv
09:29
created

DaftObjectWormPropertyWritten()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 3
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|null|array|object> $data
42
    */
43 430
    public function __construct(array $data = [], bool $writeAll = false)
44
    {
45 430
        parent::__construct();
46
47 426
        if (true === $writeAll) {
48 148
            foreach ($data as $k => $v) {
49 98
                if ( ! is_string($k)) {
50 26
                    throw new InvalidArgumentException(DaftObjectCreatedByArray::ERR_KEY_NOT_STRING);
51
                }
52 122
                $this->__set($k, $v);
53
            }
54
        } else {
55 282
            foreach ($data as $k => $v) {
56 158
                if ( ! is_string($k)) {
57 26
                    throw new InvalidArgumentException(DaftObjectCreatedByArray::ERR_KEY_NOT_STRING);
58
                }
59 132
                $this->data[$k] = $v;
60
            }
61
        }
62 370
    }
63
64 62
    public function __isset(string $property) : bool
65
    {
66
        /**
67
        * @var array<int, string>|string $properties
68
        */
69 62
        $properties = static::PROPERTIES;
70
71
        return
72 62
            in_array($property, (array) $properties, true) &&
73 62
            isset($this->data, $this->data[$property]);
74
    }
75
76 58
    public function ChangedProperties() : array
77
    {
78 58
        return array_keys($this->changedProperties);
79
    }
80
81 66
    public function MakePropertiesUnchanged(string ...$properties) : void
82
    {
83 66
        foreach ($properties as $property) {
84 66
            unset($this->changedProperties[$property]);
85
        }
86 66
    }
87
88 202
    public function HasPropertyChanged(string $property) : bool
89
    {
90 202
        return $this->changedProperties[$property] ?? false;
91
    }
92
93 44
    public function jsonSerialize() : array
94
    {
95 44
        JsonTypeUtilities::ThrowIfNotDaftJson(static::class);
96
97 12
        $properties = static::DaftObjectJsonPropertyNames();
98
99 12
        return array_filter(
100 12
            array_map(
101
                /**
102
                * @return scalar|null|array|object
103
                */
104
                function (string $property) {
105 12
                    return $this->DoGetSet($property, false);
106 12
                },
107 12
                array_combine($properties, $properties)
108
            ),
109
            /**
110
            * @param scalar|null|array|object $val
111
            */
112
            function ($val) : bool {
113 12
                return ! is_null($val);
114 12
            }
115
        );
116
    }
117
118 56
    final public static function DaftObjectFromJsonArray(
119
        array $array,
120
        bool $writeAll = false
121
    ) : DaftJson {
122 56
        $array = JsonTypeUtilities::ThrowIfJsonDefNotValid(static::class, $array);
123
124
        /**
125
        * @var array<int, string> $props
126
        */
127 18
        $props = array_keys($array);
128 18
        $mapper = static::DaftJsonClosure($array, $writeAll);
129
130
        /**
131
        * @var array<int, scalar|null|object|array> $vals
132
        */
133 18
        $vals = array_map($mapper, $props);
134
135
        /**
136
        * @var DaftJson $out
137
        */
138 14
        $out = new static(array_combine($props, $vals), $writeAll);
139
140 14
        return $out;
141
    }
142
143 44
    public static function DaftObjectFromJsonString(string $string) : DaftJson
144
    {
145 44
        JsonTypeUtilities::ThrowIfNotDaftJson(static::class);
146
147
        /**
148
        * @var array|scalar|object $out
149
        */
150 12
        $out = json_decode($string, true);
151
152 12
        if ( ! is_array($out)) {
153
            throw new InvalidArgumentException(sprintf(
154
                'Argument 1 passed to %s does not decode to an array!',
155
                __METHOD__
156
            ));
157
        }
158
159 12
        return static::DaftObjectFromJsonArray($out);
160
    }
161
162 268
    public function DaftObjectWormPropertyWritten(string $property) : bool
163
    {
164 268
        $wormProperties = $this->wormProperties;
165
166
        return
167 268
            ($this instanceof DaftObjectWorm) &&
168
            (
169 136
                $this->HasPropertyChanged($property) ||
170 268
                isset($wormProperties[$property])
171
            );
172
    }
173
174 18
    final protected static function DaftJsonClosure(array $array, bool $writeAll) : Closure
175
    {
176 18
        $jsonDef = static::DaftObjectJsonProperties();
177
178
        return
179
            /**
180
            * @return mixed
181
            */
182
            function (string $prop) use ($array, $jsonDef, $writeAll) {
183
                /**
184
                * @var string|null $jsonType
185
                */
186 16
                $jsonType = $jsonDef[$prop] ?? null;
187
188 16
                if ( ! is_string($jsonType)) {
189 12
                    return $array[$prop];
190 10
                } elseif ( ! is_array($array[$prop])) {
191
                    throw new InvalidArgumentException(
192
                        'Array was expected to have contained an array, did not contain an array.'
193
                    );
194
                }
195
196 10
                return JsonTypeUtilities::DaftJsonFromJsonType($jsonType, $prop, $array[$prop], $writeAll);
197 18
            };
198
    }
199
200
    /**
201
    * Retrieve a property from data.
202
    *
203
    * @param string $property the property being retrieved
204
    *
205
    * @throws PropertyNotNullableException if value is not set and $property is not listed as nullabe
206
    *
207
    * @return mixed the property value
208
    */
209 92
    protected function RetrievePropertyValueFromData(string $property)
210
    {
211
        /**
212
        * @var array<int, string> $properties
213
        */
214 92
        $properties = static::NULLABLE_PROPERTIES;
215
216
        if (
217 92
            false === array_key_exists($property, $this->data) &&
218 92
            false === in_array($property, $properties, true)
219
        ) {
220 4
            throw new PropertyNotNullableException(static::class, $property);
221
        } elseif (
222 88
            in_array($property, $properties, true)
223
        ) {
224 66
            return $this->data[$property] ?? null;
225
        }
226
227 88
        return $this->data[$property];
228
    }
229
230
    /**
231
    * @param scalar|null|array|object $value
232
    */
233 270
    protected function NudgePropertyValue(string $property, $value) : void
234
    {
235
        /**
236
        * @var array<int, string> $nullables
237
        */
238 270
        $nullables = static::NULLABLE_PROPERTIES;
239
240 270
        $this->MaybeThrowForPropertyOnNudge($property);
241 268
        $this->MaybeThrowOnNudge($property, $value, $nullables);
242
243
        $isChanged = (
244 152
            false === array_key_exists($property, $this->data) ||
245 152
            $this->data[$property] !== $value
246
        );
247
248 152
        $this->data[$property] = $value;
249
250 152
        if ($isChanged && true !== isset($this->changedProperties[$property])) {
251 152
            $this->changedProperties[$property] = true;
252 152
            $this->wormProperties[$property] = true;
253
        }
254 152
    }
255
256
    /**
257
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
258
    */
259 270
    private function MaybeThrowForPropertyOnNudge(string $property) : void
260
    {
261
        /**
262
        * @var array<int, string> $properties
263
        */
264 270
        $properties = static::PROPERTIES;
265
266 270
        if (true !== in_array($property, $properties, true)) {
267 2
            throw new UndefinedPropertyException(static::class, $property);
268 268
        } elseif ($this->DaftObjectWormPropertyWritten($property)) {
269 100
            throw new PropertyNotRewriteableException(static::class, $property);
270
        }
271 268
    }
272
273
    /**
274
    * @param mixed $value
275
    *
276
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
277
    */
278 268
    private function MaybeThrowOnNudge(string $property, $value, array $properties) : void
279
    {
280 268
        if (true === is_null($value) && true !== in_array($property, $properties, true)) {
281 116
            throw new PropertyNotNullableException(static::class, $property);
282
        }
283 152
    }
284
}
285