Total Complexity | 57 |
Total Lines | 332 |
Duplicated Lines | 3.01 % |
Coverage | 86.43% |
Changes | 0 |
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:
Complex classes like AbstractArrayBackedDaftObject often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractArrayBackedDaftObject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | abstract class AbstractArrayBackedDaftObject extends AbstractDaftObject implements DaftObjectCreatedByArray |
||
15 | { |
||
16 | /** |
||
17 | * data for this instance. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | private $data = []; |
||
22 | |||
23 | /** |
||
24 | * List of changed properties. |
||
25 | * |
||
26 | * @var bool[] |
||
27 | */ |
||
28 | private $changedProperties = []; |
||
29 | |||
30 | /** |
||
31 | * List of changed properties, for write-once read-many. |
||
32 | * |
||
33 | * @var bool[] |
||
34 | */ |
||
35 | private $wormProperties = []; |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 189 | public function __construct(array $data = [], bool $writeAll = false) |
|
41 | { |
||
42 | 189 | parent::__construct(); |
|
43 | |||
44 | 187 | if (true === $writeAll) { |
|
45 | 60 | foreach ($data as $k => $v) { |
|
46 | 60 | $this->__set($k, $v); |
|
47 | } |
||
48 | } else { |
||
49 | 129 | foreach ($data as $k => $v) { |
|
50 | 68 | $this->data[$k] = $v; |
|
51 | } |
||
52 | } |
||
53 | 185 | } |
|
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | 33 | public function __isset(string $property) : bool |
|
59 | { |
||
60 | return |
||
61 | 33 | in_array($property, static::PROPERTIES, true) && |
|
62 | 33 | isset($this->data, $this->data[$property]); |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | 29 | public function ChangedProperties() : array |
|
69 | { |
||
70 | /** |
||
71 | * @var string[] $out |
||
72 | */ |
||
73 | 29 | $out = array_keys($this->changedProperties); |
|
74 | |||
75 | 29 | return $out; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | 33 | public function MakePropertiesUnchanged(string ...$properties) : void |
|
85 | } |
||
86 | 33 | } |
|
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | 82 | public function HasPropertyChanged(string $property) : bool |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | 22 | public function jsonSerialize() : array |
|
102 | { |
||
103 | 22 | if (false === ($this instanceof DaftJson)) { |
|
104 | 16 | throw new DaftObjectNotDaftJsonBadMethodCallException( |
|
105 | 16 | static::class |
|
106 | ); |
||
107 | } |
||
108 | |||
109 | 6 | $out = []; |
|
110 | |||
111 | 6 | foreach (static::DaftObjectJsonProperties() as $k => $v) { |
|
112 | 6 | $property = $v; |
|
113 | 6 | if (is_string($k)) { |
|
114 | 3 | $property = $k; |
|
115 | } |
||
116 | |||
117 | 6 | $val = $this->DoGetSet( |
|
118 | 6 | $property, |
|
119 | 6 | 'Get' . ucfirst($property), |
|
120 | 6 | PropertyNotReadableException::class, |
|
121 | 6 | NotPublicGetterPropertyException::class |
|
122 | ); |
||
123 | |||
124 | 6 | if (false === is_null($val)) { |
|
125 | 6 | $out[$property] = $val; |
|
126 | } |
||
127 | } |
||
128 | |||
129 | 6 | return $out; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | 22 | final public static function DaftObjectFromJsonArray( |
|
136 | array $array, |
||
137 | bool $writeAll = false |
||
138 | ) : DaftJson { |
||
139 | 22 | View Code Duplication | if (false === is_a(static::class, DaftJson::class, true)) { |
|
|||
140 | 16 | throw new DaftObjectNotDaftJsonBadMethodCallException( |
|
141 | 16 | static::class |
|
142 | ); |
||
143 | } |
||
144 | |||
145 | 6 | $in = []; |
|
146 | |||
147 | 6 | $props = static::DaftObjectJsonProperties(); |
|
148 | 6 | $jsonDef = static::DaftObjectJsonProperties(); |
|
149 | 6 | $nullableProps = static::DaftObjectNullableProperties(); |
|
150 | |||
151 | 6 | $jsonProps = []; |
|
152 | |||
153 | 6 | foreach ($jsonDef as $k => $v) { |
|
154 | 6 | if (is_string($k)) { |
|
155 | 3 | $jsonProps[] = $k; |
|
156 | } else { |
||
157 | 6 | $jsonProps[] = $v; |
|
158 | } |
||
159 | } |
||
160 | |||
161 | 6 | foreach ($jsonProps as $prop) { |
|
162 | if ( |
||
163 | ( |
||
164 | 6 | false === isset($array[$prop]) || |
|
165 | 6 | is_null($array[$prop]) |
|
166 | ) && |
||
167 | 2 | false === in_array($prop, $nullableProps, true) |
|
168 | ) { |
||
169 | throw new PropertyNotNullableException(static::class, $prop); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | 6 | foreach (array_keys($array) as $prop) { |
|
174 | if ( |
||
175 | 6 | false === in_array($prop, $props, true) && |
|
176 | 3 | false === isset($jsonDef[$prop]) |
|
177 | ) { |
||
178 | throw new UndefinedPropertyException(static::class, $prop); |
||
179 | } elseif ( |
||
180 | 6 | false === in_array($prop, $jsonProps, true) |
|
181 | ) { |
||
182 | throw new PropertyNotJsonDecodableException( |
||
183 | static::class, |
||
184 | $prop |
||
185 | ); |
||
186 | 6 | } elseif (is_null($array[$prop])) { |
|
187 | continue; |
||
188 | 6 | } elseif (isset($jsonDef[$prop])) { |
|
189 | 3 | $in[$prop] = static::DaftObjectFromJsonType( |
|
190 | 3 | $prop, |
|
191 | 3 | $jsonDef[$prop], |
|
192 | 3 | $array[$prop], |
|
193 | 3 | $writeAll |
|
194 | ); |
||
195 | } else { |
||
196 | 6 | $in[$prop] = $array[$prop]; |
|
197 | } |
||
198 | } |
||
199 | |||
200 | 6 | $out = new static($in, $writeAll); |
|
201 | |||
202 | 6 | if ( ! ($out instanceof DaftJson)) { // here to trick phpstan |
|
203 | exit; |
||
204 | } |
||
205 | |||
206 | 6 | return $out; |
|
207 | } |
||
208 | |||
209 | /** |
||
210 | * @param mixed $propVal |
||
211 | */ |
||
212 | 3 | protected static function DaftObjectFromJsonType( |
|
269 | ); |
||
270 | } |
||
271 | |||
272 | 22 | public static function DaftObjectFromJsonString(string $string) : DaftJson |
|
273 | { |
||
274 | 22 | View Code Duplication | if (false === is_a(static::class, DaftJson::class, true)) { |
275 | 16 | throw new DaftObjectNotDaftJsonBadMethodCallException( |
|
276 | 16 | static::class |
|
277 | ); |
||
278 | } |
||
279 | |||
280 | 6 | return static::DaftObjectFromJsonArray(json_decode($string, true)); |
|
281 | } |
||
282 | |||
283 | /** |
||
284 | * Retrieve a property from data. |
||
285 | * |
||
286 | * @param string $property the property being retrieved |
||
287 | * |
||
288 | * @throws PropertyNotNullableException if value is not set and $property is not listed as nullabe |
||
289 | * |
||
290 | * @return mixed the property value |
||
291 | */ |
||
292 | 44 | protected function RetrievePropertyValueFromData(string $property) |
|
293 | { |
||
294 | if ( |
||
295 | 44 | false === array_key_exists($property, $this->data) && |
|
296 | 5 | false === in_array($property, static::NULLABLE_PROPERTIES, true) |
|
297 | ) { |
||
298 | 1 | throw new PropertyNotNullableException(static::class, $property); |
|
299 | } elseif ( |
||
300 | 43 | in_array($property, static::NULLABLE_PROPERTIES, true) |
|
301 | ) { |
||
302 | 33 | return $this->data[$property] ?? null; |
|
303 | } |
||
304 | |||
305 | 43 | return $this->data[$property]; |
|
306 | } |
||
307 | |||
308 | /** |
||
309 | * {@inheritdoc} |
||
310 | */ |
||
311 | 134 | protected function NudgePropertyValue(string $property, $value) : void |
|
346 | } |
||
347 | 75 | } |
|
348 | } |
||
349 |
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.