Total Complexity | 54 |
Total Lines | 366 |
Duplicated Lines | 1.37 % |
Coverage | 99.32% |
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 | 188 | public function __construct(array $data = [], bool $writeAll = false) |
|
41 | { |
||
42 | 188 | parent::__construct(); |
|
43 | |||
44 | 186 | if (true === $writeAll) { |
|
45 | 61 | foreach ($data as $k => $v) { |
|
46 | 61 | $this->__set($k, $v); |
|
47 | } |
||
48 | } else { |
||
49 | 127 | foreach ($data as $k => $v) { |
|
50 | 66 | $this->data[$k] = $v; |
|
51 | } |
||
52 | } |
||
53 | 184 | } |
|
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | 31 | public function __isset(string $property) : bool |
|
59 | { |
||
60 | return |
||
61 | 31 | in_array($property, static::PROPERTIES, true) && |
|
62 | 31 | 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 | static::ThrowIfNotDaftJson(); |
|
104 | |||
105 | 6 | $out = []; |
|
106 | |||
107 | 6 | foreach (static::DaftObjectJsonPropertyNames() as $property) { |
|
108 | 6 | $val = $this->DoGetSet( |
|
109 | 6 | $property, |
|
110 | 6 | false, |
|
111 | 6 | NotPublicGetterPropertyException::class |
|
112 | ); |
||
113 | |||
114 | 6 | if (false === is_null($val)) { |
|
115 | 6 | $out[$property] = $val; |
|
116 | } |
||
117 | } |
||
118 | |||
119 | 6 | return $out; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | 28 | final public static function DaftObjectFromJsonArray( |
|
126 | array $array, |
||
127 | bool $writeAll = false |
||
128 | ) : DaftJson { |
||
129 | 28 | static::ThrowIfNotDaftJson(); |
|
130 | 12 | $array = static::ThrowIfJsonDefNotValid($array); |
|
131 | 9 | $in = []; |
|
132 | |||
133 | 9 | $jsonDef = static::DaftObjectJsonProperties(); |
|
134 | |||
135 | 9 | foreach (array_keys($array) as $prop) { |
|
136 | 8 | if (isset($jsonDef[$prop])) { |
|
137 | 5 | $jsonType = $jsonDef[$prop]; |
|
138 | |||
139 | 5 | if ('[]' === mb_substr($jsonType, -2)) { |
|
140 | 3 | $in[$prop] = static::DaftObjectFromJsonTypeArray( |
|
141 | 3 | mb_substr($jsonType, 0, -2), |
|
142 | 3 | $prop, |
|
143 | 3 | $array[$prop], |
|
144 | 3 | $writeAll |
|
145 | ); |
||
146 | } else { |
||
147 | 2 | $in[$prop] = static::DaftObjectFromJsonType( |
|
148 | 2 | $jsonType, |
|
149 | 2 | $array[$prop], |
|
150 | 2 | $writeAll |
|
151 | ); |
||
152 | } |
||
153 | } else { |
||
154 | 6 | $in[$prop] = $array[$prop]; |
|
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @var DaftJson $out |
||
160 | */ |
||
161 | 7 | $out = new static($in, $writeAll); |
|
162 | |||
163 | 7 | return $out; |
|
164 | } |
||
165 | |||
166 | 22 | public static function DaftObjectFromJsonString(string $string) : DaftJson |
|
167 | { |
||
168 | 22 | static::ThrowIfNotDaftJson(); |
|
169 | |||
170 | 6 | return static::DaftObjectFromJsonArray(json_decode($string, true)); |
|
171 | } |
||
172 | |||
173 | 75 | public function DaftObjectWormPropertyWritten(string $property) : bool |
|
174 | { |
||
175 | 75 | $wormProperties = $this->wormProperties; |
|
176 | |||
177 | return |
||
178 | 75 | ($this instanceof DaftObjectWorm) && |
|
179 | ( |
||
180 | 50 | $this->HasPropertyChanged($property) || |
|
181 | 75 | false === empty($wormProperties[$property]) |
|
182 | ); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param mixed $propVal |
||
187 | * |
||
188 | * @return DaftJson |
||
189 | */ |
||
190 | 2 | protected static function DaftObjectFromJsonType( |
|
191 | string $jsonType, |
||
192 | array $propVal, |
||
193 | bool $writeAll |
||
194 | ) { |
||
195 | 2 | static::ThrowIfNotJsonType($jsonType); |
|
196 | |||
197 | 2 | return static::ArrayToJsonType($jsonType, $propVal, $writeAll); |
|
198 | } |
||
199 | |||
200 | /** |
||
201 | * @return DaftJson[] |
||
202 | */ |
||
203 | 3 | protected static function DaftObjectFromJsonTypeArray( |
|
204 | string $jsonType, |
||
205 | string $prop, |
||
206 | array $propVal, |
||
207 | bool $writeAll |
||
208 | ) : array { |
||
209 | 3 | static::ThrowIfNotJsonType($jsonType); |
|
210 | |||
211 | 2 | $out = []; |
|
212 | |||
213 | 2 | foreach ($propVal as $val) { |
|
214 | 2 | if (false === is_array($val)) { |
|
215 | 1 | throw new PropertyNotJsonDecodableShouldBeArrayException( |
|
216 | 1 | $jsonType, |
|
217 | 1 | $prop |
|
218 | ); |
||
219 | } |
||
220 | 1 | $out[] = static::ArrayToJsonType( |
|
221 | 1 | $jsonType, |
|
222 | 1 | $val, |
|
223 | 1 | $writeAll |
|
224 | ); |
||
225 | } |
||
226 | |||
227 | 1 | return $out; |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * Retrieve a property from data. |
||
232 | * |
||
233 | * @param string $property the property being retrieved |
||
234 | * |
||
235 | * @throws PropertyNotNullableException if value is not set and $property is not listed as nullabe |
||
236 | * |
||
237 | * @return mixed the property value |
||
238 | */ |
||
239 | 45 | protected function RetrievePropertyValueFromData(string $property) |
|
240 | { |
||
241 | if ( |
||
242 | 45 | false === array_key_exists($property, $this->data) && |
|
243 | 6 | false === in_array($property, static::NULLABLE_PROPERTIES, true) |
|
244 | ) { |
||
245 | 2 | throw new PropertyNotNullableException(static::class, $property); |
|
246 | } elseif ( |
||
247 | 43 | in_array($property, static::NULLABLE_PROPERTIES, true) |
|
248 | ) { |
||
249 | 33 | return $this->data[$property] ?? null; |
|
250 | } |
||
251 | |||
252 | 43 | return $this->data[$property]; |
|
253 | } |
||
254 | |||
255 | /** |
||
256 | * {@inheritdoc} |
||
257 | */ |
||
258 | 134 | protected function NudgePropertyValue(string $property, $value) : void |
|
259 | { |
||
260 | 134 | $this->MaybeThrowOnNudge($property, $value); |
|
261 | |||
262 | $isChanged = ( |
||
263 | 75 | false === array_key_exists($property, $this->data) || |
|
264 | 75 | $this->data[$property] !== $value |
|
265 | ); |
||
266 | |||
267 | 75 | $this->data[$property] = $value; |
|
268 | |||
269 | 75 | if ($isChanged && true !== isset($this->changedProperties[$property])) { |
|
270 | 75 | $this->changedProperties[$property] = true; |
|
271 | 75 | $this->wormProperties[$property] = true; |
|
272 | } |
||
273 | 75 | } |
|
274 | |||
275 | /** |
||
276 | * @param mixed $value |
||
277 | * |
||
278 | * @see AbstractArrayBackedDaftObject::NudgePropertyValue() |
||
279 | */ |
||
280 | 134 | private function MaybeThrowOnNudge(string $property, $value) : void |
|
281 | { |
||
282 | 134 | if (true !== in_array($property, static::PROPERTIES, true)) { |
|
283 | 1 | throw new UndefinedPropertyException(static::class, $property); |
|
284 | } elseif ( |
||
285 | 133 | true === is_null($value) && |
|
286 | 88 | true !== in_array($property, static::NULLABLE_PROPERTIES, true) |
|
287 | ) { |
||
288 | 58 | throw new PropertyNotNullableException(static::class, $property); |
|
289 | 75 | } elseif ($this->DaftObjectWormPropertyWritten($property)) { |
|
290 | 50 | throw new PropertyNotRewriteableException( |
|
291 | 50 | static::class, |
|
292 | 50 | $property |
|
293 | ); |
||
294 | } |
||
295 | 75 | } |
|
296 | |||
297 | 12 | private static function ThrowIfJsonDefNotValid(array $array) : array |
|
298 | { |
||
299 | 12 | $jsonProps = []; |
|
300 | |||
301 | 12 | $jsonDef = static::DaftObjectJsonProperties(); |
|
302 | 12 | $nullableProps = static::DaftObjectNullableProperties(); |
|
303 | |||
304 | 12 | foreach (static::DaftObjectJsonPropertyNames() as $prop) { |
|
305 | if ( |
||
306 | 12 | false === array_key_exists($prop, $array) && |
|
307 | 2 | false === in_array($prop, $nullableProps, true) |
|
308 | ) { |
||
309 | throw new PropertyNotNullableException(static::class, $prop); |
||
310 | } |
||
311 | |||
312 | 12 | $jsonProps[] = $prop; |
|
313 | } |
||
314 | |||
315 | 12 | $out = []; |
|
316 | |||
317 | 12 | foreach ($array as $prop => $propVal) { |
|
318 | if ( |
||
319 | 12 | false === in_array($prop, $jsonProps, true) |
|
320 | ) { |
||
321 | 1 | throw new PropertyNotJsonDecodableException( |
|
322 | 1 | static::class, |
|
323 | 1 | $prop |
|
324 | ); |
||
325 | 12 | } elseif (false === is_null($propVal)) { |
|
326 | 11 | if (isset($jsonDef[$prop])) { |
|
327 | 8 | $jsonType = $jsonDef[$prop]; |
|
328 | |||
329 | 8 | if (false === is_array($propVal)) { |
|
330 | 2 | if ('[]' === mb_substr($jsonType, -2)) { |
|
331 | 1 | throw new PropertyNotJsonDecodableShouldBeArrayException( |
|
332 | 1 | static::class, |
|
333 | 1 | $prop |
|
334 | ); |
||
335 | } |
||
336 | 1 | throw new PropertyNotJsonDecodableShouldBeArrayException( |
|
337 | 1 | $jsonType, |
|
338 | 1 | $prop |
|
339 | ); |
||
340 | } |
||
341 | } |
||
342 | 9 | $out[$prop] = $propVal; |
|
343 | } |
||
344 | } |
||
345 | |||
346 | 9 | return $out; |
|
347 | } |
||
348 | |||
349 | 5 | private static function ThrowIfNotJsonType(string $jsonType) : void |
|
350 | { |
||
351 | 5 | if (false === is_a($jsonType, DaftJson::class, true)) { |
|
352 | 1 | throw new ClassDoesNotImplementClassException( |
|
353 | 1 | $jsonType, |
|
354 | 1 | DaftJson::class |
|
355 | ); |
||
356 | } |
||
357 | 4 | } |
|
358 | |||
359 | 3 | private static function ArrayToJsonType( |
|
360 | string $jsonType, |
||
361 | array $propVal, |
||
362 | bool $writeAll |
||
363 | ) : DaftJson { |
||
364 | /** |
||
365 | * @var DaftJson $jsonType |
||
366 | */ |
||
367 | 3 | $jsonType = $jsonType; |
|
368 | |||
369 | 3 | return $jsonType::DaftObjectFromJsonArray( |
|
370 | 3 | $propVal, |
|
371 | 3 | $writeAll |
|
372 | ); |
||
373 | } |
||
374 | |||
375 | 60 | View Code Duplication | private static function ThrowIfNotDaftJson() : void |
380 | ); |
||
381 | } |
||
382 | 12 | } |
|
383 | } |
||
384 |
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.