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 ( 3efc04...993d0f )
by SignpostMarv
03:38
created

AbstractDaftObject::DaftObjectPublicSetters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 ReflectionClass;
12
use ReflectionMethod;
13
14
/**
15
* Base daft object.
16
*/
17
abstract class AbstractDaftObject implements DaftObject
18
{
19
    /**
20
    * List of properties that can be defined on an implementation.
21
    *
22
    * @var array<int, string>
23
    */
24
    const PROPERTIES = [];
25
26
    /**
27
    * List of nullable properties that can be defined on an implementation.
28
    *
29
    * @var array<int, string>
30
    */
31
    const NULLABLE_PROPERTIES = [];
32
33
    /**
34
    * List of exportable properties that can be defined on an implementation.
35
    *
36
    * @var array<int, string>
37
    */
38
    const EXPORTABLE_PROPERTIES = [];
39
40
    /**
41
    * import/export definition for DaftJson.
42
    *
43
    * @var array<int, string>
44
    */
45
    const JSON_PROPERTIES = [];
46
47
    /**
48
    * @var array<string, array<int, string>>
49
    */
50
    private static $publicGetters = [];
51
52
    /**
53
    * @var array<string, array<int, string>>
54
    */
55
    private static $publicSetters = [];
56
57
    /**
58
    * Does some sanity checking.
59
    *
60
    * @see DefinesOwnIdPropertiesInterface
61
    * @see self::CheckTypeDefinesOwnIdProperties()
62
    */
63 376
    public function __construct()
64
    {
65 376
        self::CheckTypeDefinesOwnIdProperties(
66 376
            static::class,
67 376
            ($this instanceof DefinesOwnIdPropertiesInterface)
68
        );
69 372
    }
70
71 82
    public function __get(string $property)
72
    {
73 82
        return $this->DoGetSet($property, false);
74
    }
75
76 158
    public function __set(string $property, $v)
77
    {
78 158
        return $this->DoGetSet($property, true, $v);
79
    }
80
81
    /**
82
    * @see static::NudgePropertyValue()
83
    */
84 40
    public function __unset(string $property) : void
85
    {
86 40
        $this->NudgePropertyValue($property, null);
87 36
    }
88
89
    /**
90
    * @return array<string, mixed>
91
    */
92 62
    public function __debugInfo() : array
93
    {
94 62
        $out = [];
95 62
        $publicGetters = static::DaftObjectPublicGetters();
96 62
        foreach (static::DaftObjectExportableProperties() as $prop) {
97 60
            $expectedMethod = 'Get' . ucfirst($prop);
98
            if (
99 60
                $this->__isset($prop) &&
100 60
                in_array($prop, $publicGetters, true)
101
            ) {
102 60
                $out[(string) $prop] = $this->$expectedMethod();
103
            }
104
        }
105
106 62
        return $out;
107
    }
108
109
    /**
110
    * List of properties that can be defined on an implementation.
111
    *
112
    * @return array<int, string>
113
    */
114 232
    final public static function DaftObjectProperties() : array
115
    {
116 232
        return static::PROPERTIES;
117
    }
118
119 90
    final public static function DaftObjectNullableProperties() : array
120
    {
121 90
        return static::NULLABLE_PROPERTIES;
122
    }
123
124 120
    final public static function DaftObjectExportableProperties() : array
125
    {
126 120
        return static::EXPORTABLE_PROPERTIES;
127
    }
128
129 216
    final public static function DaftObjectPublicGetters() : array
130
    {
131 216
        static::CachePublicGettersAndSetters();
132
133 216
        return self::$publicGetters[static::class];
134
    }
135
136 158
    final public static function DaftObjectPublicSetters() : array
137
    {
138 158
        static::CachePublicGettersAndSetters();
139
140 158
        return self::$publicSetters[static::class];
141
    }
142
143 68
    final public static function DaftObjectJsonProperties() : array
144
    {
145 68
        static::ThrowIfNotDaftJson();
146
147 36
        return static::JSON_PROPERTIES;
148
    }
149
150 36
    final public static function DaftObjectJsonPropertyNames() : array
151
    {
152 36
        $out = [];
153
154 36
        foreach (static::DaftObjectJsonProperties() as $k => $prop) {
155 36
            if (is_string($k)) {
156 24
                $prop = $k;
157
            }
158
159 36
            $out[] = $prop;
160
        }
161
162 36
        return $out;
163
    }
164
165 164
    protected static function ThrowIfNotDaftJson() : void
166
    {
167 164
        if (false === is_a(static::class, DaftJson::class, true)) {
168 128
            throw new DaftObjectNotDaftJsonBadMethodCallException(
169 128
                static::class
170
            );
171
        }
172 36
    }
173
174 26
    final protected static function HasPublicMethod(
175
        ReflectionClass $classReflection,
176
        string $method
177
    ) : bool {
178
        if (
179 26
            $classReflection->hasMethod($method)
180
        ) {
181 26
            $methodReflection = new ReflectionMethod(static::class, $method);
182
183
            return
184 26
                $methodReflection->isPublic() &&
185 26
                false === $methodReflection->isStatic();
186
        }
187
188 8
        return false;
189
    }
190
191 216
    final protected static function CachePublicGettersAndSetters() : void
192
    {
193 216
        if (false === isset(self::$publicGetters[static::class])) {
194 26
            self::$publicGetters[static::class] = [];
195 26
            self::$publicSetters[static::class] = [];
196
197 26
            if (is_a(static::class, DefinesOwnIdPropertiesInterface::class, true)) {
198 14
                self::$publicGetters[static::class][] = 'id';
199
            }
200
201 26
            $classReflection = new ReflectionClass(static::class);
202
203 26
            foreach (static::DaftObjectProperties() as $property) {
204
                if (
205 26
                    static::HasPublicMethod(
206 26
                        $classReflection,
207 26
                        static::DaftObjectMethodNameFromProperty($property)
208
                    )
209
                ) {
210 22
                    self::$publicGetters[static::class][] = $property;
211
                }
212
213
                if (
214 26
                    static::HasPublicMethod(
215 26
                        $classReflection,
216 26
                        static::DaftObjectMethodNameFromProperty($property, true)
217
                    )
218
                ) {
219 26
                    self::$publicSetters[static::class][] = $property;
220
                }
221
            }
222
        }
223 216
    }
224
225
    /**
226
    * Nudge the state of a given property, marking it as dirty.
227
    *
228
    * @param string $property property being nudged
229
    * @param mixed $value value to nudge property with
230
    *
231
    * @throws UndefinedPropertyException if $property is not in static::DaftObjectProperties()
232
    * @throws PropertyNotNullableException if $property is not in static::DaftObjectNullableProperties()
233
    * @throws PropertyNotRewriteableException if class is write-once read-many and $property was already changed
234
    */
235
    abstract protected function NudgePropertyValue(string $property, $value) : void;
236
237
    /**
238
    * Checks if a type correctly defines it's own id.
239
    *
240
    * @throws ClassDoesNotImplementClassException if $class is not an implementation of DefinesOwnIdPropertiesInterface
241
    * @throws ClassMethodReturnHasZeroArrayCountException if $class::DaftObjectIdProperties() does not contain at least one property
242
    * @throws ClassMethodReturnIsNotArrayOfStringsException if $class::DaftObjectIdProperties() is not string[]
243
    * @throws UndefinedPropertyException if an id property is not in $class::DaftObjectIdProperties()
244
    */
245 376
    final protected static function CheckTypeDefinesOwnIdProperties(
246
        string $class,
247
        bool $throwIfNotImplementation = false
248
    ) : void {
249 376
        $interfaceCheck = $class;
250
251 376
        if (is_a($interfaceCheck, DefinesOwnIdPropertiesInterface::class, true)) {
252 232
            $properties = $class::DaftObjectIdProperties();
253
254 232
            if (count($properties) < 1) {
255 2
                throw new ClassMethodReturnHasZeroArrayCountException(
256 2
                    $class,
257 2
                    'DaftObjectIdProperties'
258
                );
259
            }
260
261 230
            foreach ($properties as $property) {
262 230
                if (false === is_string($property)) {
263 2
                    throw new ClassMethodReturnIsNotArrayOfStringsException(
264 2
                        $class,
265 230
                        'DaftObjectIdProperties'
266
                    );
267
                }
268
            }
269 150
        } elseif ($throwIfNotImplementation) {
270 2
            throw new ClassDoesNotImplementClassException(
271 2
                $class,
272 2
                DefinesOwnIdPropertiesInterface::class
273
            );
274
        }
275 372
    }
276
277 216
    protected static function DaftObjectMethodNameFromProperty(
278
        string $property,
279
        bool $SetNotGet = false
280
    ) : string {
281 216
        return ($SetNotGet ? 'Set' : 'Get') . ucfirst($property);
282
    }
283
284
    /**
285
    * @param mixed $v
286
    *
287
    * @return mixed
288
    */
289 216
    protected function DoGetSet(string $property, bool $SetNotGet, $v = null)
290
    {
291 216
        $expectedMethod = static::DaftObjectMethodNameFromProperty($property, $SetNotGet);
292 216
        $thingers = static::DaftObjectPublicGetters();
293
294 216
        if ($SetNotGet) {
295 158
            $thingers = static::DaftObjectPublicSetters();
296
        }
297
298 216
        if (false === in_array($property, $thingers, true)) {
299 10
            if (false === in_array($property, static::DaftObjectProperties(), true)) {
300 6
                throw new UndefinedPropertyException(static::class, $property);
301 4
            } elseif ($SetNotGet) {
302 2
                throw new NotPublicSetterPropertyException(static::class, $property);
303
            }
304
305 2
            throw new NotPublicGetterPropertyException(static::class, $property);
306
        }
307
308 206
        return $this->$expectedMethod($v);
309
    }
310
}
311