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 | View Code Duplication | abstract class ValueObjectCollection implements |
|
|
|||
11 | ArrayAccess, |
||
12 | Iterator, |
||
13 | Countable, |
||
14 | Arrayable |
||
15 | { |
||
16 | /** @var array */ |
||
17 | protected $collection; |
||
18 | |||
19 | /** @var int */ |
||
20 | protected $position = 0; |
||
21 | |||
22 | public function __construct(array $collection = []) |
||
26 | |||
27 | public function current() |
||
31 | |||
32 | public function offsetGet($offset) |
||
36 | |||
37 | public function offsetSet($offset, $value) |
||
45 | |||
46 | public function offsetExists($offset) |
||
50 | |||
51 | public function offsetUnset($offset) |
||
55 | |||
56 | public function next() |
||
60 | |||
61 | public function key() |
||
65 | |||
66 | public function valid() |
||
70 | |||
71 | public function rewind() |
||
75 | |||
76 | public function toArray(): array |
||
80 | |||
81 | public function count(): int |
||
85 | } |
||
86 |
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.