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 ImageStyle 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 ImageStyle, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class ImageStyle extends DataObject |
||
|
|
|||
| 6 | { |
||
| 7 | |||
| 8 | |||
| 9 | /** |
||
| 10 | * see _config folder for details ... |
||
| 11 | * @var array |
||
| 12 | */ |
||
| 13 | private static $record_defaults = []; |
||
| 14 | |||
| 15 | |||
| 16 | ####################### |
||
| 17 | ### Names Section |
||
| 18 | ####################### |
||
| 19 | |||
| 20 | private static $singular_name = 'Image Style'; |
||
| 21 | |||
| 22 | function i18n_singular_name() |
||
| 26 | |||
| 27 | private static $plural_name = 'Image Styles'; |
||
| 28 | |||
| 29 | function i18n_plural_name() |
||
| 33 | |||
| 34 | |||
| 35 | ####################### |
||
| 36 | ### Model Section |
||
| 37 | ####################### |
||
| 38 | |||
| 39 | private static $db = [ |
||
| 40 | 'Title' => 'Varchar', |
||
| 41 | 'ClassNameForCSS' => 'Varchar', |
||
| 42 | 'Description' => 'Text', |
||
| 43 | 'Var1Name' => 'Varchar', |
||
| 44 | 'Var1Type' => 'Enum(\'Pixels,Percentage,Options\', \'Pixels\')', |
||
| 45 | 'Var1Options' => 'Varchar(200)', |
||
| 46 | 'Var1Description' => 'Varchar(200)', |
||
| 47 | |||
| 48 | 'Var2Name' => 'Varchar', |
||
| 49 | 'Var2Type' => 'Enum(\'Pixels,Percentage,Options\', \'Pixels\')', |
||
| 50 | 'Var2Options' => 'Varchar(200)', |
||
| 51 | 'Var2Description' => 'Varchar(200)', |
||
| 52 | |||
| 53 | 'Var3Name' => 'Varchar', |
||
| 54 | 'Var3Type' => 'Enum(\'Pixels,Percentage,Options\', \'Pixels\')', |
||
| 55 | 'Var3Options' => 'Varchar(200)', |
||
| 56 | 'Var3Description' => 'Varchar(200)', |
||
| 57 | |||
| 58 | 'Var4Name' => 'Varchar', |
||
| 59 | 'Var4Type' => 'Enum(\'Pixels,Percentage,Options\', \'Pixels\')', |
||
| 60 | 'Var4Options' => 'Varchar(200)', |
||
| 61 | 'Var4Description' => 'Varchar(200)', |
||
| 62 | |||
| 63 | 'Var5Name' => 'Varchar', |
||
| 64 | 'Var5Type' => 'Enum(\'Pixels,Percentage,Options\', \'Pixels\')', |
||
| 65 | 'Var5Options' => 'Varchar(200)', |
||
| 66 | 'Var5Description' => 'Varchar(200)', |
||
| 67 | |||
| 68 | ]; |
||
| 69 | |||
| 70 | private static $has_many = [ |
||
| 71 | 'ImagesWithStyle' => 'ImageWithStyle' |
||
| 72 | ]; |
||
| 73 | |||
| 74 | |||
| 75 | ####################### |
||
| 76 | ### Further DB Field Details |
||
| 77 | ####################### |
||
| 78 | |||
| 79 | private static $indexes = [ |
||
| 80 | 'Title' => true |
||
| 81 | ]; |
||
| 82 | |||
| 83 | private static $default_sort = [ |
||
| 84 | 'Title' => 'ASC' |
||
| 85 | ]; |
||
| 86 | |||
| 87 | private static $required_fields = [ |
||
| 88 | 'Title', |
||
| 89 | 'ClassNameForCSS' |
||
| 90 | ]; |
||
| 91 | |||
| 92 | private static $searchable_fields = [ |
||
| 93 | 'Title' => 'PartialMatchFilter', |
||
| 94 | 'Description' => 'PartialMatchFilter', |
||
| 95 | 'ClassNameForCSS' => 'PartialMatchFilter' |
||
| 96 | ]; |
||
| 97 | |||
| 98 | |||
| 99 | ####################### |
||
| 100 | ### Field Names and Presentation Section |
||
| 101 | ####################### |
||
| 102 | |||
| 103 | private static $field_labels = [ |
||
| 104 | 'Title' => 'Style', |
||
| 105 | |||
| 106 | 'Var1Name' => 'Variable 1 Label', |
||
| 107 | 'Var1Type' => 'Variable 1 Type', |
||
| 108 | 'Var1Options' => 'Variable 1 Options', |
||
| 109 | 'Var1Description' => 'Variable 1 Description', |
||
| 110 | |||
| 111 | 'Var2Name' => 'Variable 2 Label', |
||
| 112 | 'Var2Type' => 'Variable 2 Type', |
||
| 113 | 'Var2Options' => 'Variable 1 Options', |
||
| 114 | 'Var2Description' => 'Variable 1 Description', |
||
| 115 | |||
| 116 | 'Var3Name' => 'Variable 3 Label', |
||
| 117 | 'Var3Type' => 'Variable 3 Type', |
||
| 118 | 'Var3Options' => 'Variable 1 Options', |
||
| 119 | 'Var4Description' => 'Variable 1 Description', |
||
| 120 | |||
| 121 | 'Var4Name' => 'Variable 4 Label', |
||
| 122 | 'Var4Type' => 'Variable 4 Type', |
||
| 123 | 'Var4Options' => 'Variable 4 Options', |
||
| 124 | 'Var4Description' => 'Variable 4 Description', |
||
| 125 | |||
| 126 | 'Var5Name' => 'Variable 5 Label', |
||
| 127 | 'Var5Type' => 'Variable 5 Type', |
||
| 128 | 'Var5Options' => 'Variable 5 Options', |
||
| 129 | 'Var5Description' => 'Variable 5 Description', |
||
| 130 | ]; |
||
| 131 | |||
| 132 | private static $field_labels_right = []; |
||
| 133 | |||
| 134 | private static $summary_fields = [ |
||
| 135 | 'Title' => 'Style', |
||
| 136 | 'ImagesWithStyle.Count' => 'Usage Count' |
||
| 137 | ]; |
||
| 138 | |||
| 139 | |||
| 140 | ####################### |
||
| 141 | ### Casting Section |
||
| 142 | ####################### |
||
| 143 | |||
| 144 | |||
| 145 | ####################### |
||
| 146 | ### can Section |
||
| 147 | ####################### |
||
| 148 | |||
| 149 | function canCreate($member = null) |
||
| 153 | |||
| 154 | function canEdit($member = null) |
||
| 159 | |||
| 160 | function canDelete($member = null) |
||
| 169 | |||
| 170 | |||
| 171 | |||
| 172 | ####################### |
||
| 173 | ### write Section |
||
| 174 | ####################### |
||
| 175 | |||
| 176 | |||
| 177 | |||
| 178 | |||
| 179 | View Code Duplication | public function validate() |
|
| 224 | |||
| 225 | public function onBeforeWrite() |
||
| 234 | |||
| 235 | public function onAfterWrite() |
||
| 240 | |||
| 241 | public function requireDefaultRecords() |
||
| 279 | |||
| 280 | |||
| 281 | ####################### |
||
| 282 | ### Import / Export Section |
||
| 283 | ####################### |
||
| 284 | |||
| 285 | public function getExportFields() |
||
| 290 | |||
| 291 | |||
| 292 | |||
| 293 | ####################### |
||
| 294 | ### CMS Edit Section |
||
| 295 | ####################### |
||
| 296 | |||
| 297 | |||
| 298 | public function CMSEditLink() |
||
| 304 | |||
| 305 | public function CMSAddLink() |
||
| 311 | |||
| 312 | |||
| 313 | public function getCMSFields() |
||
| 363 | |||
| 364 | |||
| 365 | public function HasStyleVariable($varName) |
||
| 384 | |||
| 385 | public function OptionsAsArray($varName) : array |
||
| 398 | |||
| 399 | |||
| 400 | } |
||
| 401 |
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.