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:
Complex classes like BaseElement often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseElement, and based on these observations, apply Extract Interface, too.
| 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 | 'AvailableGlobally' => 'Boolean(1)'  | 
            ||
| 15 | );  | 
            ||
| 16 | |||
| 17 | /**  | 
            ||
| 18 | * @var array $has_one  | 
            ||
| 19 | */  | 
            ||
| 20 | private static $has_one = array(  | 
            ||
| 21 | 'List' => 'ElementList' // optional.  | 
            ||
| 22 | );  | 
            ||
| 23 | |||
| 24 | /**  | 
            ||
| 25 | * @var array $has_many  | 
            ||
| 26 | */  | 
            ||
| 27 | private static $has_many = array(  | 
            ||
| 28 | 'VirtualClones' => 'ElementVirtualLinked'  | 
            ||
| 29 | );  | 
            ||
| 30 | |||
| 31 | /**  | 
            ||
| 32 | * @var string  | 
            ||
| 33 | */  | 
            ||
| 34 | private static $title = "Content Block";  | 
            ||
| 35 | |||
| 36 | /**  | 
            ||
| 37 | * @var string  | 
            ||
| 38 | */  | 
            ||
| 39 | private static $singular_name = 'Content Block';  | 
            ||
| 40 | |||
| 41 | /**  | 
            ||
| 42 | * @var array  | 
            ||
| 43 | */  | 
            ||
| 44 | private static $summary_fields = array(  | 
            ||
| 45 | 'ID' => 'ID',  | 
            ||
| 46 | 'Title' => 'Title',  | 
            ||
| 47 | 'ElementType' => 'Type'  | 
            ||
| 48 | );  | 
            ||
| 49 | |||
| 50 | /**  | 
            ||
| 51 | * @var array  | 
            ||
| 52 | */  | 
            ||
| 53 | private static $searchable_fields = array(  | 
            ||
| 54 | 'ID' => array(  | 
            ||
| 55 | 'field' => 'NumericField'  | 
            ||
| 56 | ),  | 
            ||
| 57 | 'Title',  | 
            ||
| 58 | 'LastEdited',  | 
            ||
| 59 | 'ClassName',  | 
            ||
| 60 | 'AvailableGlobally'  | 
            ||
| 61 | );  | 
            ||
| 62 | |||
| 63 | /**  | 
            ||
| 64 | * @var boolean  | 
            ||
| 65 | */  | 
            ||
| 66 | private static $enable_title_in_template = false;  | 
            ||
| 67 | |||
| 68 | /**  | 
            ||
| 69 | * Enable for backwards compatibility  | 
            ||
| 70 | *  | 
            ||
| 71 | * @var boolean  | 
            ||
| 72 | */  | 
            ||
| 73 | private static $disable_pretty_anchor_name = false;  | 
            ||
| 74 | |||
| 75 | /**  | 
            ||
| 76 | * Store used anchor names, this is to avoid title clashes  | 
            ||
| 77 | * when calling 'getAnchor'  | 
            ||
| 78 | *  | 
            ||
| 79 | * @var array  | 
            ||
| 80 | */  | 
            ||
| 81 | protected static $_used_anchors = array();  | 
            ||
| 82 | |||
| 83 | /**  | 
            ||
| 84 | * For caching 'getAnchor'  | 
            ||
| 85 | *  | 
            ||
| 86 | * @var string  | 
            ||
| 87 | */  | 
            ||
| 88 | protected $_anchor = null;  | 
            ||
| 89 | |||
| 90 | /**  | 
            ||
| 91 | * @var Object  | 
            ||
| 92 | * The virtual owner VirtualLinkedElement  | 
            ||
| 93 | */  | 
            ||
| 94 | public $virtualOwner;  | 
            ||
| 95 | |||
| 96 | /**  | 
            ||
| 97 | * @config  | 
            ||
| 98 | * Elements available globally by default  | 
            ||
| 99 | */  | 
            ||
