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
Branch object-only (2b4c07)
by SignpostMarv
14:54
created

AbstractDaftObject   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
wmc 24
eloc 68
dl 0
loc 272
ccs 74
cts 76
cp 0.9737
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __debugInfo() 0 28 2
A __set() 0 3 1
A __unset() 0 3 1
A __get() 0 3 1
A DaftObjectProperties() 0 8 1
A DaftObjectExportableProperties() 0 8 1
A DaftObjectPublicSetters() 0 3 1
A DaftObjectPublicGetters() 0 3 1
A DaftObjectNullableProperties() 0 8 1
A DoGetSet() 0 18 2
A DaftObjectJsonPropertyNames() 0 21 3
A DaftObjectPropertiesChangeOtherProperties() 0 8 1
A MaybeThrowOnDoGetSet() 0 16 4
A DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues() 0 21 2
A DaftObjectJsonProperties() 0 8 1
A DaftObjectPublicOrProtectedGetters() 0 3 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 48
    public function __get(string $property)
64
    {
65 48
        return $this->DoGetSet($property, false);
66
    }
67
68 188
    public function __set(string $property, $v) : void
69
    {
70 188
        $this->DoGetSet($property, true, $v);
71 160
    }
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 548
    public static function DaftObjectProperties() : array
112
    {
113
        /**
114
        * @var array<int, string>
115
        */
116 548
        $out = static::PROPERTIES;
117
118 548
        return $out;
119
    }
120
121 324
    public static function DaftObjectNullableProperties() : array
122
    {
123
        /**
124
        * @var array<int, string>
125
        */
126 324
        $out = static::NULLABLE_PROPERTIES;
127
128 324
        return $out;
129
    }
130
131 104
    public static function DaftObjectExportableProperties() : array
132
    {
133
        /**
134
        * @var array<int, string>
135
        */
136 104
        $out = static::EXPORTABLE_PROPERTIES;
137
138 104
        return $out;
139
    }
140
141 80
    public static function DaftObjectPublicGetters() : array
142
    {
143 80
        return TypeUtilities::DaftObjectPublicGetters(static::class);
144
    }
145
146
    public static function DaftObjectPublicOrProtectedGetters() : array
147
    {
148
        return TypeUtilities::DaftObjectPublicOrProtectedGetters(static::class);
149
    }
150
151 188
    public static function DaftObjectPublicSetters() : array
152
    {
153 188
        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 56
    public static function DaftObjectPropertiesChangeOtherProperties() : array
199
    {
200
        /**
201
        * @var array<string, array<int, string>>
202
        */
203 56
        $out = static::CHANGE_OTHER_PROPERTIES;
204
205 56
        return $out;
206
    }
207
208
    /**
209
    * @return array<string, array<int, string>>
210
    */
211 88
    public static function DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues() : array
212
    {
213
        if (
214 88
            ! is_a(
215 88
                static::class,
216 88
                DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues::class,
217 88
                true
218
            )
219
        ) {
220 56
            throw new ClassDoesNotImplementClassException(
221 56
                static::class,
222 56
                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 216
    protected function MaybeThrowOnDoGetSet(string $property, bool $setter, array $props) : void
247
    {
248 216
        if ( ! in_array($property, $props, DefinitionAssistant::IN_ARRAY_STRICT_MODE)) {
249
            if (
250 20
                ! in_array(
251 15
                    $property,
252 20
                    static::DaftObjectProperties(),
253 20
                    DefinitionAssistant::IN_ARRAY_STRICT_MODE
254
                )
255
            ) {
256 12
                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 196
    }
264
265
    /**
266
    * @param mixed $v
267
    *
268
    * @return scalar|array|object|null
269
    */
270 212
    protected function DoGetSet(string $property, bool $setter, $v = null)
271
    {
272 212
        $props = $setter ? static::DaftObjectPublicSetters() : static::DaftObjectPublicGetters();
273
274 212
        $this->MaybeThrowOnDoGetSet($property, $setter, $props);
275
276
        /**
277
        * @var callable
278
        */
279 196
        $callable = [$this, TypeUtilities::MethodNameFromProperty($property, $setter)];
280 196
        $closure = Closure::fromCallable($callable);
281
282
        /**
283
        * @var scalar|array|object|null
284
        */
285 196
        $out = $closure->__invoke($v);
286
287 176
        return $out;
288
    }
289
}
290