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 |
||
8 | class OptionGroup extends DataObject |
||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | private static $db = array( |
||
14 | 'Title' => 'Varchar(100)' |
||
15 | ); |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | private static $has_many = array( |
||
21 | 'Options' => 'OptionItem', |
||
22 | ); |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private static $singular_name = 'Product Option Group'; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private static $plural_name = 'Product Option Groups'; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private static $description = 'Groups of product options, e.g. size, color, etc'; |
||
38 | |||
39 | /** |
||
40 | * |
||
41 | */ |
||
42 | public function requireDefaultRecords() |
||
67 | |||
68 | /** |
||
69 | * @return RequiredFields |
||
70 | */ |
||
71 | 1 | public function getCMSValidator() |
|
75 | |||
76 | /** |
||
77 | * @return ValidationResult |
||
78 | */ |
||
79 | 12 | public function validate() |
|
94 | |||
95 | /** |
||
96 | * |
||
97 | */ |
||
98 | 2 | public function onBeforeDelete() |
|
113 | |||
114 | /** |
||
115 | * @param bool $member |
||
116 | * @return bool |
||
117 | */ |
||
118 | 1 | public function canView($member = false) |
|
122 | |||
123 | /** |
||
124 | * @param null $member |
||
125 | * @return bool|int |
||
126 | */ |
||
127 | 3 | public function canEdit($member = null) |
|
138 | |||
139 | /** |
||
140 | * @param null $member |
||
141 | * @return bool|int |
||
142 | */ |
||
143 | 2 | public function canDelete($member = null) |
|
147 | |||
148 | /** |
||
149 | * @param null $member |
||
150 | * @return bool|int |
||
151 | */ |
||
152 | 1 | public function canCreate($member = null) |
|
156 | } |
||
157 |
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.