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 |
||
24 | View Code Duplication | class Polygon implements ExtendableInterface, OptionsAwareInterface |
|
|
|||
25 | { |
||
26 | use OptionsAwareTrait; |
||
27 | use VariableAwareTrait; |
||
28 | |||
29 | /** |
||
30 | * @var Coordinate[] |
||
31 | */ |
||
32 | private $coordinates = []; |
||
33 | |||
34 | /** |
||
35 | * @param Coordinate[] $coordinates |
||
36 | * @param mixed[] $options |
||
37 | */ |
||
38 | 48 | public function __construct(array $coordinates = [], array $options = []) |
|
43 | |||
44 | /** |
||
45 | * @return bool |
||
46 | */ |
||
47 | 24 | public function hasCoordinates() |
|
51 | |||
52 | /** |
||
53 | * @return Coordinate[] |
||
54 | */ |
||
55 | 40 | public function getCoordinates() |
|
59 | |||
60 | /** |
||
61 | * @param Coordinate[] $coordinates |
||
62 | */ |
||
63 | 8 | public function setCoordinates(array $coordinates) |
|
68 | |||
69 | /** |
||
70 | * @param Coordinate[] $coordinates |
||
71 | */ |
||
72 | 48 | public function addCoordinates(array $coordinates) |
|
78 | |||
79 | /** |
||
80 | * @param Coordinate $coordinate |
||
81 | * |
||
82 | * @return bool |
||
83 | */ |
||
84 | 16 | public function hasCoordinate(Coordinate $coordinate) |
|
88 | |||
89 | /** |
||
90 | * @param Coordinate $coordinate |
||
91 | */ |
||
92 | 36 | public function addCoordinate(Coordinate $coordinate) |
|
96 | |||
97 | /** |
||
98 | * @param Coordinate $coordinate |
||
99 | */ |
||
100 | 4 | public function removeCoordinate(Coordinate $coordinate) |
|
105 | } |
||
106 |
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.