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 | public function canView($member = NULL) { |
||
| 196 | |||
| 197 | public function canDelete($member = NULL) { |
||
| 203 | |||
| 204 | public function canEdit($member = NULL) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Version viewer must only be added at if this is the final getCMSFields for a class. |
||
| 213 | * in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main |
||
| 214 | * To do this we test if getCMSFields is from the current class |
||
| 215 | */ |
||
| 216 | public function isEndofLine($className) |
||
| 226 | |||
| 227 | |||
| 228 | public function onBeforeWrite() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Ensure that if there are elements that are virtualised from this element |
||
| 245 | * that we move the original element to replace one of the virtual elements |
||
| 246 | * But only if it's a delete not an unpublish |
||
| 247 | */ |
||
| 248 | public function onBeforeDelete() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | public function i18n_singular_name() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | public function getElementType() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | View Code Duplication | public function getTitle() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Get a unique anchor name |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | public function getAnchor() { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | View Code Duplication | public function getCMSTitle() |
|
| 384 | |||
| 385 | public function ControllerTop() |
||
| 389 | |||
| 390 | public function getPage() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should |
||
| 407 | * render with widget inside the |
||
| 408 | * |
||
| 409 | * @return HTML |
||
| 410 | */ |
||
| 411 | public function forTemplate($holder = true) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | public function getEditLink() { |
||
| 431 | |||
| 432 | public function onBeforeVersionedPublish() |
||
| 436 | |||
| 437 | public static function all_allowed_elements() { |
||
| 468 | |||
| 469 | public function getDefaultSearchContext() |
||
| 485 | |||
| 486 | public function setVirtualOwner(ElementVirtualLinked $virtualOwner) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Finds and returns elements |
||
| 492 | * that are virtual elements which link to this element |
||
| 493 | */ |
||
| 494 | public function getVirtualLinkedElements() { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Finds and returns published elements |
||
| 500 | * that are virtual elements which link to this element |
||
| 501 | */ |
||
| 502 | public function getPublishedVirtualLinkedElements() { |
||
| 509 | } |
||
| 510 |
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.