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 namespace BuildR\Collection\Collection; |
||
19 | abstract class AbstractCollection implements CollectionInterface { |
||
20 | |||
21 | /** |
||
22 | * @type array |
||
23 | */ |
||
24 | protected $data = []; |
||
25 | |||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | 11 | public function toArray() { |
|
32 | |||
33 | /** |
||
34 | * {@inheritdoc} |
||
35 | */ |
||
36 | 1 | public function clear() { |
|
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | 23 | public function contains($element) { |
|
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | 10 | View Code Duplication | public function containsAll($elements) { |
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | 6 | public function containsAny($elements) { |
|
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | 6 | public function equals(CollectionInterface $collection) { |
|
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | 1 | public function isEmpty() { |
|
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | 14 | public function size() { |
|
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | * |
||
113 | * @codeCoverageIgnore |
||
114 | */ |
||
115 | public function current() { |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | * |
||
122 | * @codeCoverageIgnore |
||
123 | */ |
||
124 | public function next() { |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | * |
||
131 | * @codeCoverageIgnore |
||
132 | */ |
||
133 | public function key() { |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | * |
||
140 | * @codeCoverageIgnore |
||
141 | */ |
||
142 | public function valid() { |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | * |
||
149 | * @codeCoverageIgnore |
||
150 | */ |
||
151 | public function rewind() { |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | 8 | public function count() { |
|
161 | |||
162 | /** |
||
163 | * @param array|\BuildR\Collection\Interfaces\CollectionInterface $elements |
||
164 | * |
||
165 | * @return array |
||
166 | */ |
||
167 | 21 | protected function collectionToArray($elements) { |
|
174 | |||
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.