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 | private static $default_style = 'unstyled-image'; |
||
| 8 | |||
| 9 | public static function get_default_style() |
||
| 25 | |||
| 26 | /** |
||
| 27 | * see _config folder for details ... |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private static $record_defaults = []; |
||
| 31 | |||
| 32 | |||
| 33 | ####################### |
||
| 34 | ### Names Section |
||
| 35 | ####################### |
||
| 36 | |||
| 37 | private static $singular_name = 'Image Style'; |
||
| 38 | |||
| 39 | public function i18n_singular_name() |
||
| 43 | |||
| 44 | private static $plural_name = 'Image Styles'; |
||
| 45 | |||
| 46 | public function i18n_plural_name() |
||
| 50 | |||
| 51 | |||
| 52 | ####################### |
||
| 53 | ### Model Section |
||
| 54 | ####################### |
||
| 55 | |||
| 56 | private static $db = [ |
||
| 57 | 'Title' => 'Varchar', |
||
| 58 | 'ClassNameForCSS' => 'Varchar', |
||
| 59 | 'Description' => 'Text', |
||
| 60 | 'Var1Name' => 'Varchar', |
||
| 61 | 'Var1Type' => 'Enum(\'Pixels,Percentage,Options\', \'Pixels\')', |
||
| 62 | 'Var1Options' => 'Varchar(200)', |
||
| 63 | 'Var1Description' => 'Varchar(200)', |
||
| 64 | |||
| 65 | 'Var2Name' => 'Varchar', |
||
| 66 | 'Var2Type' => 'Enum(\'Pixels,Percentage,Options\', \'Pixels\')', |
||
| 67 | 'Var2Options' => 'Varchar(200)', |
||
| 68 | 'Var2Description' => 'Varchar(200)', |
||
| 69 | |||
| 70 | 'Var3Name' => 'Varchar', |
||
| 71 | 'Var3Type' => 'Enum(\'Pixels,Percentage,Options\', \'Pixels\')', |
||
| 72 | 'Var3Options' => 'Varchar(200)', |
||
| 73 | 'Var3Description' => 'Varchar(200)', |
||
| 74 | |||
| 75 | 'Var4Name' => 'Varchar', |
||
| 76 | 'Var4Type' => 'Enum(\'Pixels,Percentage,Options\', \'Pixels\')', |
||
| 77 | 'Var4Options' => 'Varchar(200)', |
||
| 78 | 'Var4Description' => 'Varchar(200)', |
||
| 79 | |||
| 80 | 'Var5Name' => 'Varchar', |
||
| 81 | 'Var5Type' => 'Enum(\'Pixels,Percentage,Options\', \'Pixels\')', |
||
| 82 | 'Var5Options' => 'Varchar(200)', |
||
| 83 | 'Var5Description' => 'Varchar(200)', |
||
| 84 | |||
| 85 | ]; |
||
| 86 | |||
| 87 | private static $has_many = [ |
||
| 88 | 'ImagesWithStyle' => 'ImageWithStyle' |
||
| 89 | ]; |
||
| 90 | |||
| 91 | |||
| 92 | ####################### |
||
| 93 | ### Further DB Field Details |
||
| 94 | ####################### |
||
| 95 | |||
| 96 | private static $indexes = [ |
||
| 97 | 'Title' => true |
||
| 98 | ]; |
||
| 99 | |||
| 100 | private static $default_sort = [ |
||
| 101 | 'Title' => 'ASC' |
||
| 102 | ]; |
||
| 103 | |||
| 104 | private static $required_fields = [ |
||
| 105 | 'Title', |
||
| 106 | 'ClassNameForCSS' |
||
| 107 | ]; |
||
| 108 | |||
| 109 | private static $searchable_fields = [ |
||
| 110 | 'Title' => 'PartialMatchFilter', |
||
| 111 | 'Description' => 'PartialMatchFilter', |
||
| 112 | 'ClassNameForCSS' => 'PartialMatchFilter' |
||
| 113 | ]; |
||
| 114 | |||
| 115 | |||
| 116 | ####################### |
||
| 117 | ### Field Names and Presentation Section |
||
| 118 | ####################### |
||
| 119 | |||
| 120 | private static $field_labels = [ |
||
| 121 | 'Title' => 'Style', |
||
| 122 | |||
| 123 | 'Var1Name' => 'Variable 1 Label', |
||
| 124 | 'Var1Type' => 'Variable 1 Type', |
||
| 125 | 'Var1Options' => 'Variable 1 Options', |
||
| 126 | 'Var1Description' => 'Variable 1 Description', |
||
| 127 | |||
| 128 | 'Var2Name' => 'Variable 2 Label', |
||
| 129 | 'Var2Type' => 'Variable 2 Type', |
||
| 130 | 'Var2Options' => 'Variable 1 Options', |
||
| 131 | 'Var2Description' => 'Variable 1 Description', |
||
| 132 | |||
| 133 | 'Var3Name' => 'Variable 3 Label', |
||
| 134 | 'Var3Type' => 'Variable 3 Type', |
||
| 135 | 'Var3Options' => 'Variable 1 Options', |
||
| 136 | 'Var4Description' => 'Variable 1 Description', |
||
| 137 | |||
| 138 | 'Var4Name' => 'Variable 4 Label', |
||
| 139 | 'Var4Type' => 'Variable 4 Type', |
||
| 140 | 'Var4Options' => 'Variable 4 Options', |
||
| 141 | 'Var4Description' => 'Variable 4 Description', |
||
| 142 | |||
| 143 | 'Var5Name' => 'Variable 5 Label', |
||
| 144 | 'Var5Type' => 'Variable 5 Type', |
||
| 145 | 'Var5Options' => 'Variable 5 Options', |
||
| 146 | 'Var5Description' => 'Variable 5 Description', |
||
| 147 | ]; |
||
| 148 | |||
| 149 | private static $field_labels_right = []; |
||
| 150 | |||
| 151 | private static $summary_fields = [ |
||
| 152 | 'Title' => 'Style', |
||
| 153 | 'ImagesWithStyle.Count' => 'Usage Count' |
||
| 154 | ]; |
||
| 155 | |||
| 156 | |||
| 157 | ####################### |
||
| 158 | ### Casting Section |
||
| 159 | ####################### |
||
| 160 | |||
| 161 | |||
| 162 | ####################### |
||
| 163 | ### can Section |
||
| 164 | ####################### |
||
| 165 | |||
| 166 | public function canCreate($member = null) |
||
| 170 | |||
| 171 | public function canEdit($member = null) |
||
| 176 | |||
| 177 | public function canDelete($member = null) |
||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | ####################### |
||
| 189 | ### write Section |
||
| 190 | ####################### |
||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | View Code Duplication | public function validate() |
|
| 240 | |||
| 241 | public function onBeforeWrite() |
||
| 250 | |||
| 251 | public function onAfterWrite() |
||
| 256 | |||
| 257 | public function requireDefaultRecords() |
||
| 294 | |||
| 295 | |||
| 296 | ####################### |
||
| 297 | ### Import / Export Section |
||
| 298 | ####################### |
||
| 299 | |||
| 300 | public function getExportFields() |
||
| 305 | |||
| 306 | |||
| 307 | |||
| 308 | ####################### |
||
| 309 | ### CMS Edit Section |
||
| 310 | ####################### |
||
| 311 | |||
| 312 | |||
| 313 | public function CMSEditLink() |
||
| 319 | |||
| 320 | public function CMSAddLink() |
||
| 326 | |||
| 327 | |||
| 328 | public function getCMSFields() |
||
| 378 | |||
| 379 | |||
| 380 | public function HasStyleVariable($varName) |
||
| 397 | |||
| 398 | public function OptionsAsArray($varName) |
||
| 411 | } |
||
| 412 |
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.