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) |
||
42 | { |
||
43 | $fieldLabels = $this->owner->FieldLabels(); |
||
44 | $fieldLabelsRight = Config::inst()->get('SilverstripeColumnsPageExtension', 'field_labels_right'); |
||
45 | $tabTitleSummary = _t('SilverstripeColumnsPageExtension.SUMMARY_TAB', 'Summary'); |
||
46 | $tabTitleContent = _t('SilverstripeColumnsPageExtension.ADDITIONAL_CONTENT_TAB', 'MoreContent'); |
||
47 | View Code Duplication | if ($this->owner->UseSummaries()) { |
|
48 | $fields->addFieldsToTab( |
||
49 | 'Root.' . $tabTitleSummary, |
||
50 | [ |
||
51 | HTMLEditorField::create( |
||
52 | 'Summary', |
||
53 | $fieldLabels['Summary'] |
||
54 | )->setRows(3) |
||
55 | ->setRightTitle($fieldLabelsRight['Summary']), |
||
56 | UploadField::create( |
||
57 | 'SummaryImage', |
||
58 | $fieldLabels['SummaryImage'] |
||
59 | )->setRightTitle($fieldLabelsRight['SummaryImage']) |
||
60 | ] |
||
61 | ); |
||
62 | } |
||
63 | View Code Duplication | if ($this->owner->UseDefaultSidebarContent()) { |
|
64 | $fields->addFieldsToTab( |
||
65 | 'Root.' . $tabTitleContent, |
||
66 | [ |
||
67 | UploadField::create( |
||
68 | 'SidebarImage', |
||
69 | $fieldLabels['SidebarImage'] |
||
70 | )->setRightTitle($fieldLabelsRight['SidebarImage']), |
||
71 | HTMLEditorField::create( |
||
72 | 'DefaultSidebarContent', |
||
73 | $fieldLabels['DefaultSidebarContent'] |
||
74 | )->setRightTitle($fieldLabelsRight['DefaultSidebarContent']) |
||
75 | ] |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | return $fields; |
||
80 | } |
||
81 | |||
82 | |||
83 | |||
84 | /** |
||
85 | * @return boolean |
||
86 | */ |
||
87 | public function UseDefaultSideBarContent() |
||
98 | |||
99 | |||
100 | /** |
||
101 | * @return boolean |
||
102 | */ |
||
103 | public function UseSummaries() |
||
114 | |||
115 | /** |
||
116 | * @return Image | null |
||
117 | */ |
||
118 | public function MySidebarImage() |
||
140 | |||
141 | /** |
||
142 | * |
||
143 | * @return string (HTML) |
||
144 | */ |
||
145 | View Code Duplication | public function getMyDefaultSidebarContent() |
|
155 | |||
156 | /** |
||
157 | * |
||
158 | * @return string (HTML) |
||
159 | */ |
||
160 | View Code Duplication | public function getFullWidthContent() |
|
170 | |||
171 | /** |
||
172 | * |
||
173 | * @return string (HTML) |
||
174 | */ |
||
175 | View Code Duplication | public function getSummaryContent() |
|
185 | |||
186 | private static $_children_show_in_menu = []; |
||
187 | |||
188 | private $showMenuItemsFor = null; |
||
189 | |||
190 | public function setShowMenuItemsFor($showMenuItemsFor) |
||
195 | |||
196 | public function ChildrenShowInMenu($root = false) |
||
226 | |||
227 | public function MyMenuItems() |
||
263 | |||
264 | public function MyMenuItemsParentPage() |
||
276 | |||
277 | public function MyMenuItemsParentLink() |
||
285 | |||
286 | public function MyMenuItemsMenuLink($id = null) |
||
293 | } |
||
294 |
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.