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 Circle implements ExtendableInterface, OptionsAwareInterface |
|
|
|||
25 | { |
||
26 | use OptionsAwareTrait; |
||
27 | use VariableAwareTrait; |
||
28 | |||
29 | /** |
||
30 | * @var Coordinate |
||
31 | */ |
||
32 | private $center; |
||
33 | |||
34 | /** |
||
35 | * @var float |
||
36 | */ |
||
37 | private $radius; |
||
38 | |||
39 | /** |
||
40 | * @param float $radius |
||
41 | * @param mixed[] $options |
||
42 | */ |
||
43 | 32 | public function __construct(Coordinate $center, $radius = 1.0, array $options = []) |
|
49 | |||
50 | /** |
||
51 | * @return Coordinate |
||
52 | */ |
||
53 | 20 | public function getCenter() |
|
57 | |||
58 | 32 | public function setCenter(Coordinate $center) |
|
62 | |||
63 | /** |
||
64 | * @return float |
||
65 | */ |
||
66 | 16 | public function getRadius() |
|
70 | |||
71 | /** |
||
72 | * @param float $radius |
||
73 | */ |
||
74 | 32 | public function setRadius($radius) |
|
78 | } |
||
79 |
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.