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 |
||
18 | abstract class AbstractStorage implements StorageInterface |
||
19 | { |
||
20 | /** |
||
21 | * Array onde os dados serão armazenados |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $storage = array(); |
||
26 | |||
27 | /** |
||
28 | * Elemento atual em operação |
||
29 | * |
||
30 | * @var int |
||
31 | */ |
||
32 | protected $current = 0; |
||
33 | |||
34 | /** |
||
35 | * Define se o storage é apenas para leitura |
||
36 | * |
||
37 | * @var bool |
||
38 | */ |
||
39 | private $readOnly = false; |
||
40 | |||
41 | /** |
||
42 | * Define a capacidade máxima do storage |
||
43 | * |
||
44 | * @var int|null |
||
45 | */ |
||
46 | private $capacity = null; |
||
47 | |||
48 | /** |
||
49 | * Valida um offset |
||
50 | * |
||
51 | * @param $offset |
||
52 | * @throws InvalidOffsetTypeException |
||
53 | */ |
||
54 | 8 | private function validateOffsetType($offset) |
|
62 | |||
63 | /** |
||
64 | * @return boolean |
||
65 | */ |
||
66 | 8 | public function isReadOnly() |
|
70 | |||
71 | /** |
||
72 | * Fecha a coleção para apenas leitura |
||
73 | */ |
||
74 | 2 | public function lock() |
|
78 | |||
79 | /** |
||
80 | * @return int|null |
||
81 | */ |
||
82 | 8 | public function getCapacity() |
|
86 | |||
87 | /** |
||
88 | * @param int|null $capacity |
||
89 | * @throws InvalidCapacityException |
||
90 | */ |
||
91 | 1 | public function setCapacity($capacity) |
|
101 | |||
102 | /** |
||
103 | * Executa callback para cada elemento armazenado |
||
104 | * |
||
105 | * @param callable $callback |
||
106 | * @return void |
||
107 | */ |
||
108 | 3 | public function each(callable $callback) |
|
113 | |||
114 | /** |
||
115 | * Filtra elementos armazenados de acordo com callback |
||
116 | * |
||
117 | * @param callable $callback |
||
118 | * @return StorageInterface |
||
119 | */ |
||
120 | 2 | public function filter(callable $callback) |
|
132 | |||
133 | /** |
||
134 | * Copia o conteúdo do storage para dentro de outra array ou storage |
||
135 | * |
||
136 | * @param $array |
||
137 | * @param bool $override |
||
138 | */ |
||
139 | public function copyTo(&$array, $override = true) |
||
148 | |||
149 | /** |
||
150 | * Limpa o storage completamente |
||
151 | * |
||
152 | * @throws ReadOnlyStorageException |
||
153 | */ |
||
154 | 2 | public function clear() |
|
161 | |||
162 | /** |
||
163 | * Retorna o número de elementos armazenados |
||
164 | * |
||
165 | * @return int |
||
166 | */ |
||
167 | 8 | public function count() |
|
171 | |||
172 | /** |
||
173 | * Return the current element |
||
174 | * |
||
175 | * @link http://php.net/manual/en/iterator.current.php |
||
176 | * @return mixed Can return any type. |
||
177 | * @since 5.0.0 |
||
178 | */ |
||
179 | public function current() |
||
183 | |||
184 | /** |
||
185 | * Move forward to next element |
||
186 | * |
||
187 | * @link http://php.net/manual/en/iterator.next.php |
||
188 | * @return void Any returned value is ignored. |
||
189 | * @since 5.0.0 |
||
190 | */ |
||
191 | 4 | public function next() |
|
195 | |||
196 | /** |
||
197 | * Return the key of the current element |
||
198 | * |
||
199 | * @link http://php.net/manual/en/iterator.key.php |
||
200 | * @return mixed scalar on success, or null on failure. |
||
201 | * @since 5.0.0 |
||
202 | */ |
||
203 | 3 | public function key() |
|
207 | |||
208 | /** |
||
209 | * Checks if current position is valid |
||
210 | * |
||
211 | * @link http://php.net/manual/en/iterator.valid.php |
||
212 | * @return boolean The return value will be casted to boolean and then evaluated. |
||
213 | * Returns true on success or false on failure. |
||
214 | * @since 5.0.0 |
||
215 | */ |
||
216 | 3 | public function valid() |
|
220 | |||
221 | /** |
||
222 | * Rewind the Iterator to the first element |
||
223 | * |
||
224 | * @link http://php.net/manual/en/iterator.rewind.php |
||
225 | * @return void Any returned value is ignored. |
||
226 | * @since 5.0.0 |
||
227 | */ |
||
228 | 4 | public function rewind() |
|
232 | |||
233 | /** |
||
234 | * Whether a offset exists |
||
235 | * |
||
236 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
237 | * @param mixed $offset <p> |
||
238 | * An offset to check for. |
||
239 | * </p> |
||
240 | * @return boolean true on success or false on failure. |
||
241 | * </p> |
||
242 | * <p> |
||
243 | * The return value will be casted to boolean if non-boolean was returned. |
||
244 | * @since 5.0.0 |
||
245 | */ |
||
246 | 5 | public function offsetExists($offset) |
|
250 | |||
251 | /** |
||
252 | * Offset to retrieve |
||
253 | * |
||
254 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
255 | * @param mixed $offset <p> |
||
256 | * The offset to retrieve. |
||
257 | * </p> |
||
258 | * @return mixed Can return all value types. |
||
259 | * @throws InvalidOffsetException |
||
260 | * @since 5.0.0 |
||
261 | */ |
||
262 | 4 | public function offsetGet($offset) |
|
269 | |||
270 | /** |
||
271 | * Offset to set |
||
272 | * |
||
273 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
274 | * @param mixed $offset <p> |
||
275 | * The offset to assign the value to. |
||
276 | * </p> |
||
277 | * @param mixed $value <p> |
||
278 | * The value to set. |
||
279 | * </p> |
||
280 | * @throws InvalidOffsetTypeException |
||
281 | * @throws MaxCapacityReachedException |
||
282 | * @throws ReadOnlyStorageException |
||
283 | * @since 5.0.0 |
||
284 | */ |
||
285 | 8 | View Code Duplication | public function offsetSet($offset, $value) |
296 | |||
297 | /** |
||
298 | * Offset to unset |
||
299 | * |
||
300 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
301 | * @param mixed $offset <p> |
||
302 | * The offset to unset. |
||
303 | * </p> |
||
304 | * @throws InvalidOffsetException |
||
305 | * @throws ReadOnlyStorageException |
||
306 | * @since 5.0.0 |
||
307 | */ |
||
308 | 2 | View Code Duplication | public function offsetUnset($offset) |
319 | |||
320 | /** |
||
321 | * Converte o objeto para array |
||
322 | * |
||
323 | * @return array |
||
324 | */ |
||
325 | 1 | public function toArray() |
|
329 | } |
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.