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 ( 4a7e81...ca99c5 )
by SignpostMarv
01:57
created

AbstractDaftObject   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 239
Duplicated Lines 2.09 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 26
dl 5
loc 239
ccs 74
cts 74
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
B DoGetSet() 0 32 6
A DaftObjectProperties() 0 3 1
A DaftObjectExportableProperties() 0 3 1
B __debugInfo() 0 17 5
B CheckTypeDefinesOwnIdProperties() 0 30 5
A __set() 0 8 1
A __unset() 0 3 1
A __get() 0 7 1
A __construct() 0 6 2
A DaftObjectNullableProperties() 0 3 1
A DaftObjectJsonProperties() 5 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    * List of exportable properties that can be defined on an implementation.
34
    *
35
    * @var string[]
36
    */
37
    const EXPORTABLE_PROPERTIES = [];
38
39
    /**
40
    * import/export definition for DaftJson.
41
    */
42
    const JSON_PROPERTIES = [];
43
44
    /**
45
    * Does some sanity checking.
46
    *
47
    * @see DefinesOwnIdPropertiesInterface
48
    * @see self::CheckTypeDefinesOwnIdProperties()
49
    */
50 189
    public function __construct()
51
    {
52
        if (
53 189
            ($this instanceof DefinesOwnIdPropertiesInterface)
54
        ) {
55 117
            self::CheckTypeDefinesOwnIdProperties($this);
56
        }
57 187
    }
58
59
    /**
60
    * {@inheritdoc}
61
    */
62 41
    public function __get(string $property)
63
    {
64 41
        return $this->DoGetSet(
65 41
            $property,
66 41
            'Get' . ucfirst($property),
67 41
            PropertyNotReadableException::class,
68 41
            NotPublicGetterPropertyException::class
69
        );
70
    }
71
72
    /**
73
    * {@inheritdoc}
74
    */
75 80
    public function __set(string $property, $v)
76
    {
77 80
        return $this->DoGetSet(
78 80
            $property,
79 80
            'Set' . ucfirst($property),
80 80
            PropertyNotWriteableException::class,
81 80
            NotPublicSetterPropertyException::class,
82 80
            $v
83
        );
84
    }
85
86
    /**
87
    * {@inheritdoc}
88
    *
89
    * @see static::NudgePropertyValue()
90
    */
91 20
    public function __unset(string $property) : void
92
    {
93 20
        $this->NudgePropertyValue($property, null);
94 18
    }
95
96
    /**
97
    * {@inheritdoc}
98
    */
99 31
    public function __debugInfo() : array
100
    {
101 31
        $out = [];
102 31
        foreach (static::DaftObjectExportableProperties() as $prop) {
103 30
            $expectedMethod = 'Get' . ucfirst($prop);
104
            if (
105 30
                $this->__isset($prop) &&
106 30
                method_exists($this, $expectedMethod) &&
107
                (
108 24
                    new ReflectionMethod(static::class, $expectedMethod)
109 24
                )->isPublic()
110
            ) {
111 24
                $out[$prop] = $this->$expectedMethod();
112
            }
113
        }
114
115 31
        return $out;
116
    }
117
118
    /**
119
    * List of properties that can be defined on an implementation.
120
    *
121
    * @return string[]
122
    */
123 217
    final public static function DaftObjectProperties() : array
124
    {
125 217
        return static::PROPERTIES;
126
    }
127
128
    /**
129
    * {@inheritdoc}
130
    */
131 57
    final public static function DaftObjectNullableProperties() : array
132
    {
133 57
        return static::NULLABLE_PROPERTIES;
134
    }
135
136
    /**
137
    * {@inheritdoc}
138
    */
139 63
    final public static function DaftObjectExportableProperties() : array
140
    {
141 63
        return static::EXPORTABLE_PROPERTIES;
142
    }
143
144
    /**
145
    * {@inheritdoc}
146
    */
