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 BoxList implements IteratorAggregate |
||
18 | { |
||
19 | /** |
||
20 | * List containing boxes |
||
21 | * @var Box[] |
||
22 | */ |
||
23 | private $list = []; |
||
24 | |||
25 | /** |
||
26 | * Has this list already been sorted? |
||
27 | * @var bool |
||
28 | */ |
||
29 | private $isSorted = false; |
||
30 | |||
31 | /** |
||
32 | * @return Traversable |
||
33 | */ |
||
34 | 29 | View Code Duplication | public function getIterator(): Traversable |
42 | |||
43 | /** |
||
44 | * @param Box $item |
||
45 | */ |
||
46 | 28 | public function insert(Box $item) |
|
50 | |||
51 | /** |
||
52 | * @param Box $boxA |
||
53 | * @param Box $boxB |
||
54 | * |
||
55 | * @return int |
||
56 | */ |
||
57 | 10 | public function compare($boxA, $boxB): int |
|
64 | } |
||
65 |
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.