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 |
||
| 5 | class ImagesWithStyleSelection extends DataObject |
||
|
|
|||
| 6 | { |
||
| 7 | |||
| 8 | |||
| 9 | ####################### |
||
| 10 | ### Names Section |
||
| 11 | ####################### |
||
| 12 | |||
| 13 | private static $singular_name = 'Selection of Images'; |
||
| 14 | |||
| 15 | public function i18n_singular_name() |
||
| 19 | |||
| 20 | private static $plural_name = 'Selections of Images'; |
||
| 21 | |||
| 22 | public function i18n_plural_name() |
||
| 26 | |||
| 27 | |||
| 28 | ####################### |
||
| 29 | ### Model Section |
||
| 30 | ####################### |
||
| 31 | |||
| 32 | private static $db = [ |
||
| 33 | 'Title' => 'Varchar(255)', // this needs to be lengthy to avoid the same names ... |
||
| 34 | 'Description' => 'Text' |
||
| 35 | ]; |
||
| 36 | |||
| 37 | private static $has_one = [ |
||
| 38 | 'PlaceToStoreImages' => 'Folder' |
||
| 39 | ]; |
||
| 40 | |||
| 41 | private static $many_many = [ |
||
| 42 | 'StyledImages' => 'ImageWithStyle' |
||
| 43 | ]; |
||
| 44 | |||
| 45 | private static $many_many_extraFields = [ |
||
| 46 | 'StyledImages' => [ |
||
| 47 | 'SortOrder' => 'Int', |
||
| 48 | ] |
||
| 49 | ]; |
||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | ####################### |
||
| 54 | ### Further DB Field Details |
||
| 55 | ####################### |
||
| 56 | |||
| 57 | private static $indexes = [ |
||
| 58 | 'Created' => true, |
||
| 59 | 'Title' => 'unique("Title")' |
||
| 60 | ]; |
||
| 61 | |||
| 62 | private static $defaults = [ |
||
| 63 | 'Title' => '' |
||
| 64 | ]; |
||
| 65 | |||
| 66 | private static $default_sort = [ |
||
| 67 | 'Created' => 'DESC' |
||
| 68 | ]; |
||
| 69 | |||
| 70 | private static $required_fields = [ |
||
| 71 | 'Title' |
||
| 72 | ]; |
||
| 73 | |||
| 74 | private static $searchable_fields = [ |
||
| 75 | 'Title' => 'PartialMatchFilter' |
||
| 76 | ]; |
||
| 77 | |||
| 78 | |||
| 79 | ####################### |
||
| 80 | ### Field Names and Presentation Section |
||
| 81 | ####################### |
||
| 82 | |||
| 83 | private static $field_labels = [ |
||
| 84 | 'StyledImages' => 'Images to be included' |
||
| 85 | ]; |
||
| 86 | |||
| 87 | private static $field_labels_right = [ |
||
| 88 | 'StyledImages' => 'Select as many as you like and sort them in the right order' |
||
| 89 | ]; |
||
| 90 | |||
| 91 | private static $summary_fields = [ |
||
| 92 | 'Title' => 'Name', |
||
| 93 | 'StyledImages.Count' => 'Number of Images' |
||
| 94 | ]; |
||
| 95 | |||
| 96 | |||
| 97 | ####################### |
||
| 98 | ### Casting Section |
||
| 99 | ####################### |
||
| 100 | |||
| 101 | |||
| 102 | ####################### |
||
| 103 | ### can Section |
||
| 104 | ####################### |
||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | ####################### |
||
| 109 | ### write Section |
||
| 110 | ####################### |
||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | View Code Duplication | public function validate() |
|
| 160 | |||
| 161 | |||
| 162 | public function onBeforeWrite() |
||
| 186 | |||
| 187 | public function requireDefaultRecords() |
||
| 192 | |||
| 193 | |||
| 194 | ####################### |
||
| 195 | ### Import / Export Section |
||
| 196 | ####################### |
||
| 197 | |||
| 198 | public function getExportFields() |
||
| 203 | |||
| 204 | |||
| 205 | |||
| 206 | ####################### |
||
| 207 | ### CMS Edit Section |
||
| 208 | ####################### |
||
| 209 | |||
| 210 | |||
| 211 | public function CMSEditLink() |
||
| 217 | |||
| 218 | public function CMSAddLink() |
||
| 224 | |||
| 225 | |||
| 226 | public function getCMSFields() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @return DataList |
||
| 279 | */ |
||
| 280 | public function RawImages() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * force the list to use a certain folder ... |
||
| 296 | * @param string $folderName |
||
| 297 | * @return ImagesWithStyleSelection |
||
| 298 | */ |
||
| 299 | public function createFolder($folderName) |
||
| 307 | } |
||
| 308 |
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.