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 SilverstripeColumnsPageExtension 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 SilverstripeColumnsPageExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class SilverstripeColumnsPageExtension extends DataExtension |
||
|
|
|||
| 10 | { |
||
| 11 | private static $db = [ |
||
| 12 | 'Summary' => 'HTMLVarchar(255)', |
||
| 13 | 'DefaultSidebarContent' => 'HTMLText' |
||
| 14 | ]; |
||
| 15 | |||
| 16 | private static $has_one = [ |
||
| 17 | 'SummaryImage' => 'Image', |
||
| 18 | 'SidebarImage' => 'Image' |
||
| 19 | ]; |
||
| 20 | |||
| 21 | private static $casting = [ |
||
| 22 | 'MyDefaultSidebarContent' => 'HTMLText', |
||
| 23 | 'FullWidthContent' => 'HTMLText', |
||
| 24 | 'SummaryContent' => 'HTMLText' |
||
| 25 | ]; |
||
| 26 | |||
| 27 | private static $field_labels = [ |
||
| 28 | 'Summary' => 'Page Summary', |
||
| 29 | 'DefaultSidebarContent' => 'Sidebar content', |
||
| 30 | 'SummaryImage' => 'Image for Summaries', |
||
| 31 | 'SidebarImage' => 'Sidebar Image' |
||
| 32 | ]; |
||
| 33 | |||
| 34 | private static $field_labels_right = [ |
||
| 35 | 'Summary' => 'A summary of the page for use on other pages.', |
||
| 36 | 'DefaultSidebarContent' => 'The sidebar show up to the right of the main content. It is usually for something like DID YOU KNOW? or CONTACT DETAILS.', |
||
| 37 | 'SummaryImage' => 'Image used to show a link to this page together with the summary of the page provided.', |
||
| 38 | 'SidebarImage' => 'Image to show up in the sidebar instead of content.' |
||
| 39 | ]; |
||
| 40 | |||
| 41 | public function updateCMSFields(FieldList $fields) |
||
| 80 | |||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * @return boolean |
||
| 85 | */ |
||
| 86 | function UseDefaultSideBarContent() |
||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * @return boolean |
||
| 101 | */ |
||
| 102 | function UseSummaries() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return Image | null |
||
| 116 | */ |
||
| 117 | function MySidebarImage() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * |
||
| 142 | * @return string (HTML) |
||
| 143 | */ |
||
| 144 | View Code Duplication | function getMyDefaultSidebarContent() |
|
| 154 | |||
| 155 | /** |
||
| 156 | * |
||
| 157 | * @return string (HTML) |
||
| 158 | */ |
||
| 159 | View Code Duplication | function getFullWidthContent() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * |
||
| 172 | * @return string (HTML) |
||
| 173 | */ |
||
| 174 | View Code Duplication | function getSummaryContent() |
|
| 184 | |||
| 185 | private static $_children_show_in_menu = []; |
||
| 186 | |||
| 187 | private $showMenuItemsFor = null; |
||
| 188 | |||
| 189 | public function setShowMenuItemsFor($showMenuItemsFor) { |
||
| 193 | |||
| 194 | function ChildrenShowInMenu($root = false) |
||
| 224 | |||
| 225 | function MyMenuItems() |
||
| 260 | |||
| 261 | function MyMenuItemsParentPage() |
||
| 273 | |||
| 274 | function MyMenuItemsParentLink() |
||
| 282 | |||
| 283 | function MyMenuItemsMenuLink($id = null) |
||
| 290 | |||
| 291 | } |
||
| 292 |
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.