147 28
    final public static function DaftObjectJsonProperties() : array
148
    {
149 28 View Code Duplication
        if (false === is_a(static::class, DaftJson::class, true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150 16
            throw new DaftObjectNotDaftJsonBadMethodCallException(
151 16
                static::class
152
            );
153
        }
154
155 12
        return static::JSON_PROPERTIES;
156
    }
157
158
    /**
159
    * Nudge the state of a given property, marking it as dirty.
160
    *
161
    * @param string $property property being nudged
162
    * @param mixed $value value to nudge property with
163
    *
164
    * @throws UndefinedPropertyException if $property is not in static::DaftObjectProperties()
165
    * @throws PropertyNotNullableException if $property is not in static::DaftObjectNullableProperties()
166
    * @throws PropertyNotRewriteableException if class is write-once read-many and $property was already changed
167
    */
168
    abstract protected function NudgePropertyValue(
169
        string $property,
170
        $value
171
    ) : void;
172
173
    /**
174
    * Checks if a type correctly defines it's own id.
175
    *
176
    * @param DaftObject $object
177
    *
178
    * @throws ClassDoesNotImplementClassException if $object is not an implementation of DefinesOwnIdPropertiesInterface
179
    * @throws ClassMethodReturnHasZeroArrayCountException if $object::DaftObjectIdProperties() does not contain at least one property
180
    * @throws ClassMethodReturnIsNotArrayOfStringsException if $object::DaftObjectIdProperties() is not string[]
181
    * @throws UndefinedPropertyException if an id property is not in $object::DaftObjectIdProperties()
182
    */
183 118
    final protected static function CheckTypeDefinesOwnIdProperties(
184
        DaftObject $object
185
    ) : void {
186 118
        $class = get_class($object);
187 118
        if (false === ($object instanceof DefinesOwnIdPropertiesInterface)) {
188 1
            throw new ClassDoesNotImplementClassException(
189 1
                $class,
190 1
                DefinesOwnIdPropertiesInterface::class
191
            );
192
        }
193
194
        /**
195
        * @var DefinesOwnIdPropertiesInterface $object
196
        */
197 117
        $object = $object;
198
199 117
        $properties = $object::DaftObjectIdProperties();
200
201 117
        if (count($properties) < 1) {
202 1
            throw new ClassMethodReturnHasZeroArrayCountException(
203 1
                $class,
204 1
                'DaftObjectIdProperties'
205
            );
206
        }
207
208 116
        foreach ($properties as $property) {
209 116
            if (false === is_string($property)) {
210 1
                throw new ClassMethodReturnIsNotArrayOfStringsException(
211 1
                    $class,
212 1
                    'DaftObjectIdProperties'
213
                );
214
            }
215
        }
216 115
    }
217
218
    /**
219
    * @param mixed $v
220
    *
221
    * @return mixed
222
    */
223 109
    protected function DoGetSet(
224
        string $property,
225
        string $expectedMethod,
226
        string $notExists,
227
        string $notPublic,
228
        $v = null
229
    ) {
230
        if (
231
            (
232 109
                'id' !== $property ||
233 9
                false === ($this instanceof DefinesOwnIdPropertiesInterface)
234
            ) &&
235 109
            false === in_array($property, static::DaftObjectProperties(), true)
236
        ) {
237 3
            throw new UndefinedPropertyException(static::class, $property);
238 106
        } elseif (false === method_exists($this, $expectedMethod)) {
239 2
            throw new $notExists(
240 2
                static::class,
241 2
                $property
242
            );
243
        } elseif (
244
            false === (
245 104
                new ReflectionMethod(static::class, $expectedMethod)
246 104
            )->isPublic()
247
        ) {
248 2
            throw new $notPublic(
249 2
                static::class,
250 2
                $property
251
            );
252
        }
253
254 102
        return $this->$expectedMethod($v);
255
    }
256
}
257