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() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Version viewer must only be added at if this is the final getCMSFields for a class. |
||
| 197 | * in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main |
||
| 198 | * To do this we test if getCMSFields is from the current class |
||
| 199 | */ |
||
| 200 | public function isEndofLine($className) |
||
| 210 | |||
| 211 | |||
| 212 | public function onBeforeWrite() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Ensure that if there are elements that are virtualised from this element |
||
| 229 | * that we move the original element to replace one of the virtual elements |
||
| 230 | * But only if it's a delete not an unpublish |
||
| 231 | */ |
||
| 232 | public function onBeforeDelete() { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function i18n_singular_name() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public function getElementType() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | View Code Duplication | public function getTitle() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Get a unique anchor name |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | public function getAnchor() { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | View Code Duplication | public function getCMSTitle() |
|
| 368 | |||
| 369 | public function ControllerTop() |
||
| 373 | |||
| 374 | public function getPage() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should |
||
| 395 | * render with widget inside the |
||
| 396 | * |
||
| 397 | * @return HTML |
||
| 398 | */ |
||
| 399 | public function forTemplate($holder = true) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | public function getEditLink() { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | public function CMSEditLink($inList = false) { |
||
| 451 | |||
| 452 | public function UsedOn() { |
||
| 459 | |||
| 460 | public static function all_allowed_elements() { |
||
| 491 | |||
| 492 | public function getDefaultSearchContext() |
||
| 508 | |||
| 509 | public function setVirtualOwner(ElementVirtualLinked $virtualOwner) { |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Finds and returns elements |
||
| 515 | * that are virtual elements which link to this element |
||
| 516 | */ |
||
| 517 | public function getVirtualLinkedElements() { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Finds and returns published elements |
||
| 523 | * that are virtual elements which link to this element |
||
| 524 | */ |
||
| 525 | public function getPublishedVirtualLinkedElements() { |
||
| 532 | } |
||
| 533 |
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.