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 ( b3c539...42e9ee )
by SignpostMarv
05:30
created

MakePropertiesUnchanged()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
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 430
    public function __construct(array $data = [], bool $writeAll = false)
41
    {
42 430
        parent::__construct();
43
44 426
        if (true === $writeAll) {
45 148
            foreach ($data as $k => $v) {
46 98
                if ( ! is_string($k)) {
47 26
                    throw new InvalidArgumentException(DaftObjectCreatedByArray::ERR_KEY_NOT_STRING);
48
                }
49 122
                $this->__set($k, $v);
50
            }
51
        } else {
52 282
            foreach ($data as $k => $v) {
53 158
                if ( ! is_string($k)) {
54 26
                    throw new InvalidArgumentException(DaftObjectCreatedByArray::ERR_KEY_NOT_STRING);
55
                }
56 132
                $this->data[$k] = $v;
57
            }
58
        }
59 370
    }
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 202
    public function HasPropertyChanged(string $property) : bool
81
    {
82
        return
83 202
            isset($this->changedProperties[$property]) &&
84 202
            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 268
    public function DaftObjectWormPropertyWritten(string $property) : bool
128
    {
129 268
        $wormProperties = $this->wormProperties;
130
131
        return
132 268
            ($this instanceof DaftObjectWorm) &&
133
            (
134 136
                $this->HasPropertyChanged($property) ||
135 268
                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 92
    protected function RetrievePropertyValueFromData(string $property)
168
    {
169
        if (
170 92
            false === array_key_exists($property, $this->data) &&
171 92
            false === in_array($property, static::NULLABLE_PROPERTIES, true)
172
        ) {
173 4
            throw new PropertyNotNullableException(static::class, $property);
174
        } elseif (
175 88
            in_array($property, static::NULLABLE_PROPERTIES, true)
176
        ) {
177 66
            return $this->data[$property] ?? null;
178
        }
179
180 88
        return $this->data[$property];
181
    }
182
183
    /**
184
    * @param mixed $value
185
    */
186 270
    protected function NudgePropertyValue(string $property, $value) : void
187
    {
188 270
        $this->MaybeThrowForPropertyOnNudge($property);
189 268
        $this->MaybeThrowOnNudge($property, $value, static::NULLABLE_PROPERTIES);
190
191
        $isChanged = (
192 152
            false === array_key_exists($property, $this->data) ||
193 152
            $this->data[$property] !== $value
194
        );
195
196 152
        $this->data[$property] = $value;
197
198 152
        if ($isChanged && true !== isset($this->changedProperties[$property])) {
199 152
            $this->changedProperties[$property] = true;
200 152
            $this->wormProperties[$property] = true;
201
        }
202 152
    }
203
204
    /**
205
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
206
    */
207 270
    private function MaybeThrowForPropertyOnNudge(string $property) : void
208
    {
209 270
        if (true !== in_array($property, static::PROPERTIES, true)) {
210 2
            throw new UndefinedPropertyException(static::class, $property);
211 268
        } elseif ($this->DaftObjectWormPropertyWritten($property)) {
212 100
            throw new PropertyNotRewriteableException(static::class, $property);
213
        }
214 268
    }
215
216
    /**
217
    * @param mixed $value
218
    *
219
    * @see AbstractArrayBackedDaftObject::NudgePropertyValue()
220
    */
221 268
    private function MaybeThrowOnNudge(string $property, $value, array $properties) : void
222
    {
223 268
        if (true === is_null($value) && true !== in_array($property, $properties, true)) {
224 116
            throw new PropertyNotNullableException(static::class, $property);
225
        }
226 152
    }
227
}
228