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 |
||
14 | class IteratorScheme extends Common\IteratorScheme |
||
15 | { |
||
16 | public static function compatibleWith($phpVersion) |
||
20 | |||
21 | public function createOrderedMap(\Traversable $iterator = null) |
||
25 | |||
26 | public function createSet(\Traversable $iterator = null) |
||
30 | |||
31 | public function walk(\Traversable $iterator, callable $function) |
||
41 | |||
42 | public function toArray(\Traversable $iterator) |
||
54 | |||
55 | /** |
||
56 | * @param \Traversable $iterator |
||
57 | * |
||
58 | * @return IIterator |
||
59 | */ |
||
60 | View Code Duplication | public static function adapter(\Traversable $iterator) |
|
74 | |||
75 | View Code Duplication | public function arrayCompatibleIterator(\Traversable $iterator) |
|
|
|||
76 | { |
||
77 | $iterator = $this->adapter($iterator); |
||
78 | if ($iterator->isArrayCompatible()) { |
||
79 | return $iterator; |
||
80 | } |
||
81 | |||
82 | return new ArrayCompatibleIterator($this->adapter($iterator)); |
||
83 | } |
||
84 | |||
85 | public function adapterIterator(\Traversable $iterator) |
||
89 | |||
90 | public function arrayIterator(array $array) |
||
94 | |||
95 | public function emptyIterator() |
||
99 | |||
100 | public function filterIterator(\Traversable $iterator, callable $predicate) |
||
104 | |||
105 | public function projectionIterator( |
||
115 | |||
116 | public function reindexerIterator(\Traversable $iterator) |
||
120 | |||
121 | public function rangeIterator(\Traversable $iterator, $start, $amount) |
||
125 | |||
126 | public function groupedIterator( |
||
136 | |||
137 | public function orderedIterator(\Traversable $iterator, callable $function, $isAscending) |
||
141 | |||
142 | public function joinIterator(\Traversable $outerIterator, \Traversable $innerIterator) |
||
148 | |||
149 | public function groupJoinIterator( |
||
159 | |||
160 | protected function setOperationIterator(\Traversable $iterator, Common\SetOperations\ISetFilter $setFilter) |
||
164 | |||
165 | protected function flattenedIteratorsIterator(\Traversable $iteratorsIterator) |
||
169 | } |
||
170 |
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.