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() |
||
| 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.