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 array |
||
| 51 | */ |
||
| 52 | private static $searchable_fields = array( |
||
| 53 | 'ID' => array( |
||
| 54 | 'field' => 'NumericField' |
||
| 55 | ), |
||
| 56 | 'Title', |
||
| 57 | 'LastEdited', |
||
| 58 | 'ClassName' |
||
| 59 | ); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var boolean |
||
| 63 | */ |
||
| 64 | private static $enable_title_in_template = false; |
||
| 65 | |||
| 66 | |||
| 67 | public function getCMSFields() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Version viewer must only be added at if this is the final getCMSFields for a class. |
||
| 149 | * in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main |
||
| 150 | * To do this we test if getCMSFields is from the current class |
||
| 151 | */ |
||
| 152 | public function isEndofLine($className) |
||
| 162 | |||
| 163 | |||
| 164 | public function onBeforeWrite() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public function i18n_singular_name() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getElementType() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | View Code Duplication | public function getTitle() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | View Code Duplication | public function getCMSTitle() |
|
| 225 | |||
| 226 | public function ControllerTop() |
||
| 230 | |||
| 231 | public function getPage() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should |
||
| 244 | * render with widget inside the |
||
| 245 | * |
||
| 246 | * @return HTML |
||
| 247 | */ |
||
| 248 | public function forTemplate($holder = true) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | public function getEditLink() { |
||
| 264 | |||
| 265 | public function onBeforeVersionedPublish() |
||
| 269 | |||
| 270 | public static function all_allowed_elements() { |
||
| 298 | |||
| 299 | public function getDefaultSearchContext() |
||
| 315 | } |
||
| 316 |
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.