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 ( c62a1b...be4cfb )
by SignpostMarv
03:07
created

AbstractArrayBackedDaftObject::MaybeThrowOnNudge()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 3
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 3
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|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
        $out = [];
98
99
        /**
100
        * @var array<int, string> $properties
101
        */
102 12
        $properties = static::DaftObjectJsonPropertyNames();
103
104 12
        foreach ($properties as $property) {
105
            /**
106
            * @var scalar|null|array|object $val
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 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 12
        return static::DaftObjectFromJsonArray((array) json_decode($string, true));
148
    }
149
150 268
    public function DaftObjectWormPropertyWritten(string $property) : bool
151
    {
152 268
        $wormProperties = $this->wormProperties;
153
154
        return
155 268
            ($this instanceof DaftObjectWorm) &&
156
            (
157 136
                $this->HasPropertyChanged($property) ||
158 268
                isset($wormProperties[$property])
159
            );
160
    }
161
162 18
    final protected static function DaftJsonClosure(array $array, bool $writeAll) : Closure
163
    {
164 18
        $jsonDef = static::DaftObjectJsonProperties();
165
166
        return
167
            /**
168
            * @return mixed
169
            */
170
            function (string $prop) use ($array, $jsonDef, $writeAll) {
171
                /**
172
                * @var string|null $jsonType
173
                */
174 16
                $jsonType = $jsonDef[$prop] ?? null;
175
176 16
                if ( ! is_string($jsonType)) {
177 12
                    return $array[$prop];
178
                }
179
180 10
                return JsonTypeUtilities::DaftJsonFromJsonType($jsonType, $prop, (array) $array[$prop], $writeAll);
181 18
            };
182
    }
183
184
    /**
185
    * Retrieve a property from data.
186
    *
187
    * @param string $property the property being retrieved
188
    *
189
    * @throws PropertyNotNullableException if value is not set and $property is not listed as nullabe
190
    *
191
    * @return mixed the property value
192
    */
193 92
    protected function RetrievePropertyValueFromData(string $property)
194
    {
195
        /**
196
        * @var array<int, string> $properties
197
        */
198 92
        $properties = static::NULLABLE_PROPERTIES;
199
200
        if (
201 92
            false === array_key_exists($property, $this->data) &&
202 92
            false === in_array($property, $properties, true)
203
        ) {
204 4
            throw new PropertyNotNullableException(static::class, $property);
205
        } elseif (
206 88
            in_array($property, $properties, true)
207
        ) {
208 66
            return $this->data[$property] ?? null;
209
        }
210
211 88
        return $this->data[$property];
212
    }
213
214
    /**
215
    * @param scalar|null|array|object $value
216
    */
217 270
    protected function NudgePropertyValue(string $property, $value) : void
218
    {
219
        /**
220
        * @var array<int, string> $nullables
221
        */
222 270
        $nullables = static::NULLABLE_PROPERTIES;
223
224 270
        $this->MaybeThrowForPropertyOnNudge($property);
225 268
        $this->MaybeThrowOnNudge($property, $value, $nullables);
226
227
        $isChanged = (
228 152
            false === array_key_exists($property, $this->data) ||
229 152
            $this->data[$property] !== $value
230
        );
231
232 152
        $this->data[$property] = $value;
233
234 152
        if ($isChanged && true !== isset($this->changedProperties[$property])) {
235 152
            $this->changedProperties[$property] = true;
236 152
            $this->wormProperties[$property] = true;
237
        }
238 152
    }
239
240
    /**
241
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
242
    */
243 270
    private function MaybeThrowForPropertyOnNudge(string $property) : void
244
    {
245
        /**
246
        * @var array<int, string> $properties
247
        */
248 270
        $properties = static::PROPERTIES;
249
250 270
        if (true !== in_array($property, $properties, true)) {
251 2
            throw new UndefinedPropertyException(static::class, $property);
252 268
        } elseif ($this->DaftObjectWormPropertyWritten($property)) {
253 100
            throw new PropertyNotRewriteableException(static::class, $property);
254
        }
255 268
    }
256
257
    /**
258
    * @param mixed $value
259
    *
260
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
261
    */
262 268
    private function MaybeThrowOnNudge(string $property, $value, array $properties) : void
263
    {
264 268
        if (true === is_null($value) && true !== in_array($property, $properties, true)) {
265 116
            throw new PropertyNotNullableException(static::class, $property);
266
        }
267 152
    }
268
}
269