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 ( 3f7dd9...cdf6e2 )
by SignpostMarv
06:43 queued 10s
created

AbstractArrayBackedDaftObject::__construct()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.027

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 2
dl 0
loc 17
ccs 10
cts 11
cp 0.9091
crap 6.027
rs 9.0777
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 398
    public function __construct(array $data = [], bool $writeAll = false)
41
    {
42 398
        parent::__construct();
43
44 394
        if (true === $writeAll) {
45 122
            foreach ($data as $k => $v) {
46 72
                if ( ! is_string($k)) {
47
                    throw new InvalidArgumentException(DaftObjectCreatedByArray::ERR_KEY_NOT_STRING);
48
                }
49 122
                $this->__set($k, $v);
50
            }
51
        } else {
52 276
            foreach ($data as $k => $v) {
53 156
                if ( ! is_string($k)) {
54 26
                    throw new InvalidArgumentException(DaftObjectCreatedByArray::ERR_KEY_NOT_STRING);
55
                }
56 130
                $this->data[$k] = $v;
57
            }
58
        }
59 364
    }
60
61 62
    public function __isset(string $property) : bool
62
    {
63
        return
64 62
            in_array($property, static::PROPERTIES, true) &&
65 62
            isset($this->data, $this->data[$property]);
66
    }
67
68 58
    public function ChangedProperties() : array
69
    {
70 58
        return array_keys($this->changedProperties);
71
    }
72
73 66
    public function MakePropertiesUnchanged(string ...$properties) : void
74
    {
75 66
        foreach ($properties as $property) {
76 66
            unset($this->changedProperties[$property]);
77
        }
78 66
    }
79
80 200
    public function HasPropertyChanged(string $property) : bool
81
    {
82
        return
83 200
            isset($this->changedProperties[$property]) &&
84 200
            true === $this->changedProperties[$property];
85
    }
86
87 44
    public function jsonSerialize() : array
88
    {
89 44
        JsonTypeUtilities::ThrowIfNotDaftJson(static::class);
90
91 12
        $out = [];
92
93 12
        foreach (static::DaftObjectJsonPropertyNames() as $property) {
94 12
            $val = $this->DoGetSet($property, false);
95
96 12
            if (false === is_null($val)) {
97 12
                $out[$property] = $val;
98
            }
99
        }
100
101 12
        return $out;
102
    }
103
104 56
    final public static function DaftObjectFromJsonArray(
105
        array $array,
106
        bool $writeAll = false
107
    ) : DaftJson {
108 56
        $array = JsonTypeUtilities::ThrowIfJsonDefNotValid(static::class, $array);
109 18
        $props = array_keys($array);
110 18
        $mapper = static::DaftJsonClosure($array, $writeAll);
111
112
        /**
113
        * @var DaftJson $out
114
        */
115 18
        $out = new static(array_combine($props, array_map($mapper, $props)), $writeAll);
116
117 14
        return $out;
118
    }
119
120 44
    public static function DaftObjectFromJsonString(string $string) : DaftJson
121
    {
122 44
        JsonTypeUtilities::ThrowIfNotDaftJson(static::class);
123
124 12
        return static::DaftObjectFromJsonArray(json_decode($string, true));
125
    }
126
127 266
    public function DaftObjectWormPropertyWritten(string $property) : bool
128
    {
129 266
        $wormProperties = $this->wormProperties;
130
131
        return
132 266
            ($this instanceof DaftObjectWorm) &&
133
            (
134 136
                $this->HasPropertyChanged($property) ||
135 266
                isset($wormProperties[$property])
136
            );
137
    }
138
139 18
    final protected static function DaftJsonClosure(array $array, bool $writeAll) : Closure
140
    {
141 18
        $jsonDef = static::DaftObjectJsonProperties();
142
143
        return
144
            /**
145
            * @return mixed
146
            */
147
            function (string $prop) use ($array, $jsonDef, $writeAll) {
148 16
                $jsonType = $jsonDef[$prop] ?? null;
149
150 16
                if ( ! is_string($jsonType)) {
151 12
                    return $array[$prop];
152
                }
153
154 10
                return JsonTypeUtilities::DaftJsonFromJsonType($jsonType, $prop, $array[$prop], $writeAll);
155 18
            };
156
    }
157
158
    /**
159
    * Retrieve a property from data.
160
    *
161
    * @param string $property the property being retrieved
162
    *
163
    * @throws PropertyNotNullableException if value is not set and $property is not listed as nullabe
164
    *
165
    * @return mixed the property value
166
    */
167 90
    protected function RetrievePropertyValueFromData(string $property)
168
    {
169
        if (
170 90
            false === array_key_exists($property, $this->data) &&
171 90
            false === in_array($property, static::NULLABLE_PROPERTIES, true)
172
        ) {
173 4
            throw new PropertyNotNullableException(static::class, $property);
174
        } elseif (
175 86
            in_array($property, static::NULLABLE_PROPERTIES, true)
176
        ) {
177 66
            return $this->data[$property] ?? null;
178
        }
179
180 86
        return $this->data[$property];
181
    }
182
183
    /**
184
    * @param mixed $value
185
    */
186 268
    protected function NudgePropertyValue(string $property, $value) : void
187
    {
188 268
        $this->MaybeThrowForPropertyOnNudge($property);
189 266
        $this->MaybeThrowOnNudge($property, $value, static::NULLABLE_PROPERTIES);
190
191
        $isChanged = (
192 150
            false === array_key_exists($property, $this->data) ||
193 150
            $this->data[$property] !== $value
194
        );
195
196 150
        $this->data[$property] = $value;
197
198 150
        if ($isChanged && true !== isset($this->changedProperties[$property])) {
199 150
            $this->changedProperties[$property] = true;
200 150
            $this->wormProperties[$property] = true;
201
        }
202 150
    }
203
204
    /**
205
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
206
    */
207 268
    private function MaybeThrowForPropertyOnNudge(string $property) : void
208
    {
209 268
        if (true !== in_array($property, static::PROPERTIES, true)) {
210 2
            throw new UndefinedPropertyException(static::class, $property);
211 266
        } elseif ($this->DaftObjectWormPropertyWritten($property)) {
212 100
            throw new PropertyNotRewriteableException(static::class, $property);
213
        }
214 266
    }
215
216
    /**
217
    * @param mixed $value
218
    *
219
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
220
    */
221 266
    private function MaybeThrowOnNudge(string $property, $value, array $properties) : void
222
    {
223 266
        if (true === is_null($value) && true !== in_array($property, $properties, true)) {
224 116
            throw new PropertyNotNullableException(static::class, $property);
225
        }
226 150
    }
227
}
228