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 ( ce1e69...5665d5 )
by SignpostMarv
04:49
created

AbstractDaftObject   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 194
Duplicated Lines 10.31 %

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
dl 20
loc 194
ccs 48
cts 51
cp 0.9412
rs 10
c 0
b 0
f 0
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A DaftObjectProperties() 0 3 1
B CheckTypeDefinesOwnIdProperties() 0 53 7
A __set() 10 10 2
A __unset() 0 3 1
A __get() 8 8 2
A __construct() 0 8 3
A DaftObjectNullableProperties() 0 3 1

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 TypeError;
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
    * Index of checked types.
34
    *
35
    * @see self::CheckTypeDefinesOwnIdProperties()
36
    *
37
    * @var bool[]
38
    */
39
    private static $checkedTypes = [];
40
41
    /**
42
    * Does some sanity checking.
43
    *
44
    * @see DefinesOwnIdPropertiesInterface
45
    * @see self::CheckTypeDefinesOwnIdProperties()
46
    *
47
    * @throws TypeError if static::class was previously determined to be incorrectly implemented
48
    */
49 40
    public function __construct()
50
    {
51
        if (
52 40
            ($this instanceof DefinesOwnIdPropertiesInterface) &&
53 32
            false === self::CheckTypeDefinesOwnIdProperties($this)
54
        ) {
55 1
            throw new AlreadyIncorrectlyImplementedTypeError(
56 1
                get_class($this) // phpunit coverage does not pick up static::class here
57
            );
58
        }
59 37
    }
60
61
    /**
62
    * Maps param $property to the getter method.
63
    *
64
    * @param string $property the property being retrieved
65
    *
66
    * @throws UndefinedPropertyException if a property is undefined
67
    *
68
    * @return mixed
69
    */
70 26 View Code Duplication
    public function __get(string $property)
71
    {
72 26
        $expectedMethod = 'Get' . ucfirst($property);
73 26
        if (true !== method_exists($this, $expectedMethod)) {
74 1
            throw new UndefinedPropertyException(static::class, $property);
75
        }
76
77 25
        return $this->$expectedMethod();
78
    }
79
80
    /**
81
    * Maps param $property to the getter method.
82
    *
83
    * @param string $property the property being retrieved
84
    * @param mixed $v
85
    *
86
    * @throws UndefinedPropertyException if a property is undefined
87
    *
88
    * @return mixed
89
    */
90 13 View Code Duplication
    public function __set(string $property, $v)
91
    {
92 13
        $expectedMethod = 'Set' . ucfirst($property);
93
        if (
94 13
            true !== method_exists($this, $expectedMethod)
95
        ) {
96 1
            throw new PropertyNotWriteableException(static::class, $property);
97
        }
98
99 12
        return $this->$expectedMethod($v);
100
    }
101
102
    /**
103
    * required to support unset($foo->bar).
104
    *
105
    * @param string $property the property being unset
106
    *
107
    * @see static::NudgePropertyValue()
108
    */
109 8
    public function __unset(string $property) : void
110
    {
111 8
        $this->NudgePropertyValue($property, null);
112 6
    }
113
114
    /**
115
    * List of properties that can be defined on an implementation.
116
    *
117
    * @return string[]
118
    */
119 6
    final public static function DaftObjectProperties() : array
120
    {
121 6
        return static::PROPERTIES;
122
    }
123
124
    /**
125
    * List of nullable properties that can be defined on an implementation.
126
    *
127
    * @return string[]
128
    */
129 9
    final public static function DaftObjectNullableProperties() : array
130
    {
131 9
        return static::NULLABLE_PROPERTIES;
132
    }
133
134
    /**
135
    * Nudge the state of a given property, marking it as dirty.
136
    *
137
    * @param string $property property being nudged
138
    * @param mixed $value value to nudge property with
139
    *
140
    * @throws UndefinedPropertyException if $property is not in static::DaftObjectProperties()
141
    * @throws PropertyNotNullableException if $property is not in static::DaftObjectNullableProperties()
142
    */
143
    abstract protected function NudgePropertyValue(
144
        string $property,
145
        $value
146
    ) : void;
147
148
    /**
149
    * Checks if a type correctly defines it's own id.
150
    *
151
    * @param DaftObject $object
152
    *
153
    * @throws TypeError if $object::DaftObjectIdProperties() does not contain at least one property
154
    * @throws TypeError if $object::DaftObjectIdProperties() is not string[]
155
    * @throws UndefinedPropertyException if an id property is not in $object::DaftObjectIdProperties()
156
    */
157 33
    final protected static function CheckTypeDefinesOwnIdProperties(
158
        DaftObject $object
159
    ) : bool {
160 33
        static $checkedTypes = [];
161
162 33
        if (false === isset($checkedTypes[get_class($object)])) {
163 8
            $checkedTypes[get_class($object)] = false;
164
165 8
            if (false === ($object instanceof DefinesOwnIdPropertiesInterface)) {
166 1
                throw new ClassDoesNotImplementClassException(
167 1
                    get_class($object),
168 1
                    DefinesOwnIdPropertiesInterface::class
169
                );
170
            }
171
172
            /**
173
            * @var DefinesOwnIdPropertiesInterface $object
174
            */
175 7
            $object = $object;
176
177 7
            $properties = $object::DaftObjectIdProperties();
178
179 7
            if (count($properties) < 1) {
180 1
                throw new ClassMethodReturnHasZeroArrayCountException(
181 1
                    get_class($object),
182 1
                    'DaftObjectIdProperties'
183
                );
184
            }
185
186 6
            foreach ($properties as $property) {
187 6
                if (false === is_string($property)) {
188 1
                    throw new ClassMethodReturnIsNotArrayOfStringsException(
189 1
                        get_class($object),
190 1
                        'DaftObjectIdProperties'
191
                    );
192
                } elseif (
193 5
                    false === in_array(
194 5
                        $property,
195 5
                        $object::DaftObjectProperties(),
196 5
                        true
197
                    )
198
                ) {
199
                    throw new UndefinedPropertyException(
200
                        get_class($object),
201
                        $property
202
                    );
203
                }
204
            }
205
206 5
            $checkedTypes[get_class($object)] = true;
207
        }
208
209 30
        return $checkedTypes[get_class($object)];
210
    }
211
}
212