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 | * @var array $db |
||
| 10 | */ |
||
| 11 | private static $db = array( |
||
| 12 | 'ExtraClass' => 'Varchar(255)', |
||
| 13 | 'HideTitle' => 'Boolean' |
||
| 14 | ); |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var array $has_one |
||
| 18 | */ |
||
| 19 | private static $has_one = array( |
||
| 20 | 'List' => 'ElementList' // optional. |
||
| 21 | ); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array $has_many |
||
| 25 | */ |
||
| 26 | private static $has_many = array( |
||
| 27 | 'VirtualClones' => 'ElementVirtualLinked' |
||
| 28 | ); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private static $title = "Content Block"; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private static $singular_name = 'Content Block'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private static $summary_fields = array( |
||
| 44 | 'ID' => 'ID', |
||
| 45 | 'Title' => 'Title', |
||
| 46 | 'ElementType' => 'Type' |
||
| 47 | ); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var boolean |
||
| 51 | */ |
||
| 52 | protected $enable_title_in_template = false; |
||
| 53 | |||
| 54 | |||
| 55 | public function getCMSFields() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Version viewer must only be added at if this is the final getCMSFields for a class. |
||
| 125 | * in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main |
||
| 126 | * To do this we test if getCMSFields is from the current class |
||
| 127 | */ |
||
| 128 | public function isEndofLine($className) |
||
| 138 | |||
| 139 | |||
| 140 | public function onBeforeWrite() |
||
| 154 | |||
| 155 | public function i18n_singular_name() |
||
| 159 | |||
| 160 | public function getElementType() |
||
| 164 | |||
| 165 | View Code Duplication | public function getTitle() |
|
| 177 | |||
| 178 | View Code Duplication | public function getCMSTitle() |
|
| 189 | |||
| 190 | public function canView($member = null) |
||
| 194 | |||
| 195 | public function canEdit($member = null) |
||
| 199 | |||
| 200 | public function canDelete($member = null) |
||
| 204 | |||
| 205 | public function canCreate($member = null) |
||
| 209 | |||
| 210 | public function ControllerTop() |
||
| 214 | |||
| 215 | public function getPage() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should |
||
| 228 | * render with widget inside the |
||
| 229 | * |
||
| 230 | * @return HTML |
||
| 231 | */ |
||
| 232 | public function forTemplate($holder = true) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function getEditLink() { |
||
| 247 | |||
| 248 | public function onBeforeVersionedPublish() |
||
| 252 | } |
||
| 253 |
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.