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 ( 94cdbd...59ea50 )
by SignpostMarv
02:05
created

AbstractDaftObject::DoGetSet()   D

Complexity

Conditions 9
Paths 2

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 27
cts 27
cp 1
rs 4.909
c 0
b 0
f 0
cc 9
eloc 29
nc 2
nop 3
crap 9
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 ReflectionMethod;
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 string[]
22
    */
23
    const PROPERTIES = [];
24
25
    /**
26
    * List of nullable properties that can be defined on an implementation.
27
    *
28
    * @var string[]
29
    */
30
    const NULLABLE_PROPERTIES = [];
31
32
    /**
33
    * Does some sanity checking.
34
    *
35
    * @see DefinesOwnIdPropertiesInterface
36
    * @see self::CheckTypeDefinesOwnIdProperties()
37
    */
38 46
    public function __construct()
39
    {
40
        if (
41 46
            ($this instanceof DefinesOwnIdPropertiesInterface)
42
        ) {
43 33
            self::CheckTypeDefinesOwnIdProperties($this);
44
        }
45 44
    }
46
47
    /**
48
    * {@inheritdoc}
49
    */
50 29
    public function __get(string $property)
51
    {
52 29
        return $this->DoGetSet($property, true);
53
    }
54
55
    /**
56
    * {@inheritdoc}
57
    */
58 17
    public function __set(string $property, $v)
59
    {
60 17
        return $this->DoGetSet($property, false, $v);
61
    }
62
63
    /**
64
    * {@inheritdoc}
65
    *
66
    * @see static::NudgePropertyValue()
67
    */
68 8
    public function __unset(string $property) : void
69
    {
70 8
        $this->NudgePropertyValue($property, null);
71 6
    }
72
73
    /**
74
    * List of properties that can be defined on an implementation.
75
    *
76
    * @return string[]
77
    */
78 43
    final public static function DaftObjectProperties() : array
79
    {
80 43
        return static::PROPERTIES;
81
    }
82
83
    /**
84
    * {@inheritdoc}
85
    */
86 9
    final public static function DaftObjectNullableProperties() : array
87
    {
88 9
        return static::NULLABLE_PROPERTIES;
89
    }
90
91
    /**
92
    * Nudge the state of a given property, marking it as dirty.
93
    *
94
    * @param string $property property being nudged
95
    * @param mixed $value value to nudge property with
96
    *
97
    * @throws UndefinedPropertyException if $property is not in static::DaftObjectProperties()
98
    * @throws PropertyNotNullableException if $property is not in static::DaftObjectNullableProperties()
99
    * @throws PropertyNotRewriteableException if class is write-once read-many and $property was already changed
100
    */
101
    abstract protected function NudgePropertyValue(
102
        string $property,
103
        $value
104
    ) : void;
105
106
    /**
107
    * Checks if a type correctly defines it's own id.
108
    *
109
    * @param DaftObject $object
110
    *
111
    * @throws ClassDoesNotImplementClassException if $object is not an implementation of DefinesOwnIdPropertiesInterface
112
    * @throws ClassMethodReturnHasZeroArrayCountException if $object::DaftObjectIdProperties() does not contain at least one property
113
    * @throws ClassMethodReturnIsNotArrayOfStringsException if $object::DaftObjectIdProperties() is not string[]
114
    * @throws UndefinedPropertyException if an id property is not in $object::DaftObjectIdProperties()
115
    */
116 34
    final protected static function CheckTypeDefinesOwnIdProperties(
117
        DaftObject $object
118
    ) : void {
119 34
        if (false === ($object instanceof DefinesOwnIdPropertiesInterface)) {
120 1
            throw new ClassDoesNotImplementClassException(
121 1
                get_class($object),
122 1
                DefinesOwnIdPropertiesInterface::class
123
            );
124
        }
125
126
        /**
127
        * @var DefinesOwnIdPropertiesInterface $object
128
        */
129 33
        $object = $object;
130
131 33
        $properties = $object::DaftObjectIdProperties();
132
133 33
        if (count($properties) < 1) {
134 1
            throw new ClassMethodReturnHasZeroArrayCountException(
135 1
                get_class($object),
136 1
                'DaftObjectIdProperties'
137
            );
138
        }
139
140 32
        foreach ($properties as $property) {
141 32
            if (false === is_string($property)) {
142 1
                throw new ClassMethodReturnIsNotArrayOfStringsException(
143 1
                    get_class($object),
144 1
                    'DaftObjectIdProperties'
145
                );
146
            } elseif (
147 31
                false === in_array(
148 31
                    $property,
149 31
                    $object::DaftObjectProperties(),
150 31
                    true
151
                )
152
            ) {
153
                throw new UndefinedPropertyException(
154
                    get_class($object),
155
                    $property
156
                );
157
            }
158
        }
159 31
    }
160
161
    /**
162
    * @param mixed $v
163
    *
164
    * @return mixed
165
    */
166 40
    private function DoGetSet(
167
        string $property,
168
        bool $getNotSet,
169
        $v = null
170
    ) {
171 40
        static $scopes = [];
172 40
        $expectedMethod = ($getNotSet ? 'Get' : 'Set') . ucfirst($property);
173
174
        if (
175
            false === (
176 40
                'id' === $property &&
177 9
                is_a(
178 9
                    static::class,
179 9
                    DefinesOwnIdPropertiesInterface::class,
180 40
                    true
181
                )
182
            ) &&
183 40
            false === in_array($property, static::DaftObjectProperties(), true)
184
        ) {
185 3
            throw new UndefinedPropertyException(static::class, $property);
186 37
        } elseif (false === method_exists($this, $expectedMethod)) {
187 2
            if ($getNotSet) {
188 1
                throw new PropertyNotReadableException(
189 1
                    static::class,
190 1
                    $property
191
                );
192
            }
193 1
            throw new PropertyNotWriteableException(
194 1
                static::class,
195 1
                $property
196
            );
197 35
        } elseif (false === $this->CheckPublicScope($expectedMethod)) {
198 2
            if ($getNotSet) {
199 1
                throw new NotPublicGetterPropertyException(
200 1
                    static::class,
201 1
                    $property
202
                );
203
            }
204 1
            throw new NotPublicSetterPropertyException(
205 1
                static::class,
206 1
                $property
207
            );
208
        }
209
210 33
        return $this->$expectedMethod($v);
211
    }
212
213 35
    private function CheckPublicScope(string $expectedMethod) : bool
214
    {
215 35
        static $scopes = [];
216 35
        if (false === isset($scopes[$expectedMethod])) {
217 8
            $scopes[$expectedMethod] = (
218 8
                new ReflectionMethod(static::class, $expectedMethod)
219 8
            )->isPublic();
220
        }
221
222 35
        return $scopes[$expectedMethod];
223
    }
224
}
225