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 |
||
3 | class AccordionBlock extends Block |
||
4 | { |
||
5 | /** |
||
6 | * @var string |
||
7 | */ |
||
8 | private static $singular_name = 'Accordion Block'; |
||
9 | |||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | private static $plural_name = 'Accordion Blocks'; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private static $db = array( |
||
19 | 'Content' => 'HTMLText', |
||
20 | 'SortOrder' => 'Int', |
||
21 | ); |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private static $has_many = array( |
||
27 | 'Panels' => 'AccordionPanel', |
||
28 | ); |
||
29 | |||
30 | /** |
||
31 | * @return FieldList |
||
32 | */ |
||
33 | 1 | View Code Duplication | public function getCMSFields() |
58 | |||
59 | /** |
||
60 | * @return DataList |
||
61 | */ |
||
62 | 1 | public function getPanelList() |
|
69 | } |
||
70 | |||
102 | } |
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.