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 ( 3b7ec1...327507 )
by SignpostMarv
02:50
created

DaftObjectFromJsonTypeArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 4
dl 0
loc 18
ccs 0
cts 8
cp 0
crap 12
rs 9.4285
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
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
        JsonTypeUtilities::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
        JsonTypeUtilities::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
        JsonTypeUtilities::ThrowIfNotDaftJson(static::class);
117
118 12
        return static::DaftObjectFromJsonArray(json_decode($string, true));
119
    }
120
121 266
    public function DaftObjectWormPropertyWritten(string $property) : bool
122
    {
123 266
        $wormProperties = $this->wormProperties;
124
125
        return
126 266
            ($this instanceof DaftObjectWorm) &&
127
            (
128 136
                $this->HasPropertyChanged($property) ||
129 266
                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 JsonTypeUtilities::DaftJsonFromJsonType($jsonType, $prop, $array[$prop], $writeAll);
149 18
            };
150
    }
151
152
    /**
153
    * Retrieve a property from data.
154
    *
155
    * @param string $property the property being retrieved
156
    *
157
    * @throws PropertyNotNullableException if value is not set and $property is not listed as nullabe
158
    *
159
    * @return mixed the property value
160
    */
161 90
    protected function RetrievePropertyValueFromData(string $property)
162
    {
163
        if (
164 90
            false === array_key_exists($property, $this->data) &&
165 90
            false === in_array($property, static::NULLABLE_PROPERTIES, true)
166
        ) {
167 4
            throw new PropertyNotNullableException(static::class, $property);
168
        } elseif (
169 86
            in_array($property, static::NULLABLE_PROPERTIES, true)
170
        ) {
171 66
            return $this->data[$property] ?? null;
172
        }
173
174 86
        return $this->data[$property];
175
    }
176
177 268
    protected function NudgePropertyValue(string $property, $value) : void
178
    {
179 268
        $this->MaybeThrowForPropertyOnNudge($property);
180 266
        $this->MaybeThrowOnNudge($property, $value, static::NULLABLE_PROPERTIES);
181
182
        $isChanged = (
183 150
            false === array_key_exists($property, $this->data) ||
184 150
            $this->data[$property] !== $value
185
        );
186
187 150
        $this->data[$property] = $value;
188
189 150
        if ($isChanged && true !== isset($this->changedProperties[$property])) {
190 150
            $this->changedProperties[$property] = true;
191 150
            $this->wormProperties[$property] = true;
192
        }
193 150
    }
194
195
    /**
196
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
197
    */
198 268
    private function MaybeThrowForPropertyOnNudge(string $property) : void
199
    {
200 268
        if (true !== in_array($property, static::PROPERTIES, true)) {
201 2
            throw new UndefinedPropertyException(static::class, $property);
202 266
        } elseif ($this->DaftObjectWormPropertyWritten($property)) {
203 100
            throw new PropertyNotRewriteableException(static::class, $property);
204
        }
205 266
    }
206
207
    /**
208
    * @param mixed $value
209
    *
210
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
211
    */
212 266
    private function MaybeThrowOnNudge(string $property, $value, array $properties) : void
213
    {
214 266
        if (true === is_null($value) && true !== in_array($property, $properties, true)) {
215 116
            throw new PropertyNotNullableException(static::class, $property);
216
        }
217 150
    }
218
219 24
    private static function ThrowIfJsonDefNotValid(array $array) : array
220
    {
221 24
        $jsonProps = static::DaftObjectJsonPropertyNames();
222 24
        $array = JsonTypeUtilities::FilterThrowIfJsonDefNotValid(static::class, $jsonProps, $array);
223 22
        $jsonDef = static::DaftObjectJsonProperties();
224
225 22
        $keys = array_keys($array);
226
227 22
        return array_combine($keys, array_map(
228 22
            JsonTypeUtilities::MakeMapperThrowIfJsonDefNotValid(static::class, $jsonDef, $array),
229 22
            $keys
230
        ));
231
    }
232
}
233