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 | * @var Object |
||
| 68 | * The virtual owner VirtualLinkedElement |
||
| 69 | */ |
||
| 70 | public $virtualOwner; |
||
| 71 | |||
| 72 | |||
| 73 | public function getCMSFields() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Version viewer must only be added at if this is the final getCMSFields for a class. |
||
| 156 | * in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main |
||
| 157 | * To do this we test if getCMSFields is from the current class |
||
| 158 | */ |
||
| 159 | public function isEndofLine($className) |
||
| 169 | |||
| 170 | |||
| 171 | public function onBeforeWrite() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Ensure that if there are elements that are virtualised from this element |
||
| 188 | * that we move the original element to replace one of the virtual elements |
||
| 189 | * But only if it's a delete not an unpublish |
||
| 190 | */ |
||
| 191 | public function onBeforeDelete() { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function i18n_singular_name() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | public function getElementType() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | View Code Duplication | public function getTitle() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | View Code Duplication | public function getCMSTitle() |
|
| 290 | |||
| 291 | public function ControllerTop() |
||
| 295 | |||
| 296 | public function getPage() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should |
||
| 313 | * render with widget inside the |
||
| 314 | * |
||
| 315 | * @return HTML |
||
| 316 | */ |
||
| 317 | public function forTemplate($holder = true) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | public function getEditLink() { |
||
| 333 | |||
| 334 | public function onBeforeVersionedPublish() |
||
| 338 | |||
| 339 | public static function all_allowed_elements() { |
||
| 370 | |||
| 371 | public function getDefaultSearchContext() |
||
| 387 | |||
| 388 | public function setVirtualOwner(ElementVirtualLinked $virtualOwner) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Finds and returns elements |
||
| 394 | * that are virtual elements which link to this element |
||
| 395 | */ |
||
| 396 | public function getVirtualLinkedElements() { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Finds and returns published elements |
||
| 402 | * that are virtual elements which link to this element |
||
| 403 | */ |
||
| 404 | public function getPublishedVirtualLinkedElements() { |
||
| 411 | } |
||
| 412 | |||
| 414 |
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.