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 |
||
11 | class SortedIterator extends \IteratorIterator implements FiniteIterableInterface |
||
12 | { |
||
13 | use FiniteIterableTrait; |
||
14 | |||
15 | /** |
||
16 | * @param \Closure $func |
||
17 | * @param \Iterator $iterable |
||
18 | * @param bool $reverse |
||
19 | */ |
||
20 | 37 | public function __construct(\Closure $func, \Iterator $iterable, $reverse = false) |
|
21 | { |
||
22 | 37 | if ($reverse) { |
|
23 | View Code Duplication | $cmp = function ($a, $b) use ($func) { |
|
|
|||
24 | 2 | $orderA = $a['order']; |
|
25 | 2 | $orderB = $b['order']; |
|
26 | 2 | return $orderA == $orderB ? 0 : ($orderA < $orderB ? 1 : -1); |
|
27 | 2 | }; |
|
28 | } else { |
||
29 | 35 | View Code Duplication | $cmp = function ($a, $b) use ($func) { |
30 | 35 | $orderA = $a['order']; |
|
31 | 35 | $orderB = $b['order']; |
|
32 | 35 | return $orderA == $orderB ? 0 : ($orderA < $orderB ? -1 : 1); |
|
33 | 35 | }; |
|
34 | } |
||
35 | |||
36 | 37 | $data = []; |
|
37 | 37 | foreach ($iterable as $key => $value) { |
|
38 | 37 | $data [] = ['key' => $key, 'value' => $value, 'order' => call_user_func($func, $value, $key)]; |
|
39 | } |
||
40 | |||
41 | 37 | $this->mergeSort($data, $cmp); |
|
42 | |||
43 | 37 | parent::__construct(new \ArrayIterator($data)); |
|
44 | 37 | } |
|
45 | |||
46 | /** |
||
47 | * {@inheritDoc} |
||
48 | */ |
||
49 | 35 | public function key() |
|
50 | { |
||
51 | 35 | return $this->getInnerIterator()->current()['key']; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * {@inheritDoc} |
||
56 | */ |
||
57 | 35 | public function current() |
|
61 | |||
62 | /** |
||
63 | * As the manual says, "If two members compare as equal, their |
||
64 | * order in the sorted array is undefined." This means that the |
||
65 | * sort used is not "stable" and may change the order of elements |
||
66 | * that compare equal. |
||
67 | * |
||
68 | * Sometimes you really do need a stable sort. For example, if you |
||
69 | * sort a list by one field, then sort it again by another field, |
||
70 | * but don't want to lose the ordering from the previous field. |
||
71 | * In that case it is better to use usort with a comparison |
||
72 | * function that takes both fields into account, but if you can't |
||
73 | * do that then use the function below. It is a merge sort, which |
||
74 | * is guaranteed O(n*log(n)) complexity, which means it stays |
||
75 | * reasonably fast even when you use larger lists (unlike |
||
76 | * bubblesort and insertion sort, which are O(n^2)). |
||
77 | * |
||
78 | * http://www.php.net/manual/en/function.usort.php#38827 |
||
79 | * |
||
80 | * @param array $array |
||
81 | * @param \Closure $cmp_function |
||
82 | */ |
||
83 | 37 | protected function mergeSort(array &$array, \Closure $cmp_function) |
|
125 | } |
||
126 |
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.