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 ( 0ac302...d1ed7d )
by SignpostMarv
05:21
created

AbstractArrayBackedDaftObject::__isset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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