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.
Passed
Push — object-only ( 708468...98fa5f )
by SignpostMarv
10:10
created

AbstractDaftObject::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 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
* Base daft object.
15
*/
16
abstract class AbstractDaftObject implements DaftObject
17
{
18
    /**
19
    * List of properties that can be defined on an implementation.
20
    *
21
    * @var array<int, string>
22
    */
23
    const PROPERTIES = [];
24
25
    /**
26
    * List of nullable properties that can be defined on an implementation.
27
    *
28
    * @var array<int, string>
29
    */
30
    const NULLABLE_PROPERTIES = [];
31
32
    /**
33
    * List of exportable properties that can be defined on an implementation.
34
    *
35
    * @var array<int, string>
36
    */
37
    const EXPORTABLE_PROPERTIES = [];
38
39
    /**
40
    * import/export definition for DaftJson.
41
    *
42
    * @var array<int, string>
43
    */
44
    const JSON_PROPERTIES = [];
45
46
    /**
47
    * List of sortable properties for DaftSortableObject.
48
    *
49
    * @var array<int, string>
50
    */
51
    const SORTABLE_PROPERTIES = [];
52
53
    /**
54
    * @var array<string, array<int, string>>
55
    */
56
    const CHANGE_OTHER_PROPERTIES = [];
57
58
    /**
59
    * @var array<string, array<int, string>>
60
    */
61
    const PROPERTIES_WITH_MULTI_TYPED_ARRAYS = [];
62
63 52
    public function __get(string $property)
64
    {
65 52
        return $this->DoGetSet($property, false);
66
    }
67
68 196
    public function __set(string $property, $v) : void
69
    {
70 196
        $this->DoGetSet($property, true, $v);
71 164
    }
72
73
    /**
74
    * @see static::NudgePropertyValue()
75
    */
76 24
    public function __unset(string $property) : void
77
    {
78 24
        $this->NudgePropertyValue($property, null);
79 20
    }
80
81 48
    public function __debugInfo() : array
82
    {
83 48
        $getters = static::DaftObjectPublicGetters();
84 48
        $exportables = static::DaftObjectExportableProperties();
85
86
        /**
87
        * @var array<int, string>
88
        */
89
        $properties = array_filter($exportables, function (string $prop) use ($getters) : bool {
90
            return
91 44
                $this->__isset($prop) &&
92 44
                in_array($prop, $getters, DefinitionAssistant::IN_ARRAY_STRICT_MODE);
93 48
        });
94
95
        /**
96
        * @var array<string, mixed>
97
        */
98 48
        $out = array_combine($properties, array_map(
99
            /**
100
            * @return mixed
101
            */
102
            function (string $prop) {
103 20
                return $this->__get($prop);
104 48
            },
105 24
            $properties
106
        ));
107
108 48
        return $out;
109
    }
110
111 604
    public static function DaftObjectProperties() : array
112
    {
113
        /**
114
        * @var array<int, string>
115
        */
116 604
        $out = static::PROPERTIES;
117
118 604
        return $out;
119
    }
120
121 336
    public static function DaftObjectNullableProperties() : array
122
    {
123
        /**
124
        * @var array<int, string>
125
        */
126 336
        $out = static::NULLABLE_PROPERTIES;
127
128 336
        return $out;
129
    }
130
131 120
    public static function DaftObjectExportableProperties() : array
132
    {
133
        /**
134
        * @var array<int, string>
135
        */
136 120
        $out = static::EXPORTABLE_PROPERTIES;
137
138 120
        return $out;
139
    }
140
141 84
    public static function DaftObjectPublicGetters() : array
142
    {
143 84
        return TypeUtilities::DaftObjectPublicGetters(static::class);
144
    }
145
146 4
    public static function DaftObjectPublicOrProtectedGetters() : array
147
    {
148 4
        return TypeUtilities::DaftObjectPublicOrProtectedGetters(static::class);
149
    }
150
151 196
    public static function DaftObjectPublicSetters() : array
152
    {
153 196
        return TypeUtilities::DaftObjectPublicSetters(static::class);
154
    }
155
156
    /**
157
    * @return array<int|string, string>
158
    */
159 48
    public static function DaftObjectJsonProperties() : array
160
    {
161
        /**
162
        * @var array<int|string, string>
163
        */
164 48
        $out = JsonTypeUtilities::ThrowIfNotDaftJson(static::class)::JSON_PROPERTIES;
165
166 12
        return $out;
167
    }
168
169
    /**
170
    * @return array<int, string>
171
    */
172 48
    final public static function DaftObjectJsonPropertyNames() : array
173
    {
174
        /**
175
        * @var array<int, string>
176
        */
177 48
        $out = [];
178
179
        /**
180
        * @var array<int|string, string>
181
        */
182 48
        $jsonProperties = static::DaftObjectJsonProperties();
183
184 12
        foreach ($jsonProperties as $k => $prop) {
185 12
            if (is_string($k)) {
186 8
                $prop = $k;
187
            }
188
189 12
            $out[] = $prop;
190
        }
191
192 12
        return $out;
193
    }
194
195
    /**
196
    * @return array<string, array<int, string>>
197
    */
198 64
    public static function DaftObjectPropertiesChangeOtherProperties() : array
199
    {
200
        /**
201
        * @var array<string, array<int, string>>
202
        */
203 64
        $out = static::CHANGE_OTHER_PROPERTIES;
204
205 64
        return $out;
206
    }
207
208
    /**
209
    * @return array<string, array<int, string>>
210
    */
211 96
    public static function DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues() : array
212
    {
213
        if (
214 96
            ! is_a(
215 96
                static::class,
216 96
                DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues::class,
217 96
                true
218
            )
219
        ) {
220 64
            throw new ClassDoesNotImplementClassException(
221 64
                static::class,
222 64
                DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues::class
223
            );
224
        }
225
226
        /**
227
        * @var array<string, array<int, string>>
228
        */
229 32
        $out = static::PROPERTIES_WITH_MULTI_TYPED_ARRAYS;
230
231 32
        return $out;
232
    }
233
234
    /**
235
    * Nudge the state of a given property, marking it as dirty.
236
    *
237
    * @param string $property property being nudged
238
    * @param scalar|array|object|null $value value to nudge property with
239
    *
240
    * @throws UndefinedPropertyException if $property is not in static::DaftObjectProperties()
241
    * @throws PropertyNotNullableException if $property is not in static::DaftObjectNullableProperties()
242
    * @throws PropertyNotRewriteableException if class is write-once read-many and $property was already changed
243
    */
244
    abstract protected function NudgePropertyValue(string $property, $value) : void;
245
246 224
    protected function MaybeThrowOnDoGetSet(string $property, bool $setter, array $props) : void
247
    {
248 224
        if ( ! in_array($property, $props, DefinitionAssistant::IN_ARRAY_STRICT_MODE)) {
249
            if (
250 24
                ! in_array(
251 18
                    $property,
252 24
                    static::DaftObjectProperties(),
253 24
                    DefinitionAssistant::IN_ARRAY_STRICT_MODE
254
                )
255
            ) {
256 16
                throw new UndefinedPropertyException(static::class, $property);
257 8
            } elseif ($setter) {
258 4
                throw new NotPublicSetterPropertyException(static::class, $property);
259
            }
260
261 4
            throw new NotPublicGetterPropertyException(static::class, $property);
262
        }
263 200
    }
264
265
    /**
266
    * @param mixed $v
267
    *
268
    * @return scalar|array|object|null
269
    */
270 220
    protected function DoGetSet(string $property, bool $setter, $v = null)
271
    {
272 220
        $props = $setter ? static::DaftObjectPublicSetters() : static::DaftObjectPublicGetters();
273
274 220
        $this->MaybeThrowOnDoGetSet($property, $setter, $props);
275
276
        /**
277
        * @var callable
278
        */
279 200
        $callable = [$this, TypeUtilities::MethodNameFromProperty($property, $setter)];
280 200
        $closure = Closure::fromCallable($callable);
281
282
        /**
283
        * @var scalar|array|object|null
284
        */
285 200
        $out = $closure->__invoke($v);
286
287 180
        return $out;
288
    }
289
}
290