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 |
||
16 | View Code Duplication | class ConfiguratorOption extends Base |
|
|
|||
17 | { |
||
18 | /** |
||
19 | * @var int |
||
20 | */ |
||
21 | protected $id; |
||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | protected $groupId; |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $name; |
||
30 | /** |
||
31 | * @var int |
||
32 | */ |
||
33 | protected $position; |
||
34 | |||
35 | /** |
||
36 | * @return int |
||
37 | */ |
||
38 | public function getId() |
||
42 | |||
43 | /** |
||
44 | * @param int $id |
||
45 | * |
||
46 | * @return ConfiguratorOption |
||
47 | */ |
||
48 | public function setId($id) |
||
54 | |||
55 | /** |
||
56 | * @return int |
||
57 | */ |
||
58 | public function getGroupId() |
||
62 | |||
63 | /** |
||
64 | * @param int $groupId |
||
65 | * |
||
66 | * @return ConfiguratorOption |
||
67 | */ |
||
68 | public function setGroupId($groupId) |
||
74 | |||
75 | /** |
||
76 | * @return string |
||
77 | */ |
||
78 | public function getName() |
||
82 | |||
83 | /** |
||
84 | * @param string $name |
||
85 | * |
||
86 | * @return ConfiguratorOption |
||
87 | */ |
||
88 | public function setName($name) |
||
94 | |||
95 | /** |
||
96 | * @return int |
||
97 | */ |
||
98 | public function getPosition() |
||
102 | |||
103 | /** |
||
104 | * @param int $position |
||
105 | * |
||
106 | * @return ConfiguratorOption |
||
107 | */ |
||
108 | public function setPosition($position) |
||
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.