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 | class SpecValueObject |
|
|
|||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | private $name; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $role; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $backgroundImage; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $icon; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $description; |
||
36 | |||
37 | /** |
||
38 | * @var int |
||
39 | */ |
||
40 | private $order; |
||
41 | |||
42 | /** |
||
43 | * SpecValueObject constructor. |
||
44 | * @param string $name |
||
45 | * @param string $role |
||
46 | * @param string $backgroundImage |
||
47 | * @param string $icon |
||
48 | * @param string $description |
||
49 | * @param int $order |
||
50 | */ |
||
51 | public function __construct( |
||
66 | |||
67 | /** |
||
68 | * @return string |
||
69 | */ |
||
70 | public function getName() |
||
74 | |||
75 | /** |
||
76 | * @return string |
||
77 | */ |
||
78 | public function getRole() |
||
82 | |||
83 | /** |
||
84 | * @return string |
||
85 | */ |
||
86 | public function getBackgroundImage() |
||
90 | |||
91 | /** |
||
92 | * @return string |
||
93 | */ |
||
94 | public function getIcon() |
||
98 | |||
99 | /** |
||
100 | * @return string |
||
101 | */ |
||
102 | public function getDescription() |
||
106 | |||
107 | /** |
||
108 | * @return int |
||
109 | */ |
||
110 | public function getOrder() |
||
114 | } |
||
115 |
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.