| 100 | private static $default_global_elements = true;  | 
            ||
| 101 | |||
| 102 |      public function populateDefaults() { | 
            ||
| 106 | |||
| 107 | public function getCMSFields()  | 
            ||
| 189 | |||
| 190 | /**  | 
            ||
| 191 | * Version viewer must only be added at if this is the final getCMSFields for a class.  | 
            ||
| 192 | * in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main  | 
            ||
| 193 | * To do this we test if getCMSFields is from the current class  | 
            ||
| 194 | */  | 
            ||
| 195 | public function isEndofLine($className)  | 
            ||
| 205 | |||
| 206 | |||
| 207 | public function onBeforeWrite()  | 
            ||
| 221 | |||
| 222 | /**  | 
            ||
| 223 | * Ensure that if there are elements that are virtualised from this element  | 
            ||
| 224 | * that we move the original element to replace one of the virtual elements  | 
            ||
| 225 | * But only if it's a delete not an unpublish  | 
            ||
| 226 | */  | 
            ||
| 227 |     public function onBeforeDelete() { | 
            ||
| 279 | |||
| 280 | /**  | 
            ||
| 281 | * @return string  | 
            ||
| 282 | */  | 
            ||
| 283 | public function i18n_singular_name()  | 
            ||
| 287 | |||
| 288 | /**  | 
            ||
| 289 | * @return string  | 
            ||
| 290 | */  | 
            ||
| 291 | public function getElementType()  | 
            ||
| 295 | |||
| 296 | /**  | 
            ||
| 297 | * @return string  | 
            ||
| 298 | */  | 
            ||
| 299 | View Code Duplication | public function getTitle()  | 
            |
| 311 | |||
| 312 | /**  | 
            ||
| 313 | * Get a unique anchor name  | 
            ||
| 314 | *  | 
            ||
| 315 | * @return string  | 
            ||
| 316 | */  | 
            ||
| 317 |     public function getAnchor() { | 
            ||
| 348 | |||
| 349 | /**  | 
            ||
| 350 | * @return string  | 
            ||
| 351 | */  | 
            ||
| 352 | View Code Duplication | public function getCMSTitle()  | 
            |
| 363 | |||
| 364 | public function ControllerTop()  | 
            ||
| 368 | |||
| 369 | public function getPage()  | 
            ||
| 383 | |||
| 384 | /**  | 
            ||
| 385 |      * Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should | 
            ||
| 386 | * render with widget inside the  | 
            ||
| 387 | *  | 
            ||
| 388 | * @return HTML  | 
            ||
| 389 | */  | 
            ||
| 390 | public function forTemplate($holder = true)  | 
            ||
| 398 | |||
| 399 | /**  | 
            ||
| 400 | * @return string  | 
            ||
| 401 | */  | 
            ||
| 402 |     public function getEditLink() { | 
            ||
| 410 | |||
| 411 | public function onBeforeVersionedPublish()  | 
            ||
| 415 | |||
| 416 | 		public static function all_allowed_elements() { | 
            ||
| 447 | |||
| 448 | public function getDefaultSearchContext()  | 
            ||
| 464 | |||
| 465 |     public function setVirtualOwner(ElementVirtualLinked $virtualOwner) { | 
            ||
| 468 | |||
| 469 | /**  | 
            ||
| 470 | * Finds and returns elements  | 
            ||
| 471 | * that are virtual elements which link to this element  | 
            ||
| 472 | */  | 
            ||
| 473 |     public function getVirtualLinkedElements() { | 
            ||
| 476 | |||
| 477 | /**  | 
            ||
| 478 | * Finds and returns published elements  | 
            ||
| 479 | * that are virtual elements which link to this element  | 
            ||
| 480 | */  | 
            ||
| 481 |     public function getPublishedVirtualLinkedElements() { | 
            ||
| 488 | }  | 
            ||
| 489 | 
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.