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 |
||
6 | class BaseElement extends Widget |
||
|
|||
7 | { |
||
8 | |||
9 | private static $db = array( |
||
10 | 'ExtraClass' => 'Varchar(255)', |
||
11 | 'HideTitle' => 'Boolean' |
||
12 | ); |
||
13 | |||
14 | private static $has_one = array( |
||
15 | 'List' => 'ElementList' // optional. |
||
16 | ); |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private static $title = "Base Element"; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private static $summary_fields = array( |
||
27 | 'ID', |
||
28 | 'Title', |
||
29 | 'ElementType' |
||
30 | ); |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private static $description = "Base class for elements"; |
||
36 | |||
37 | /** |
||
38 | * @var boolean |
||
39 | */ |
||
40 | protected $enable_title_in_template = false; |
||
41 | |||
42 | |||
43 | public function getCMSFields() |
||
90 | |||
91 | /** |
||
92 | * Version viewer must only be added at if this is the final getCMSFields for a class. |
||
93 | * in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main |
||
94 | * To do this we test if getCMSFields is from the current class |
||
95 | */ |
||
96 | public function isEndofLine($className) |
||
106 | |||
107 | |||
108 | public function onBeforeWrite() |
||
122 | |||
123 | public function i18n_singular_name() |
||
127 | |||
128 | public function getElementType() |
||
132 | |||
133 | View Code Duplication | public function getTitle() |
|
145 | |||
146 | View Code Duplication | public function getCMSTitle() |
|
157 | |||
158 | public function canView($member = null) |
||
162 | |||
163 | public function canEdit($member = null) |
||
167 | |||
168 | public function canDelete($member = null) |
||
172 | |||
173 | public function canCreate($member = null) |
||
177 | |||
178 | public function ControllerTop() |
||
182 | |||
183 | public function getPage() |
||
193 | |||
194 | /** |
||
195 | * Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should |
||
196 | * render with widget inside the |
||
197 | * |
||
198 | * @return HTML |
||
199 | */ |
||
200 | public function forTemplate($holder = true) { |
||
203 | } |
||
204 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.