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 ( 7de44a...428616 )
by SignpostMarv
02:00
created

AbstractDaftObject::DoGetSet()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 15
cts 15
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 18
nc 1
nop 5
crap 6
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(
53 29
            $property,
54 29
            'Get' . ucfirst($property),
55 29
            PropertyNotReadableException::class,
56 29
            NotPublicGetterPropertyException::class
57
        );
58
    }
59
60
    /**
61
    * {@inheritdoc}
62
    */
63 17
    public function __set(string $property, $v)
64
    {
65 17
        return $this->DoGetSet(
66 17
            $property,
67 17
            'Set' . ucfirst($property),
68 17
            PropertyNotWriteableException::class,
69 17
            NotPublicSetterPropertyException::class,
70 17
            $v
71
        );
72
    }
73
74
    /**
75
    * {@inheritdoc}
76
    *
77
    * @see static::NudgePropertyValue()
78
    */
79 8
    public function __unset(string $property) : void
80
    {
81 8
        $this->NudgePropertyValue($property, null);
82 6
    }
83
84
    /**
85
    * List of properties that can be defined on an implementation.
86
    *
87
    * @return string[]
88
    */
89 40
    final public static function DaftObjectProperties() : array
90
    {
91 40
        return static::PROPERTIES;
92
    }
93
94
    /**
95
    * {@inheritdoc}
96
    */
97 9
    final public static function DaftObjectNullableProperties() : array
98
    {
99 9
        return static::NULLABLE_PROPERTIES;
100
    }
101
102
    /**
103
    * Nudge the state of a given property, marking it as dirty.
104
    *
105
    * @param string $property property being nudged
106
    * @param mixed $value value to nudge property with
107
    *
108
    * @throws UndefinedPropertyException if $property is not in static::DaftObjectProperties()
109
    * @throws PropertyNotNullableException if $property is not in static::DaftObjectNullableProperties()
110
    * @throws PropertyNotRewriteableException if class is write-once read-many and $property was already changed
111
    */
112
    abstract protected function NudgePropertyValue(
113
        string $property,
114
        $value
115
    ) : void;
116
117
    /**
118
    * Checks if a type correctly defines it's own id.
119
    *
120
    * @param DaftObject $object
121
    *
122
    * @throws ClassDoesNotImplementClassException if $object is not an implementation of DefinesOwnIdPropertiesInterface
123
    * @throws ClassMethodReturnHasZeroArrayCountException if $object::DaftObjectIdProperties() does not contain at least one property
124
    * @throws ClassMethodReturnIsNotArrayOfStringsException if $object::DaftObjectIdProperties() is not string[]
125
    * @throws UndefinedPropertyException if an id property is not in $object::DaftObjectIdProperties()
126
    */
127 34
    final protected static function CheckTypeDefinesOwnIdProperties(
128
        DaftObject $object
129
    ) : void {
130 34
        if (false === ($object instanceof DefinesOwnIdPropertiesInterface)) {
131 1
            throw new ClassDoesNotImplementClassException(
132 1
                get_class($object),
133 1
                DefinesOwnIdPropertiesInterface::class
134
            );
135
        }
136
137
        /**
138
        * @var DefinesOwnIdPropertiesInterface $object
139
        */
140 33
        $object = $object;
141
142 33
        $properties = $object::DaftObjectIdProperties();
143
144 33
        if (count($properties) < 1) {
145 1
            throw new ClassMethodReturnHasZeroArrayCountException(
146 1
                get_class($object),
147 1
                'DaftObjectIdProperties'
148
            );
149
        }
150
151 32
        foreach ($properties as $property) {
152 32
            if (false === is_string($property)) {
153 1
                throw new ClassMethodReturnIsNotArrayOfStringsException(
154 1
                    get_class($object),
155 1
                    'DaftObjectIdProperties'
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
        string $expectedMethod,
169
        string $notExists,
170
        string $notPublic,
171
        $v = null
172
    ) {
173
        if (
174
            (
175 40
                'id' !== $property ||
176 9
                false === ($this instanceof DefinesOwnIdPropertiesInterface)
177
            ) &&
178 40
            false === in_array($property, static::DaftObjectProperties(), true)
179
        ) {
180 3
            throw new UndefinedPropertyException(static::class, $property);
181 37
        } elseif (false === method_exists($this, $expectedMethod)) {
182 2
            throw new $notExists(
183 2
                static::class,
184 2
                $property
185
            );
186
        } elseif (
187
            false === (
188 35
                new ReflectionMethod(static::class, $expectedMethod)
189 35
            )->isPublic()
190
        ) {
191 2
            throw new $notPublic(
192 2
                static::class,
193 2
                $property
194
            );
195
        }
196
197 33
        return $this->$expectedMethod($v);
198
    }
199
}
200