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 | ); |
||
| 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 | * Enable for backwards compatibility |
||
| 68 | * |
||
| 69 | * @var boolean |
||
| 70 | */ |
||
| 71 | private static $disable_pretty_anchor_name = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Store used anchor names, this is to avoid title clashes |
||
| 75 | * when calling 'getAnchor' |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected static $_used_anchors = array(); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * For caching 'getAnchor' |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $_anchor = null; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var Object |
||
| 90 | * The virtual owner VirtualLinkedElement |
||
| 91 | */ |
||
| 92 | public $virtualOwner; |
||
| 93 | |||
| 94 | |||
| 95 | public function getCMSFields() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Version viewer must only be added at if this is the final getCMSFields for a class. |
||
| 178 | * in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main |
||
| 179 | * To do this we test if getCMSFields is from the current class |
||
| 180 | */ |
||
| 181 | public function isEndofLine($className) |
||
| 191 | |||
| 192 | |||
| 193 | public function onBeforeWrite() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Ensure that if there are elements that are virtualised from this element |
||
| 210 | * that we move the original element to replace one of the virtual elements |
||
| 211 | * But only if it's a delete not an unpublish |
||
| 212 | */ |
||
| 213 | public function onBeforeDelete() { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public function i18n_singular_name() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | public function getElementType() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | View Code Duplication | public function getTitle() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Get a unique anchor name |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | public function getAnchor() { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | View Code Duplication | public function getCMSTitle() |
|
| 349 | |||
| 350 | public function ControllerTop() |
||
| 354 | |||
| 355 | public function getPage() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should |
||
| 372 | * render with widget inside the |
||
| 373 | * |
||
| 374 | * @return HTML |
||
| 375 | */ |
||
| 376 | public function forTemplate($holder = true) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | public function getEditLink() { |
||
| 396 | |||
| 397 | public function onBeforeVersionedPublish() |
||
| 401 | |||
| 402 | public static function all_allowed_elements() { |
||
| 433 | |||
| 434 | public function getDefaultSearchContext() |
||
| 450 | |||
| 451 | public function setVirtualOwner(ElementVirtualLinked $virtualOwner) { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Finds and returns elements |
||
| 457 | * that are virtual elements which link to this element |
||
| 458 | */ |
||
| 459 | public function getVirtualLinkedElements() { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Finds and returns published elements |
||
| 465 | * that are virtual elements which link to this element |
||
| 466 | */ |
||
| 467 | public function getPublishedVirtualLinkedElements() { |
||
| 474 | } |
||
| 475 | |||
| 477 |
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.