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 ( 1a4436...b0f090 )
by SignpostMarv
03:44
created

AbstractArrayBackedDaftObject   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 259
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 259
ccs 98
cts 98
cp 1
rs 8.6
c 0
b 0
f 0
wmc 37

17 Methods

Rating   Name   Duplication   Size   Complexity  
A MakePropertiesUnchanged() 0 4 2
A DaftObjectFromJsonArray() 0 15 1
A __isset() 0 5 2
A DaftObjectFromJsonString() 0 5 1
A jsonSerialize() 0 15 3
A ChangedProperties() 0 3 1
A DaftObjectWormPropertyWritten() 0 9 3
A __construct() 0 11 4
A HasPropertyChanged() 0 5 2
A DaftObjectFromJsonType() 0 5 1
A DaftJsonClosure() 0 16 2
A DaftObjectFromJsonTypeArray() 0 18 3
A RetrievePropertyValueFromData() 0 14 4
A DaftJsonFromJsonType() 0 13 2
A NudgePropertyValue() 0 20 4
A ThrowIfJsonDefNotValid() 0 11 1
A ArrayToJsonType() 0 8 1
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
13
/**
14
* Array-backed daft objects.
15
*/
16
abstract class AbstractArrayBackedDaftObject extends AbstractDaftObject implements DaftObjectCreatedByArray
17
{
18
    /**
19
    * data for this instance.
20
    *
21
    * @var array<string, mixed>
22
    */
23
    private $data = [];
24
25
    /**
26
    * List of changed properties.
27
    *
28
    * @var array<string, bool>
29
    */
30
    private $changedProperties = [];
31
32
    /**
33
    * List of changed properties, for write-once read-many.
34
    *
35
    * @var array<string, bool>
36
    */
37
    private $wormProperties = [];
38
39 376
    public function __construct(array $data = [], bool $writeAll = false)
40
    {
41 376
        parent::__construct();
42
43 372
        if (true === $writeAll) {
44 122
            foreach ($data as $k => $v) {
45 122
                $this->__set($k, $v);
46
            }
47
        } else {
48 254
            foreach ($data as $k => $v) {
49 132
                $this->data[$k] = $v;
50
            }
51
        }
52 368
    }
53
54 62
    public function __isset(string $property) : bool
55
    {
56
        return
57 62
            in_array($property, static::PROPERTIES, true) &&
58 62
            isset($this->data, $this->data[$property]);
59
    }
60
61 58
    public function ChangedProperties() : array
62
    {
63 58
        return array_keys($this->changedProperties);
64
    }
65
66 66
    public function MakePropertiesUnchanged(string ...$properties) : void
67
    {
68 66
        foreach ($properties as $property) {
69 66
            unset($this->changedProperties[$property]);
70
        }
71 66
    }
72
73 200
    public function HasPropertyChanged(string $property) : bool
74
    {
75
        return
76 200
            isset($this->changedProperties[$property]) &&
77 200
            true === $this->changedProperties[$property];
78
    }
79
80 44
    public function jsonSerialize() : array
81
    {
82 44
        TypeUtilities::ThrowIfNotDaftJson(static::class);
83
84 12
        $out = [];
85
86 12
        foreach (static::DaftObjectJsonPropertyNames() as $property) {
87 12
            $val = $this->DoGetSet($property, false);
88
89 12
            if (false === is_null($val)) {
90 12
                $out[$property] = $val;
91
            }
92
        }
93
94 12
        return $out;
95
    }
96
97 56
    final public static function DaftObjectFromJsonArray(
98
        array $array,
99
        bool $writeAll = false
100
    ) : DaftJson {
101 56
        TypeUtilities::ThrowIfNotDaftJson(static::class);
102 24
        $array = static::ThrowIfJsonDefNotValid($array);
103 18
        $props = array_keys($array);
104 18
        $mapper = static::DaftJsonClosure($array, $writeAll);
105
106
        /**
107
        * @var DaftJson $out
108
        */
109 18
        $out = new static(array_combine($props, array_map($mapper, $props)), $writeAll);
110
111 14
        return $out;
112
    }
113
114 44
    public static function DaftObjectFromJsonString(string $string) : DaftJson
115
    {
116 44
        TypeUtilities::ThrowIfNotDaftJson(static::class);
117
118 12
        return static::DaftObjectFromJsonArray(json_decode($string, true));
119
    }
120
121 136
    public function DaftObjectWormPropertyWritten(string $property) : bool
122
    {
123 136
        $wormProperties = $this->wormProperties;
124
125
        return
126 136
            ($this instanceof DaftObjectWorm) &&
127
            (
128 136
                $this->HasPropertyChanged($property) ||
129 136
                false === empty($wormProperties[$property])
130
            );
131
    }
132
133 18
    final protected static function DaftJsonClosure(array $array, bool $writeAll) : Closure
134
    {
135 18
        $jsonDef = static::DaftObjectJsonProperties();
136
137
        return
138
            /**
139
            * @return mixed
140
            */
141
            function (string $prop) use ($array, $jsonDef, $writeAll) {
142 16
                $jsonType = $jsonDef[$prop] ?? null;
143
144 16
                if ( ! is_string($jsonType)) {
145 12
                    return $array[$prop];
146
                }
147
148 10
                return static::DaftJsonFromJsonType($jsonType, $prop, $array[$prop], $writeAll);
149 18
            };
150
    }
151
152
    /**
153
    * @return array<int, DaftJson>|DaftJson
154
    */
155 10
    final protected static function DaftJsonFromJsonType(
156
        string $jsonType,
157
        string $prop,
158
        array $propVal,
159
        bool $writeAll
160
    ) {
161 10
        if ('[]' === mb_substr($jsonType, -2)) {
162 6
            $jsonType = mb_substr($jsonType, 0, -2);
163
164 6
            return static::DaftObjectFromJsonTypeArray($jsonType, $prop, $propVal, $writeAll);
165
        }
166
167 4
        return static::DaftObjectFromJsonType($jsonType, $propVal, $writeAll);
168
    }
169
170
    /**
171
    * @param array<string, mixed> $propVal
172
    *
173
    * @return DaftJson
174
    */
175 4
    protected static function DaftObjectFromJsonType(string $type, array $propVal, bool $writeAll)
176
    {
177 4
        TypeUtilities::ThrowIfNotJsonType($type);
178
179 4
        return static::ArrayToJsonType($type, $propVal, $writeAll);
180
    }
181
182
    /**
183
    * @return array<int, DaftJson>
184
    */
185 6
    protected static function DaftObjectFromJsonTypeArray(
186
        string $jsonType,
187
        string $prop,
188
        array $propVal,
189
        bool $writeAll
190
    ) : array {
191 6
        TypeUtilities::ThrowIfNotJsonType($jsonType);
192
193 4
        $out = [];
194
195 4
        foreach ($propVal as $val) {
196 4
            if (false === is_array($val)) {
197 2
                throw new PropertyNotJsonDecodableShouldBeArrayException($jsonType, $prop);
198
            }
199 2
            $out[] = static::ArrayToJsonType($jsonType, $val, $writeAll);
200
        }
201
202 2
        return $out;
203
    }
204
205
    /**
206
    * Retrieve a property from data.
207
    *
208
    * @param string $property the property being retrieved
209
    *
210
    * @throws PropertyNotNullableException if value is not set and $property is not listed as nullabe
211
    *
212
    * @return mixed the property value
213
    */
214 90
    protected function RetrievePropertyValueFromData(string $property)
215
    {
216
        if (
217 90
            false === array_key_exists($property, $this->data) &&
218 90
            false === in_array($property, static::NULLABLE_PROPERTIES, true)
219
        ) {
220 4
            throw new PropertyNotNullableException(static::class, $property);
221
        } elseif (
222 86
            in_array($property, static::NULLABLE_PROPERTIES, true)
223
        ) {
224 66
            return $this->data[$property] ?? null;
225
        }
226
227 86
        return $this->data[$property];
228
    }
229
230 268
    protected function NudgePropertyValue(string $property, $value) : void
231
    {
232 268
        TypeUtilities::MaybeThrowForPropertyOnNudge($this, static::PROPERTIES, $property);
233 266
        TypeUtilities::MaybeThrowOnNudge(
234 266
            static::class,
235 266
            $property,
236 266
            $value,
237 266
            static::NULLABLE_PROPERTIES
238
        );
239
240
        $isChanged = (
241 150
            false === array_key_exists($property, $this->data) ||
242 150
            $this->data[$property] !== $value
243
        );
244
245 150
        $this->data[$property] = $value;
246
247 150
        if ($isChanged && true !== isset($this->changedProperties[$property])) {
248 150
            $this->changedProperties[$property] = true;
249 150
            $this->wormProperties[$property] = true;
250
        }
251 150
    }
252
253 24
    private static function ThrowIfJsonDefNotValid(array $array) : array
254
    {
255 24
        $jsonProps = static::DaftObjectJsonPropertyNames();
256 24
        $array = TypeUtilities::FilterThrowIfJsonDefNotValid(static::class, $jsonProps, $array);
257 22
        $jsonDef = static::DaftObjectJsonProperties();
258
259 22
        $keys = array_keys($array);
260
261 22
        return array_combine($keys, array_map(
262 22
            TypeUtilities::MakeMapperThrowIfJsonDefNotValid(static::class, $jsonDef, $array),
263 22
            $keys
264
        ));
265
    }
266
267 6
    private static function ArrayToJsonType(string $type, array $value, bool $writeAll) : DaftJson
268
    {
269
        /**
270
        * @var DaftJson $type
271
        */
272 6
        $type = $type;
273
274 6
        return $type::DaftObjectFromJsonArray($value, $writeAll);
275
    }
276
}
277