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 Collection 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Collection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Collection extends Property implements ComplexPropertyInterface, \IteratorAggregate |
||
14 | { |
||
15 | /** |
||
16 | * Collection elements |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | private $elements = []; |
||
21 | /** |
||
22 | * Cached value of "allowed" configuration option |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | private $allowed; |
||
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | * @return array |
||
31 | */ |
||
32 | 1 | public function getValue() |
|
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | * @throws \InvalidArgumentException |
||
40 | */ |
||
41 | 46 | public function setValue($value) |
|
66 | |||
67 | /** |
||
68 | * Normalize given value to make it compatible with property requirements |
||
69 | * |
||
70 | * @param mixed $value Given property value (passed by reference) |
||
71 | * @param int|string $key OPTIONAL Key for given value in a case if multiple values are given |
||
72 | * @return boolean TRUE if value can be accepted, FALSE otherwise |
||
73 | */ |
||
74 | 96 | protected function normalize(&$value, $key = null) |
|
91 | |||
92 | /** |
||
93 | * Invalid value setting handler |
||
94 | * |
||
95 | * @param mixed $value Invalid value given to property |
||
96 | * @param int|string $key OPTIONAL Key of this value |
||
97 | * @return void |
||
98 | */ |
||
99 | 25 | protected function onInvalidValue($value, $key = null) |
|
103 | |||
104 | /** |
||
105 | * Toggle given element in collection. |
||
106 | * Adds element in collection if it is missed, removes if it is available |
||
107 | * |
||
108 | * @param mixed $element The element to toggle. |
||
109 | * @return void |
||
110 | * @throws \RuntimeException |
||
111 | */ |
||
112 | 7 | public function toggle($element) |
|
139 | |||
140 | /** |
||
141 | * Removes the specified element from the collection, if it is found. |
||
142 | * |
||
143 | * @param mixed $element The element to remove. |
||
144 | * @return boolean TRUE if this collection contained the specified element, FALSE otherwise. |
||
145 | * @throws \RuntimeException |
||
146 | */ |
||
147 | 6 | public function removeElement($element) |
|
166 | |||
167 | /** |
||
168 | * Checks whether an element is contained in the collection. |
||
169 | * |
||
170 | * @param mixed $element The element to search for. |
||
171 | * @return boolean |
||
172 | */ |
||
173 | 4 | public function contains($element) |
|
180 | |||
181 | /** |
||
182 | * Gets the index/key of a given element. |
||
183 | * |
||
184 | * @param mixed $element The element to search for. |
||
185 | * @return int|string|boolean The key/index of the element or FALSE if the element was not found. |
||
186 | */ |
||
187 | 4 | public function indexOf($element) |
|
194 | |||
195 | /** |
||
196 | * Gets all keys/indices of the collection. |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | 1 | public function getKeys() |
|
204 | |||
205 | /** |
||
206 | * Gets all values of the collection. |
||
207 | * |
||
208 | * @return array |
||
209 | */ |
||
210 | 3 | public function getValues() |
|
214 | |||
215 | /** |
||
216 | * Checks whether the collection is empty. |
||
217 | * |
||
218 | * @return boolean |
||
219 | */ |
||
220 | 2 | public function isEmpty() |
|
224 | |||
225 | /** |
||
226 | * Clears the collection, removing all elements. |
||
227 | * |
||
228 | * @return void |
||
229 | * @throws \RuntimeException |
||
230 | */ |
||
231 | 4 | public function clear() |
|
236 | |||
237 | /** |
||
238 | * {@inheritdoc} |
||
239 | */ |
||
240 | 20 | public function count() |
|
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | 2 | public function getIterator() |
|
252 | |||
253 | /** |
||
254 | * {@inheritDoc} |
||
255 | */ |
||
256 | 4 | public function offsetExists($offset) |
|
260 | |||
261 | /** |
||
262 | * Checks whether the collection contains an element with the specified key/index. |
||
263 | * |
||
264 | * @param string|integer $key The key/index to check for. |
||
265 | * @return boolean |
||
266 | */ |
||
267 | 8 | public function containsKey($key) |
|
271 | |||
272 | /** |
||
273 | * {@inheritDoc} |
||
274 | */ |
||
275 | 2 | public function offsetGet($offset) |
|
279 | |||
280 | /** |
||
281 | * Gets the element at the specified key/index. |
||
282 | * |
||
283 | * @param string|integer $key The key/index of the element to retrieve. |
||
284 | * @return mixed |
||
285 | */ |
||
286 | 3 | public function get($key) |
|
293 | |||
294 | /** |
||
295 | * {@inheritDoc} |
||
296 | * @throws \RuntimeException |
||
297 | */ |
||
298 | 9 | public function offsetSet($offset, $value) |
|
306 | |||
307 | /** |
||
308 | * Sets an element in the collection at the specified key/index. |
||
309 | * |
||
310 | * @param string|integer $key The key/index of the element to set. |
||
311 | * @param mixed $element The element to set. |
||
312 | * @return void |
||
313 | * @throws \RuntimeException |
||
314 | */ |
||
315 | 8 | public function set($key, $element) |
|
324 | |||
325 | /** |
||
326 | * Adds an element at the end of the collection. |
||
327 | * |
||
328 | * @param mixed $element The element to add. |
||
329 | * @return void |
||
330 | * @throws \RuntimeException |
||
331 | */ |
||
332 | 26 | public function add($element) |
|
341 | |||
342 | /** |
||
343 | * {@inheritDoc} |
||
344 | * @throws \RuntimeException |
||
345 | */ |
||
346 | 6 | public function offsetUnset($offset) |
|
350 | |||
351 | /** |
||
352 | * Removes the element at the specified index from the collection. |
||
353 | * |
||
354 | * @param string|integer $key The kex/index of the element to remove. |
||
355 | * @return mixed The removed element or NULL, if the collection did not contain the element. |
||
356 | * @throws \RuntimeException |
||
357 | */ |
||
358 | 12 | public function remove($key) |
|
368 | |||
369 | /** |
||
370 | * {@inheritdoc} |
||
371 | */ |
||
372 | 22 | public function toArray() |
|
376 | |||
377 | /** |
||
378 | * {@inheritdoc} |
||
379 | * @throws \InvalidArgumentException |
||
380 | */ |
||
381 | 108 | public function validateConfig($name, &$value) |
|
382 | { |
||
383 | switch ($name) { |
||
384 | 108 | View Code Duplication | case 'default': |
385 | 33 | if (!is_array($value)) { |
|
386 | 7 | if (is_object($value) && method_exists($value, 'toArray')) { |
|
387 | 1 | $value = $value->toArray(); |
|
388 | 1 | } else { |
|
389 | 6 | throw new \InvalidArgumentException('Only arrays are accepted as default values for collection properties'); |
|
390 | } |
||
391 | 1 | } |
|
392 | 27 | break; |
|
393 | 97 | case 'allowed': |
|
394 | 65 | $valid = false; |
|
395 | 65 | if (($value === null) || is_callable($value)) { |
|
396 | // Explicitly defined validator or empty validator |
||
397 | 4 | $valid = true; |
|
398 | 65 | } elseif (is_array($value) && (count($value) === 1) && isset($value[0]) && is_string($value[0])) { |
|
399 | // This is probably validator defined through annotation's "allowed" parameter |
||
400 | 3 | $v = $value[0]; |
|
401 | 3 | if (class_exists($v)) { |
|
402 | // Class name for validation |
||
403 | 1 | $value = $v; |
|
404 | 1 | $valid = true; |
|
405 | 3 | View Code Duplication | } elseif (method_exists($this, $v)) { |
406 | // Name of validation method |
||
407 | $value = [$this, $v]; |
||
408 | $valid = true; |
||
409 | } else { |
||
410 | // Explicitly given list of valid values |
||
411 | 2 | $valid = true; |
|
412 | } |
||
413 | 61 | } elseif (is_string($value)) { |
|
414 | 5 | if (class_exists($value)) { |
|
415 | // Explicitly given class name for validation |
||
416 | 3 | $valid = true; |
|
417 | 5 | View Code Duplication | } elseif (method_exists($this, $value)) { |
418 | // Explicitly given name of validation method |
||
419 | 1 | $value = [$this, $value]; |
|
420 | 1 | $valid = true; |
|
421 | 1 | } |
|
422 | 58 | } /** @noinspection NotOptimalIfConditionsInspection */ elseif (is_array($value)) { |
|
423 | // Explicitly given list of valid values |
||
424 | 49 | $valid = true; |
|
425 | 49 | } |
|
426 | 65 | if (!$valid) { |
|
427 | 5 | throw new \InvalidArgumentException('Unable to recognize given validator for collection'); |
|
428 | } |
||
429 | 60 | break; |
|
430 | 73 | default: |
|
431 | 73 | return parent::validateConfig($name, $value); |
|
432 | 73 | } |
|
433 | 67 | return true; |
|
434 | } |
||
435 | |||
436 | /** |
||
437 | * {@inheritdoc} |
||
438 | */ |
||
439 | 95 | public function reset() |
|
440 | { |
||
441 | // No change notification should be made for reset, |
||
442 | // property value should be set to its default |
||
443 | 95 | $flag = $this->skipNotify; |
|
444 | 95 | $this->skipNotify = true; |
|
445 | /** @var array $default */ |
||
446 | 95 | $default = (array)$this->getConfig('default'); |
|
447 | 95 | foreach ($default as $k => &$v) { |
|
448 | 25 | if (!$this->normalize($v, $k)) { |
|
449 | throw new Exception('Default value for property class ' . get_class($this) . ' is not acceptable for property validation rules'); |
||
450 | } |
||
451 | 95 | } |
|
452 | 95 | unset($v); |
|
453 | 95 | $this->elements = $default; |
|
454 | 95 | $this->skipNotify = $flag; |
|
455 | 95 | } |
|
456 | |||
457 | /** |
||
458 | * {@inheritdoc} |
||
459 | */ |
||
460 | 1 | protected function initConfig() |
|
469 | |||
470 | /** |
||
471 | * {@inheritdoc} |
||
472 | */ |
||
473 | 67 | protected function onConfigChange($name, $value) |
|
485 | } |
||
486 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.