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 | public function getCMSFields() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Version viewer must only be added at if this is the final getCMSFields for a class. |
||
| 171 | * in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main |
||
| 172 | * To do this we test if getCMSFields is from the current class |
||
| 173 | */ |
||
| 174 | public function isEndofLine($className) |
||
| 184 | |||
| 185 | |||
| 186 | public function onBeforeWrite() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function i18n_singular_name() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public function getElementType() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | View Code Duplication | public function getTitle() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Get a unique anchor name |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | public function getAnchor() { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | View Code Duplication | public function getCMSTitle() |
|
| 284 | |||
| 285 | public function ControllerTop() |
||
| 289 | |||
| 290 | public function getPage() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should |
||
| 303 | * render with widget inside the |
||
| 304 | * |
||
| 305 | * @return HTML |
||
| 306 | */ |
||
| 307 | public function forTemplate($holder = true) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | public function getEditLink() { |
||
| 323 | |||
| 324 | public function onBeforeVersionedPublish() |
||
| 328 | |||
| 329 | public static function all_allowed_elements() { |
||
| 360 | |||
| 361 | public function getDefaultSearchContext() |
||
| 377 | } |
||
| 378 |
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.