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 ( 4a3ff7...3f7dd9 )
by SignpostMarv
03:58
created

AbstractArrayBackedDaftObject   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Test Coverage

Coverage 98.7%

Importance

Changes 0
Metric Value
dl 0
loc 205
rs 9.52
c 0
b 0
f 0
ccs 76
cts 77
cp 0.987
wmc 36

14 Methods

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