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() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Version viewer must only be added at if this is the final getCMSFields for a class. |
||
| 203 | * in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main |
||
| 204 | * To do this we test if getCMSFields is from the current class |
||
| 205 | */ |
||
| 206 | public function isEndofLine($className) |
||
| 216 | |||
| 217 | |||
| 218 | public function onBeforeWrite() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Ensure that if there are elements that are virtualised from this element |
||
| 235 | * that we move the original element to replace one of the virtual elements |
||
| 236 | * But only if it's a delete not an unpublish |
||
| 237 | */ |
||
| 238 | public function onBeforeDelete() { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | public function i18n_singular_name() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | public function getElementType() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | View Code Duplication | public function getTitle() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Get a unique anchor name |
||
| 325 | * |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | public function getAnchor() { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | View Code Duplication | public function getCMSTitle() |
|
| 374 | |||
| 375 | public function ControllerTop() |
||
| 379 | |||
| 380 | public function getPage() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should |
||
| 401 | * render with widget inside the |
||
| 402 | * |
||
| 403 | * @return HTML |
||
| 404 | */ |
||
| 405 | public function forTemplate($holder = true) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | public function getEditLink() { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | public function CMSEditLink($inList = false) { |
||
| 457 | |||
| 458 | View Code Duplication | public function PageLink() { |
|
| 465 | |||
| 466 | View Code Duplication | public function PageCMSEditLink() { |
|
| 473 | |||
| 474 | public function ParentCMSEditLink() { |
||
| 483 | |||
| 484 | public static function all_allowed_elements() { |
||
| 515 | |||
| 516 | public function getDefaultSearchContext() |
||
| 532 | |||
| 533 | public function setVirtualOwner(ElementVirtualLinked $virtualOwner) { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Finds and returns elements |
||
| 539 | * that are virtual elements which link to this element |
||
| 540 | */ |
||
| 541 | public function getVirtualLinkedElements() { |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Finds and returns published elements |
||
| 547 | * that are virtual elements which link to this element |
||
| 548 | */ |
||
| 549 | public function getPublishedVirtualLinkedElements() { |
||
| 556 | } |
||
| 557 |
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.