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 GeneratorScheme extends Common\IteratorScheme |
||
15 | { |
||
16 | public static function compatibleWith($phpVersion) |
||
17 | { |
||
18 | // HHVM does not support foreach by reference on iterators. |
||
19 | // This is used extensively by the generator classes, |
||
20 | // hence fallback to the standard iterator scheme. |
||
21 | return version_compare($phpVersion, '5.5.0', '>=') |
||
22 | && strpos($phpVersion, 'hhvm') === false |
||
23 | && strpos($phpVersion, 'hiphop') === false; |
||
24 | } |
||
25 | |||
26 | public function createOrderedMap(\Traversable $iterator = null) |
||
30 | |||
31 | public function createSet(\Traversable $iterator = null) |
||
35 | |||
36 | public function walk(\Traversable $iterator, callable $function) |
||
45 | |||
46 | public function toArray(\Traversable $iterator) |
||
57 | |||
58 | View Code Duplication | public function arrayCompatibleIterator(\Traversable $iterator) |
|
|
|||
59 | { |
||
60 | $iterator = $this->adapter($iterator); |
||
61 | if ($iterator->isArrayCompatible()) { |
||
62 | return $iterator; |
||
63 | } |
||
64 | |||
65 | return new ArrayCompatibleIterator($this->adapter($iterator)); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param \Traversable $iterator |
||
70 | * |
||
71 | * @return IGenerator |
||
72 | */ |
||
73 | View Code Duplication | public static function adapter(\Traversable $iterator) |
|
87 | |||
88 | protected function adapterIterator(\Traversable $iterator) |
||
92 | |||
93 | public function arrayIterator(array $array) |
||
97 | |||
98 | public function emptyIterator() |
||
102 | |||
103 | public function filterIterator(\Traversable $iterator, callable $predicate) |
||
107 | |||
108 | public function projectionIterator( |
||
118 | |||
119 | public function reindexerIterator(\Traversable $iterator) |
||
123 | |||
124 | public function joinIterator(\Traversable $outerIterator, \Traversable $innerIterator) |
||
130 | |||
131 | public function groupJoinIterator( |
||
141 | |||
142 | public function rangeIterator(\Traversable $iterator, $start, $amount) |
||
146 | |||
147 | public function groupedIterator( |
||
157 | |||
158 | public function orderedIterator(\Traversable $iterator, callable $function, $isAscending) |
||
165 | |||
166 | protected function setOperationIterator(\Traversable $iterator, Common\SetOperations\ISetFilter $setFilter) |
||
170 | |||
171 | protected function flattenedIteratorsIterator(\Traversable $iteratorsIterator) |
||
175 | } |
||
176 |
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.