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:
| 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 | ); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var boolean |
||
| 62 | */ |
||
| 63 | private static $enable_title_in_template = false; |
||
| 64 | |||
| 65 | |||
| 66 | public function getCMSFields() |
||
| 67 | { |
||
| 68 | $fields = $this->scaffoldFormFields(array( |
||
| 69 | 'includeRelations' => ($this->ID > 0), |
||
| 70 | 'tabbed' => true, |
||
| 71 | 'ajaxSafe' => true |
||
| 72 | )); |
||
| 73 | |||
| 74 | $fields->insertAfter(new ReadonlyField('ClassNameTranslated', _t('BaseElement.TYPE', 'Type'), $this->i18n_singular_name()), 'Title'); |
||
| 75 | $fields->removeByName('ListID'); |
||
| 76 | $fields->removeByName('ParentID'); |
||
| 77 | $fields->removeByName('Sort'); |
||
| 78 | $fields->removeByName('ExtraClass'); |
||
| 79 | |||
| 80 | if (!$this->config()->enable_title_in_template) { |
||
| 81 | $fields->removeByName('HideTitle'); |
||
| 82 | $title = $fields->fieldByName('Root.Main.Title'); |
||
| 83 | |||
| 84 | if ($title) { |
||
| 85 | $title->setRightTitle('For reference only. Does not appear in the template.'); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | $fields->addFieldToTab('Root.Settings', new TextField('ExtraClass', 'Extra CSS Classes to add')); |
||
| 90 | |||
| 91 | if (!is_a($this, 'ElementList')) { |
||
| 92 | $lists = ElementList::get()->filter('ParentID', $this->ParentID); |
||
| 93 | |||
| 94 | if ($lists->exists()) { |
||
| 95 | $fields->addFieldToTab('Root.Settings', |
||
| 96 | $move = new DropdownField('MoveToListID', 'Move this to another list', $lists->map('ID', 'CMSTitle'), '') |
||
| 97 | ); |
||
| 98 | |||
| 99 | $move->setEmptyString('Select a list..'); |
||
| 100 | $move->setHasEmptyDefault(true); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | |||
| 105 | if($virtual = $fields->dataFieldByName('VirtualClones')) { |
||
| 106 | if($this->Parent() && $this->Parent()->exists() && $this->Parent()->getOwnerPage() && $this->Parent()->getOwnerPage()->exists()) { |
||
| 107 | $tab = $fields->findOrMakeTab('Root.VirtualClones'); |
||
| 108 | $tab->setTitle(_t('BaseElement.VIRTUALTABTITLE', 'Linked To')); |
||
| 109 | |||
| 110 | $tab->push(new LiteralField('DisplaysOnPage', sprintf( |
||
| 111 | "<p>The original content block appears on <a href='%s'>%s</a></p>", |
||
| 112 | $this->Parent()->getOwnerPage()->Link(), |
||
| 113 | $this->Parent()->getOwnerPage()->MenuTitle |
||
| 114 | ))); |
||
| 115 | |||
| 116 | $virtual->setConfig(new GridFieldConfig_Base()); |
||
| 117 | $virtual |
||
| 118 | ->setTitle(_t('BaseElement.OTHERPAGES', 'Other pages')) |
||
| 119 | ->getConfig() |
||
| 120 | ->removeComponentsByType('GridFieldAddExistingAutocompleter') |
||
| 121 | ->removeComponentsByType('GridFieldAddNewButton') |
||
| 122 | ->removeComponentsByType('GridFieldDeleteAction') |
||
| 123 | ->removeComponentsByType('GridFieldDetailForm') |
||
| 124 | ->addComponent(new ElementalGridFieldDeleteAction()); |
||
| 125 | |||
| 126 | $virtual->getConfig() |
||
| 127 | ->getComponentByType('GridFieldDataColumns') |
||
| 128 | ->setDisplayFields(array( |
||
| 129 | 'getPage.Title' => 'Title', |
||
| 130 | 'getPage.Link' => 'Link' |
||
| 131 | )); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | $this->extend('updateCMSFields', $fields); |
||
| 136 | |||
| 137 | if ($this->IsInDB()) { |
||
| 138 | if ($this->isEndofLine('BaseElement') && $this->hasExtension('VersionViewerDataObject')) { |
||
| 139 | $fields = $this->addVersionViewer($fields, $this); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | return $fields; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Version viewer must only be added at if this is the final getCMSFields for a class. |
||
| 148 | * in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main |
||
| 149 | * To do this we test if getCMSFields is from the current class |
||
| 150 | */ |
||
| 151 | public function isEndofLine($className) |
||
| 161 | |||
| 162 | |||
| 163 | public function onBeforeWrite() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function i18n_singular_name() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function getElementType() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | View Code Duplication | public function getTitle() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | View Code Duplication | public function getCMSTitle() |
|
| 224 | |||
| 225 | public function ControllerTop() |
||
| 229 | |||
| 230 | public function getPage() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should |
||
| 243 | * render with widget inside the |
||
| 244 | * |
||
| 245 | * @return HTML |
||
| 246 | */ |
||
| 247 | public function forTemplate($holder = true) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | public function getEditLink() { |
||
| 263 | |||
| 264 | public function onBeforeVersionedPublish() |
||
| 268 | } |
||
| 269 |
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.