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 |
||
15 | class AlterableResultIterator implements Result, \ArrayAccess, \JsonSerializable |
||
16 | { |
||
17 | /** |
||
18 | * @var \Iterator|null |
||
19 | */ |
||
20 | private $resultIterator; |
||
21 | |||
22 | /** |
||
23 | * Key: the object to alter in the result set. |
||
24 | * Value: "add" => the object will be added to the resultset (if it is not found in it) |
||
25 | * "delete" => the object will be removed from the resultset (if found). |
||
26 | * |
||
27 | * @var \SplObjectStorage |
||
28 | */ |
||
29 | private $alterations; |
||
30 | |||
31 | /** |
||
32 | * The result array from the result set. |
||
33 | * |
||
34 | * @var array|null |
||
35 | */ |
||
36 | private $resultArray; |
||
37 | |||
38 | /** |
||
39 | * @param \Iterator|null $resultIterator |
||
40 | */ |
||
41 | public function __construct(\Iterator $resultIterator = null) |
||
46 | |||
47 | /** |
||
48 | * Sets a new iterator as the base iterator to be altered. |
||
49 | * |
||
50 | * @param \Iterator $resultIterator |
||
51 | */ |
||
52 | public function setResultIterator(\Iterator $resultIterator) |
||
57 | |||
58 | /** |
||
59 | * Returns the non altered result iterator (or null if none exist). |
||
60 | * |
||
61 | * @return \Iterator|null |
||
62 | */ |
||
63 | public function getUnderlyingResultIterator() |
||
67 | |||
68 | /** |
||
69 | * Adds an additional object to the result set (if not already available). |
||
70 | * |
||
71 | * @param $object |
||
72 | */ |
||
73 | View Code Duplication | public function add($object) |
|
84 | |||
85 | /** |
||
86 | * Removes an object from the result set. |
||
87 | * |
||
88 | * @param $object |
||
89 | */ |
||
90 | View Code Duplication | public function remove($object) |
|
101 | |||
102 | /** |
||
103 | * Casts the result set to a PHP array. |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | public function toArray() |
||
131 | |||
132 | /** |
||
133 | * Whether a offset exists. |
||
134 | * |
||
135 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
136 | * |
||
137 | * @param mixed $offset <p> |
||
138 | * An offset to check for. |
||
139 | * </p> |
||
140 | * |
||
141 | * @return bool true on success or false on failure. |
||
142 | * </p> |
||
143 | * <p> |
||
144 | * The return value will be casted to boolean if non-boolean was returned |
||
145 | * |
||
146 | * @since 5.0.0 |
||
147 | */ |
||
148 | public function offsetExists($offset) |
||
152 | |||
153 | /** |
||
154 | * Offset to retrieve. |
||
155 | * |
||
156 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
157 | * |
||
158 | * @param mixed $offset <p> |
||
159 | * The offset to retrieve. |
||
160 | * </p> |
||
161 | * |
||
162 | * @return mixed Can return all value types |
||
163 | * |
||
164 | * @since 5.0.0 |
||
165 | */ |
||
166 | public function offsetGet($offset) |
||
170 | |||
171 | /** |
||
172 | * Offset to set. |
||
173 | * |
||
174 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
175 | * |
||
176 | * @param mixed $offset <p> |
||
177 | * The offset to assign the value to. |
||
178 | * </p> |
||
179 | * @param mixed $value <p> |
||
180 | * The value to set. |
||
181 | * </p> |
||
182 | * |
||
183 | * @since 5.0.0 |
||
184 | */ |
||
185 | public function offsetSet($offset, $value) |
||
189 | |||
190 | /** |
||
191 | * Offset to unset. |
||
192 | * |
||
193 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
194 | * |
||
195 | * @param mixed $offset <p> |
||
196 | * The offset to unset. |
||
197 | * </p> |
||
198 | * |
||
199 | * @since 5.0.0 |
||
200 | */ |
||
201 | public function offsetUnset($offset) |
||
205 | |||
206 | /** |
||
207 | * @param int $offset |
||
208 | * |
||
209 | * @return \Porpaginas\Page |
||
210 | */ |
||
211 | public function take($offset, $limit) |
||
216 | |||
217 | /** |
||
218 | * Return the number of all results in the paginatable. |
||
219 | * |
||
220 | * @return int |
||
221 | */ |
||
222 | public function count() |
||
226 | |||
227 | /** |
||
228 | * Return an iterator over all results of the paginatable. |
||
229 | * |
||
230 | * @return Iterator |
||
231 | */ |
||
232 | public function getIterator() |
||
244 | |||
245 | /** |
||
246 | * Specify data which should be serialized to JSON. |
||
247 | * |
||
248 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
249 | * |
||
250 | * @return mixed data which can be serialized by <b>json_encode</b>, |
||
251 | * which is a value of any type other than a resource |
||
252 | * |
||
253 | * @since 5.4.0 |
||
254 | */ |
||
255 | public function jsonSerialize() |
||
259 | |||
260 | /** |
||
261 | * Returns only one value (the first) of the result set. |
||
262 | * Returns null if no value exists. |
||
263 | * |
||
264 | * @return mixed|null |
||
265 | */ |
||
266 | public function first() |
||
275 | |||
276 | /** |
||
277 | * Returns a new iterator mapping any call using the $callable function. |
||
278 | * |
||
279 | * @param callable $callable |
||
280 | * |
||
281 | * @return MapIterator |
||
282 | */ |
||
283 | public function map(callable $callable) |
||
287 | } |
||
288 |
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.