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 | function i18n_singular_name() |
||
| 19 | |||
| 20 | private static $plural_name = 'Selections of Images'; |
||
| 21 | |||
| 22 | function i18n_plural_name() |
||
| 26 | |||
| 27 | |||
| 28 | ####################### |
||
| 29 | ### Model Section |
||
| 30 | ####################### |
||
| 31 | |||
| 32 | private static $db = [ |
||
| 33 | 'Title' => 'Varchar', |
||
| 34 | 'Description' => 'Text' |
||
| 35 | ]; |
||
| 36 | |||
| 37 | private static $many_many = [ |
||
| 38 | 'StyledImages' => 'ImageWithStyle' |
||
| 39 | ]; |
||
| 40 | |||
| 41 | private static $many_many_extraFields = [ |
||
| 42 | 'StyledImages' => [ |
||
| 43 | 'SortOrder' => 'Int', |
||
| 44 | ] |
||
| 45 | ]; |
||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | ####################### |
||
| 50 | ### Further DB Field Details |
||
| 51 | ####################### |
||
| 52 | |||
| 53 | private static $indexes = [ |
||
| 54 | 'Created' => true, |
||
| 55 | 'Title' => 'unique("Title")' |
||
| 56 | ]; |
||
| 57 | |||
| 58 | private static $defaults = [ |
||
| 59 | 'Title' => '' |
||
| 60 | ]; |
||
| 61 | |||
| 62 | private static $default_sort = [ |
||
| 63 | 'Created' => 'DESC' |
||
| 64 | ]; |
||
| 65 | |||
| 66 | private static $required_fields = [ |
||
| 67 | 'Title' |
||
| 68 | ]; |
||
| 69 | |||
| 70 | private static $searchable_fields = [ |
||
| 71 | 'Title' => 'PartialMatchFilter' |
||
| 72 | ]; |
||
| 73 | |||
| 74 | |||
| 75 | ####################### |
||
| 76 | ### Field Names and Presentation Section |
||
| 77 | ####################### |
||
| 78 | |||
| 79 | private static $field_labels = [ |
||
| 80 | 'StyledImages' => 'Images to be included' |
||
| 81 | ]; |
||
| 82 | |||
| 83 | private static $field_labels_right = [ |
||
| 84 | 'StyledImages' => 'Select as many as you like and sort them in the right order' |
||
| 85 | ]; |
||
| 86 | |||
| 87 | private static $summary_fields = [ |
||
| 88 | 'Title' => 'Name', |
||
| 89 | 'StyledImages.Count' => 'Number of Images' |
||
| 90 | ]; |
||
| 91 | |||
| 92 | |||
| 93 | ####################### |
||
| 94 | ### Casting Section |
||
| 95 | ####################### |
||
| 96 | |||
| 97 | |||
| 98 | ####################### |
||
| 99 | ### can Section |
||
| 100 | ####################### |
||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | ####################### |
||
| 105 | ### write Section |
||
| 106 | ####################### |
||
| 107 | |||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | View Code Duplication | public function validate() |
|
| 156 | |||
| 157 | public function onBeforeWrite() |
||
| 162 | |||
| 163 | public function onAfterWrite() |
||
| 168 | |||
| 169 | public function requireDefaultRecords() |
||
| 174 | |||
| 175 | |||
| 176 | ####################### |
||
| 177 | ### Import / Export Section |
||
| 178 | ####################### |
||
| 179 | |||
| 180 | public function getExportFields() |
||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | ####################### |
||
| 189 | ### CMS Edit Section |
||
| 190 | ####################### |
||
| 191 | |||
| 192 | |||
| 193 | public function CMSEditLink() |
||
| 199 | |||
| 200 | public function CMSAddLink() |
||
| 206 | |||
| 207 | |||
| 208 | public function getCMSFields() |
||
| 240 | |||
| 241 | |||
| 242 | } |
||
| 243 |
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.