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 |
||
10 | class PopoloCollection implements Countable, ArrayAccess, Iterator |
||
11 | { |
||
12 | protected $properties = [ |
||
13 | 'first', |
||
14 | ]; |
||
15 | public $lookupFromKey; |
||
16 | private $objectClass; |
||
17 | private $objectArr; |
||
18 | private $position; |
||
19 | |||
20 | /** |
||
21 | * Creates a new instance |
||
22 | */ |
||
23 | 279 | public function __construct($dataArr, $objectClass, $allPopolo) |
|
36 | |||
37 | /** |
||
38 | * String representation of {@link PopoloCollection} |
||
39 | * |
||
40 | * @return string |
||
41 | */ |
||
42 | public function __toString() |
||
46 | |||
47 | /** |
||
48 | * Getter for public attributes |
||
49 | * |
||
50 | * @param string $prop the attribute to get |
||
51 | * |
||
52 | * @return mixed |
||
53 | */ |
||
54 | 126 | View Code Duplication | public function __get($prop) |
62 | |||
63 | /** |
||
64 | * Get the first item in this collection |
||
65 | * |
||
66 | * @return mixed |
||
67 | */ |
||
68 | 126 | private function getFirst() |
|
72 | |||
73 | /** |
||
74 | * Gets an array of matching items, according to the |
||
75 | * array of filters provided. Filters are key-value pairs |
||
76 | * that are ANDed together, i.e. returned items match all |
||
77 | * the criteria |
||
78 | * |
||
79 | * @param string[] $filters key-value set of criteria |
||
80 | * |
||
81 | * @return object[] |
||
82 | */ |
||
83 | 15 | public function filter($filters) |
|
100 | |||
101 | /** |
||
102 | * Gets a single matching item, according to the |
||
103 | * array of filters provided. Filters are key-value pairs |
||
104 | * that are ANDed together, i.e. returned items match all |
||
105 | * the criteria. If more or less than one item matches, |
||
106 | * an exception is thrown |
||
107 | * |
||
108 | * @param string[] $filters key-value set of criteria |
||
109 | * |
||
110 | * @return object |
||
111 | */ |
||
112 | 9 | public function get($filters) |
|
125 | |||
126 | /** |
||
127 | * Count elements of an object |
||
128 | * |
||
129 | * Part of the Countable interface |
||
130 | * |
||
131 | * @return int |
||
132 | */ |
||
133 | 87 | public function count() |
|
137 | |||
138 | /** |
||
139 | * Checks if current position is valid |
||
140 | * |
||
141 | * Part of the ArrayAccess interface |
||
142 | * |
||
143 | * @param mixed $offset array offset |
||
144 | * |
||
145 | * @return boolean |
||
146 | */ |
||
147 | public function offsetExists($offset) |
||
151 | |||
152 | /** |
||
153 | * Offset to retrieve |
||
154 | * |
||
155 | * Part of the ArrayAccess interface |
||
156 | * |
||
157 | * @param mixed $offset array offset |
||
158 | * |
||
159 | * @return mixed |
||
160 | */ |
||
161 | 126 | public function offsetGet($offset) |
|
165 | |||
166 | /** |
||
167 | * Assign a value to the specified offset |
||
168 | * |
||
169 | * Part of the ArrayAccess interface |
||
170 | * |
||
171 | * @param mixed $offset array offset |
||
172 | * @param mixed $value value to assign |
||
173 | */ |
||
174 | public function offsetSet($offset, $value) |
||
178 | |||
179 | /** |
||
180 | * Unset an offset |
||
181 | * |
||
182 | * Part of the ArrayAccess interface |
||
183 | * |
||
184 | * @param mixed $offset array offset |
||
185 | */ |
||
186 | public function offsetUnset($offset) |
||
190 | |||
191 | /** |
||
192 | * Return the current element |
||
193 | * |
||
194 | * Part of the Iterator interface |
||
195 | * |
||
196 | * @return mixed |
||
197 | */ |
||
198 | 3 | public function current() |
|
202 | |||
203 | /** |
||
204 | * Return the key of the current element |
||
205 | * |
||
206 | * Part of the Iterator interface |
||
207 | * |
||
208 | * @return scalar |
||
209 | */ |
||
210 | public function key() |
||
214 | |||
215 | /** |
||
216 | * Move forward to next element |
||
217 | * |
||
218 | * Part of the Iterator interface |
||
219 | */ |
||
220 | 3 | public function next() |
|
224 | |||
225 | /** |
||
226 | * Rewind the Iterator to the first element |
||
227 | * |
||
228 | * Part of the Iterator interface |
||
229 | */ |
||
230 | 3 | public function rewind() |
|
234 | |||
235 | /** |
||
236 | * Checks if current position is valid |
||
237 | * |
||
238 | * Part of the Iterator interface |
||
239 | * |
||
240 | * @return boolean |
||
241 | */ |
||
242 | 3 | public function valid() |
|
246 | } |
||
247 |
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.