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 ( cdb780...be4cfb )
by SignpostMarv
02:43
created

AbstractDaftObject::__unset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 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
/**
12
* Base daft object.
13
*/
14
abstract class AbstractDaftObject implements DaftObject
15
{
16
    /**
17
    * List of properties that can be defined on an implementation.
18
    *
19
    * @var array<int, string>
20
    */
21
    const PROPERTIES = [];
22
23
    /**
24
    * List of nullable properties that can be defined on an implementation.
25
    *
26
    * @var array<int, string>
27
    */
28
    const NULLABLE_PROPERTIES = [];
29
30
    /**
31
    * List of exportable properties that can be defined on an implementation.
32
    *
33
    * @var array<int, string>
34
    */
35
    const EXPORTABLE_PROPERTIES = [];
36
37
    /**
38
    * import/export definition for DaftJson.
39
    *
40
    * @var array<int, string>
41
    */
42
    const JSON_PROPERTIES = [];
43
44
    /**
45
    * Does some sanity checking.
46
    *
47
    * @see DefinesOwnIdPropertiesInterface
48
    * @see TypeUtilities::CheckTypeDefinesOwnIdProperties()
49
    */
50 430
    public function __construct()
51
    {
52 430
        TypeUtilities::CheckTypeDefinesOwnIdProperties(
53 430
            static::class,
54 430
            ($this instanceof DefinesOwnIdPropertiesInterface)
55
        );
56 426
    }
57
58
    /**
59
    * @return mixed
60
    */
61 84
    public function __get(string $property)
62
    {
63 84
        return $this->DoGetSet($property, false);
64
    }
65
66
    /**
67
    * @param mixed $v
68
    */
69 160
    public function __set(string $property, $v) : void
70
    {
71 160
        $this->DoGetSet($property, true, $v);
72 152
    }
73
74
    /**
75
    * @see static::NudgePropertyValue()
76
    */
77 40
    public function __unset(string $property) : void
78
    {
79 40
        $this->NudgePropertyValue($property, null);
80 36
    }
81
82
    /**
83
    * @return array<string, mixed>
84
    */
85 62
    public function __debugInfo() : array
86
    {
87 62
        $getters = static::DaftObjectPublicGetters();
88 62
        $exportables = static::DaftObjectExportableProperties();
89
        /**
90
        * @var array<int, string> $properties
91
        */
92
        $properties = array_filter($exportables, function (string $prop) use ($getters) : bool {
93 60
            return $this->__isset($prop) && in_array($prop, $getters, true);
94 62
        });
95
96 62
        return array_combine($properties, array_map(
97
            /**
98
            * @return mixed
99
            */
100
            function (string $prop) {
101 48
                $expectedMethod = 'Get' . ucfirst($prop);
102
103 48
                return $this->$expectedMethod();
104 62
            },
105 62
            $properties
106
        ));
107
    }
108
109
    /**
110
    * List of properties that can be defined on an implementation.
111
    *
112
    * @return array<int, string>
113
    */
114 266
    final public static function DaftObjectProperties() : array
115
    {
116
        /**
117
        * @var array<int, string> $out
118
        */
119 266
        $out = static::PROPERTIES;
120
121 266
        return $out;
122
    }
123
124 92
    final public static function DaftObjectNullableProperties() : array
125
    {
126
        /**
127
        * @var array<int, string> $out
128
        */
129 92
        $out = static::NULLABLE_PROPERTIES;
130
131 92
        return $out;
132
    }
133
134 124
    final public static function DaftObjectExportableProperties() : array
135
    {
136
        /**
137
        * @var array<int, string> $out
138
        */
139 124
        $out = static::EXPORTABLE_PROPERTIES;
140
141 124
        return $out;
142
    }
143
144 110
    final public static function DaftObjectPublicGetters() : array
145
    {
146 110
        return TypeUtilities::DaftObjectPublicGetters(static::class);
147
    }
148
149 160
    final public static function DaftObjectPublicSetters() : array
150
    {
151 160
        return TypeUtilities::DaftObjectPublicSetters(static::class);
152
    }
153
154 68
    final public static function DaftObjectJsonProperties() : array
155
    {
156 68
        JsonTypeUtilities::ThrowIfNotDaftJson(static::class);
157
158
        /**
159
        * @var array<int|string, string> $out
160
        */
161 36
        $out = static::JSON_PROPERTIES;
162
163 36
        return $out;
164
    }
165
166 36
    final public static function DaftObjectJsonPropertyNames() : array
167
    {
168 36
        $out = [];
169
170
        /**
171
        * @var array<int|string, string> $jsonProperties
172
        */
173 36
        $jsonProperties = static::DaftObjectJsonProperties();
174
175 36
        foreach ($jsonProperties as $k => $prop) {
176 36
            if (is_string($k)) {
177 24
                $prop = $k;
178
            }
179
180 36
            $out[] = $prop;
181
        }
182
183 36
        return $out;
184
    }
185
186
    /**
187
    * Nudge the state of a given property, marking it as dirty.
188
    *
189
    * @param string $property property being nudged
190
    * @param scalar|null|array|object $value value to nudge property with
191
    *
192
    * @throws UndefinedPropertyException if $property is not in static::DaftObjectProperties()
193
    * @throws PropertyNotNullableException if $property is not in static::DaftObjectNullableProperties()
194
    * @throws PropertyNotRewriteableException if class is write-once read-many and $property was already changed
195
    */
196
    abstract protected function NudgePropertyValue(string $property, $value) : void;
197
198 218
    protected function MaybeThrowOnDoGetSet(string $property, bool $setter, array $props) : void
199
    {
200 218
        if (false === in_array($property, $props, true)) {
201 10
            if (false === in_array($property, static::DaftObjectProperties(), true)) {
202 6
                throw new UndefinedPropertyException(static::class, $property);
203 4
            } elseif ($setter) {
204 2
                throw new NotPublicSetterPropertyException(static::class, $property);
205
            }
206
207 2
            throw new NotPublicGetterPropertyException(static::class, $property);
208
        }
209 208
    }
210
211
    /**
212
    * @param mixed $v
213
    *
214
    * @return mixed
215
    */
216 218
    protected function DoGetSet(string $property, bool $setter, $v = null)
217
    {
218 218
        $props = $setter ? static::DaftObjectPublicSetters() : static::DaftObjectPublicGetters();
219
220 218
        $this->MaybeThrowOnDoGetSet($property, $setter, $props);
221
222 208
        return $this->{TypeUtilities::MethodNameFromProperty($property, $setter)}($v);
223
    }
224
}
225