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 — master ( 6cf9ce...3aaa4b )
by SignpostMarv
07:03
created

AbstractDaftObject   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 274
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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