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 | 'AvailableGlobally' => 'Boolean(1)' |
||
15 | ); |
||
16 | |||
17 | /** |
||
18 | * @var array $has_one |
||
19 | */ |
||
20 | private static $has_one = array( |
||
21 | 'List' => 'ElementList' // optional. |
||
22 | ); |
||
23 | |||
24 | /** |
||
25 | * @var array $has_many |
||
26 | */ |
||
27 | private static $has_many = array( |
||
28 | 'VirtualClones' => 'ElementVirtualLinked' |
||
29 | ); |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private static $title = 'Content Block'; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private static $singular_name = 'Content Block'; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private static $summary_fields = array( |
||
45 | 'ID' => 'ID', |
||
46 | 'Title' => 'Title', |
||
47 | 'ElementType' => 'Type' |
||
48 | ); |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | private static $searchable_fields = array( |
||
54 | 'ID' => array( |
||
55 | 'field' => 'NumericField' |
||
56 | ), |
||
57 | 'Title', |
||
58 | 'LastEdited', |
||
59 | 'ClassName', |
||
60 | 'AvailableGlobally' |
||
61 | ); |
||
62 | |||
63 | /** |
||
64 | * @var boolean |
||
65 | */ |
||
66 | private static $enable_title_in_template = false; |
||
67 | |||
68 | /** |
||
69 | * Enable for backwards compatibility |
||
70 | * |
||
71 | * @var boolean |
||
72 | */ |
||
73 | private static $disable_pretty_anchor_name = false; |
||
74 | |||
75 | /** |
||
76 | * Store used anchor names, this is to avoid title clashes |
||
77 | * when calling 'getAnchor' |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected static $_used_anchors = array(); |
||
82 | |||
83 | /** |
||
84 | * For caching 'getAnchor' |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | protected $_anchor = null; |
||
89 | |||
90 | /** |
||
91 | * @var Object |
||
92 | * The virtual owner VirtualLinkedElement |
||
93 | */ |
||
94 | public $virtualOwner; |
||
95 | |||
96 | /** |
||
97 | * @config |
||
98 | * Elements available globally by default |
||
99 | */ |
||
100 | private static $default_global_elements = true; |
||
101 | |||
102 | public function populateDefaults() { |
||
106 | |||
107 | public function getCMSFields() |
||
108 | { |
||
109 | $fields = $this->scaffoldFormFields(array( |
||
110 | 'includeRelations' => ($this->ID > 0), |
||
111 | 'tabbed' => true, |
||
112 | 'ajaxSafe' => true |
||
113 | )); |
||
114 | |||
115 | $fields->insertAfter(new ReadonlyField('ClassNameTranslated', _t('BaseElement.TYPE', 'Type'), $this->i18n_singular_name()), 'Title'); |
||
116 | $fields->removeByName('ListID'); |
||
117 | $fields->removeByName('ParentID'); |
||
118 | $fields->removeByName('Sort'); |
||
119 | $fields->removeByName('ExtraClass'); |
||
120 | $fields->removeByName('AvailableGlobally'); |
||
121 | |||
122 | if (!$this->config()->enable_title_in_template) { |
||
123 | $fields->removeByName('HideTitle'); |
||
124 | $title = $fields->fieldByName('Root.Main.Title'); |
||
125 | |||
126 | if ($title) { |
||
127 | $title->setRightTitle('For reference only. Does not appear in the template.'); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | $fields->addFieldToTab('Root.Settings', new CheckboxField('Enabled')); |
||
132 | $fields->addFieldToTab('Root.Settings', new CheckboxField('AvailableGlobally', 'Available globally - can be linked to multiple pages')); |
||
133 | $fields->addFieldToTab('Root.Settings', new TextField('ExtraClass', 'Extra CSS Classes to add')); |
||
134 | |||
135 | if (!is_a($this, 'ElementList')) { |
||
136 | $lists = ElementList::get()->filter('ParentID', $this->ParentID); |
||
137 | |||
138 | if ($lists->exists()) { |
||
139 | $fields->addFieldToTab('Root.Settings', |
||
140 | $move = new DropdownField('MoveToListID', 'Move this to another list', $lists->map('ID', 'CMSTitle'), '') |
||
141 | ); |
||
142 | |||
143 | $move->setEmptyString('Select a list..'); |
||
144 | $move->setHasEmptyDefault(true); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | if($virtual = $fields->dataFieldByName('VirtualClones')) { |
||
149 | if($this->Parent() && $this->Parent()->exists() && $this->Parent()->getOwnerPage() && $this->Parent()->getOwnerPage()->exists()) { |
||
150 | $tab = $fields->findOrMakeTab('Root.VirtualClones'); |
||
151 | $tab->setTitle(_t('BaseElement.VIRTUALTABTITLE', 'Linked To')); |
||
152 | |||
153 | $ownerPage = $this->Parent()->getOwnerPage(); |
||
154 | $tab->push(new LiteralField('DisplaysOnPage', sprintf( |
||
155 | "<p>The original content block appears on <a href='%s'>%s</a></p>", |
||
156 | ($ownerPage->hasMethod('CMSEditLink') && $ownerPage->canEdit()) ? $ownerPage->CMSEditLink() : $ownerPage->Link(), |
||
157 | $ownerPage->MenuTitle |
||
158 | ))); |
||
159 | |||
160 | $virtual->setConfig(new GridFieldConfig_Base()); |
||
161 | $virtual |
||
162 | ->setTitle(_t('BaseElement.OTHERPAGES', 'Other pages')) |
||
163 | ->getConfig() |
||
164 | ->removeComponentsByType('GridFieldAddExistingAutocompleter') |
||
165 | ->removeComponentsByType('GridFieldAddNewButton') |
||
166 | ->removeComponentsByType('GridFieldDeleteAction') |
||
167 | ->removeComponentsByType('GridFieldDetailForm') |
||
168 | ->addComponent(new ElementalGridFieldDeleteAction()); |
||
169 | |||
170 | $virtual->getConfig() |
||
171 | ->getComponentByType('GridFieldDataColumns') |
||
172 | ->setDisplayFields(array( |
||
173 | 'getPage.Title' => 'Title', |
||
174 | 'getPage.Link' => 'Link' |
||
175 | )); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | $this->extend('updateCMSFields', $fields); |
||
180 | |||
181 | if ($this->IsInDB()) { |
||
182 | if ($this->isEndofLine('BaseElement') && $this->hasExtension('VersionViewerDataObject')) { |
||
183 | $fields = $this->addVersionViewer($fields, $this); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | return $fields; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Version viewer must only be added at if this is the final getCMSFields for a class. |
||
192 | * in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main |
||
193 | * To do this we test if getCMSFields is from the current class |
||
194 | */ |
||
195 | public function isEndofLine($className) |
||
205 | |||
206 | |||
207 | public function onBeforeWrite() |
||
221 | |||
222 | /** |
||
223 | * Ensure that if there are elements that are virtualised from this element |
||
224 | * that we move the original element to replace one of the virtual elements |
||
225 | * But only if it's a delete not an unpublish |
||
226 | */ |
||
227 | public function onBeforeDelete() { |
||
279 | |||
280 | /** |
||
281 | * @return string |
||
282 | */ |
||
283 | public function i18n_singular_name() |
||
287 | |||
288 | /** |
||
289 | * @return string |
||
290 | */ |
||
291 | public function getElementType() |
||
295 | |||
296 | /** |
||
297 | * @return string |
||
298 | */ |
||
299 | View Code Duplication | public function getTitle() |
|
311 | |||
312 | /** |
||
313 | * Get a unique anchor name |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | public function getAnchor() { |
||
348 | |||
349 | /** |
||
350 | * @return string |
||
351 | */ |
||
352 | View Code Duplication | public function getCMSTitle() |
|
363 | |||
364 | public function ControllerTop() |
||
368 | |||
369 | public function getPage() |
||
387 | |||
388 | /** |
||
389 | * Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should |
||
390 | * render with widget inside the |
||
391 | * |
||
392 | * @return HTML |
||
393 | */ |
||
394 | public function forTemplate($holder = true) |
||
402 | |||
403 | /** |
||
404 | * @return string |
||
405 | */ |
||
406 | public function getEditLink() { |
||
414 | |||
415 | public function onBeforeVersionedPublish() |
||
419 | |||
420 | public static function all_allowed_elements() { |
||
451 | |||
452 | public function getDefaultSearchContext() |
||
468 | |||
469 | public function setVirtualOwner(ElementVirtualLinked $virtualOwner) { |
||
472 | |||
473 | /** |
||
474 | * Finds and returns elements |
||
475 | * that are virtual elements which link to this element |
||
476 | */ |
||
477 | public function getVirtualLinkedElements() { |
||
480 | |||
481 | /** |
||
482 | * Finds and returns published elements |
||
483 | * that are virtual elements which link to this element |
||
484 | */ |
||
485 | public function getPublishedVirtualLinkedElements() { |
||
492 | } |
||
493 |
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.