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