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 ElementPageExtension 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 ElementPageExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class ElementPageExtension extends DataExtension |
||
|
|
|||
| 7 | { |
||
| 8 | |||
| 9 | /** |
||
| 10 | * @config |
||
| 11 | * |
||
| 12 | * @var string $elements_title Title of the element in the CMS. |
||
| 13 | */ |
||
| 14 | private static $elements_title = 'Content Blocks'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @config |
||
| 18 | * |
||
| 19 | * @var boolean $disable_element_publish_button Disable publish / unpublish buttons in GridFieldDetailForm. |
||
| 20 | */ |
||
| 21 | private static $disable_element_publish_button = false; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @config |
||
| 25 | * |
||
| 26 | * @var array $ignored_classes Classes to ignore adding elements too. |
||
| 27 | */ |
||
| 28 | private static $ignored_classes = array(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @config |
||
| 32 | * |
||
| 33 | * @var boolean |
||
| 34 | */ |
||
| 35 | private static $copy_element_content_to_contentfield = true; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @config |
||
| 39 | * |
||
| 40 | * @var boolean |
||
| 41 | */ |
||
| 42 | private static $clear_contentfield = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array $db |
||
| 46 | */ |
||
| 47 | private static $db = array(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array $has_one |
||
| 51 | */ |
||
| 52 | private static $has_one = array( |
||
| 53 | 'ElementArea' => 'ElementalArea' |
||
| 54 | ); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Setup the CMS Fields |
||
| 58 | * |
||
| 59 | * @param FieldList |
||
| 60 | */ |
||
| 61 | public function updateCMSFields(FieldList $fields) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | public function getAvailableTypes() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Make sure there is always a WidgetArea sidebar for adding widgets |
||
| 161 | * |
||
| 162 | */ |
||
| 163 | public function onBeforeWrite() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return boolean |
||
| 216 | */ |
||
| 217 | public function supportsElemental() { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * If the page is duplicated, copy the widgets across too. |
||
| 237 | * |
||
| 238 | * Gets called twice from either direction, due to bad DataObject and SiteTree code, hence the weird if statement |
||
| 239 | * |
||
| 240 | * @return Page The duplicated page |
||
| 241 | */ |
||
| 242 | public function onAfterDuplicate($duplicatePage) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * If the page is duplicated across subsites, copy the widgets across too. |
||
| 262 | * |
||
| 263 | * @return Page The duplicated page |
||
| 264 | */ |
||
| 265 | public function onAfterDuplicateToSubsite($originalPage) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Publish |
||
| 283 | */ |
||
| 284 | public function onAfterPublish() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Roll back all changes if the parent page has a rollback event |
||
| 309 | * |
||
| 310 | * Only do rollback if it's the 'cancel draft changes' rollback, not a specific version |
||
| 311 | * rollback. |
||
| 312 | * |
||
| 313 | * @param string $version |
||
| 314 | * @return null |
||
| 315 | */ |
||
| 316 | public function onBeforeRollback($version) |
||
| 337 | } |
||
| 338 |
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.