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 |
||
17 | class ItemList implements Countable, IteratorAggregate |
||
18 | { |
||
19 | /** |
||
20 | * List containing items |
||
21 | * @var Item[] |
||
22 | */ |
||
23 | private $list = []; |
||
24 | |||
25 | /** |
||
26 | * Has this list already been sorted? |
||
27 | * @var bool |
||
28 | */ |
||
29 | private $isSorted = false; |
||
30 | |||
31 | /** |
||
32 | * @param Item $item |
||
33 | */ |
||
34 | 39 | public function insert(Item $item) |
|
38 | |||
39 | /** |
||
40 | * @internal |
||
41 | * @return Item |
||
42 | */ |
||
43 | 36 | View Code Duplication | public function extract(): Item |
51 | |||
52 | /** |
||
53 | * @internal |
||
54 | * @return Item |
||
55 | */ |
||
56 | 33 | View Code Duplication | public function top(): Item |
65 | |||
66 | /** |
||
67 | * @return Traversable |
||
68 | */ |
||
69 | 27 | View Code Duplication | public function getIterator(): Traversable |
77 | |||
78 | /** |
||
79 | * Number of items in list |
||
80 | * @return int |
||
81 | */ |
||
82 | 37 | public function count(): int |
|
86 | |||
87 | /** |
||
88 | * @param Item $itemA |
||
89 | * @param Item $itemB |
||
90 | * |
||
91 | * @return int |
||
92 | */ |
||
93 | 35 | private function compare(Item $itemA, Item $itemB): int |
|
107 | |||
108 | } |
||
109 |
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.