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() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Version viewer must only be added at if this is the final getCMSFields for a class. |
||
| 106 | * in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main |
||
| 107 | * To do this we test if getCMSFields is from the current class |
||
| 108 | */ |
||
| 109 | public function isEndofLine($className) |
||
| 119 | |||
| 120 | |||
| 121 | public function onBeforeWrite() |
||
| 135 | |||
| 136 | public function i18n_singular_name() |
||
| 140 | |||
| 141 | public function getElementType() |
||
| 145 | |||
| 146 | View Code Duplication | public function getTitle() |
|
| 158 | |||
| 159 | View Code Duplication | public function getCMSTitle() |
|
| 170 | |||
| 171 | public function canView($member = null) |
||
| 175 | |||
| 176 | public function canEdit($member = null) |
||
| 180 | |||
| 181 | public function canDelete($member = null) |
||
| 185 | |||
| 186 | public function canCreate($member = null) |
||
| 190 | |||
| 191 | public function ControllerTop() |
||
| 195 | |||
| 196 | public function getPage() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should |
||
| 209 | * render with widget inside the |
||
| 210 | * |
||
| 211 | * @return HTML |
||
| 212 | */ |
||
| 213 | public function forTemplate($holder = true) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | public function getEditLink() { |
||
| 228 | } |
||
| 229 |
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.