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 PerfectCMSImageDataExtension 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 PerfectCMSImageDataExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class PerfectCMSImageDataExtension extends DataExtension |
||
|
|||
8 | { |
||
9 | /** |
||
10 | * background image for padded images... |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | private static $perfect_cms_images_background_padding_color = '#cccccc'; |
||
15 | |||
16 | /*** |
||
17 | * sizes of the images |
||
18 | * width: 3200 |
||
19 | * height: 3200 |
||
20 | * folder: "myfolder" |
||
21 | * filetype: "try jpg" |
||
22 | * |
||
23 | * @var array |
||
24 | * |
||
25 | */ |
||
26 | private static $perfect_cms_images_image_definitions = array(); |
||
27 | |||
28 | /*** |
||
29 | * Images Titles will be appended to the links only |
||
30 | * if the ClassName of the Image is in this array |
||
31 | * @var array |
||
32 | * |
||
33 | */ |
||
34 | private static $perfect_cms_images_append_title_to_image_links_classes = array(); |
||
35 | |||
36 | /** |
||
37 | * @var string $name name of Image Field template |
||
38 | * @return string (link) |
||
39 | */ |
||
40 | public function PerfectCMSImageLinkNonRetina($name) |
||
44 | |||
45 | /** |
||
46 | * @var string $name name of Image Field template |
||
47 | * @return string (link) |
||
48 | */ |
||
49 | public function PerfectCMSImageLinkRetina($name) |
||
53 | |||
54 | /** |
||
55 | * |
||
56 | * @param string $name |
||
57 | * @return string (HTML) |
||
58 | */ |
||
59 | public function PerfectCMSImageTag($name) |
||
82 | |||
83 | /** |
||
84 | * @param string $name |
||
85 | * @param object (optional) $backupObject |
||
86 | * @param string (optional) $backupField |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | public function PerfectCMSImageLink( |
||
197 | |||
198 | /** |
||
199 | * @param string $name |
||
200 | * |
||
201 | * @return boolean |
||
202 | */ |
||
203 | public static function image_info_available($name) |
||
210 | |||
211 | |||
212 | /** |
||
213 | * @param string $name |
||
214 | * |
||
215 | * @return boolean |
||
216 | */ |
||
217 | public static function use_retina($name) |
||
221 | |||
222 | /** |
||
223 | * @param string $name |
||
224 | * @param bool $forceInteger |
||
225 | * |
||
226 | * @return int |
||
227 | */ |
||
228 | View Code Duplication | public static function get_width($name, $forceInteger = false) |
|
238 | |||
239 | /** |
||
240 | * @param string $name |
||
241 | * @param bool $forceInteger |
||
242 | * |
||
243 | * @return int |
||
244 | */ |
||
245 | View Code Duplication | public static function get_height($name, $forceInteger) |
|
255 | |||
256 | /** |
||
257 | * @param string $name |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | public static function get_folder($name) |
||
265 | |||
266 | /** |
||
267 | * @param string $name |
||
268 | * |
||
269 | * @return int |
||
270 | */ |
||
271 | public static function max_size_in_kilobytes($name) |
||
275 | |||
276 | /** |
||
277 | * @param string $name |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | public static function get_file_type($name) |
||
285 | |||
286 | /** |
||
287 | * @param string $name |
||
288 | * |
||
289 | * @return boolean |
||
290 | */ |
||
291 | public static function get_enforce_size($name) |
||
295 | |||
296 | /** |
||
297 | * @param string $name |
||
298 | * @param int $key |
||
299 | * @param mixed $default |
||
300 | * |
||
301 | * @return mixed |
||
302 | */ |
||
303 | private static function get_one_value_for_image($name, $key, $default = '') |
||
317 | |||
318 | /** |
||
319 | * @return array |
||
320 | */ |
||
321 | private static function get_all_values_for_images() |
||
325 | |||
326 | /** |
||
327 | * replace the last instance of a string occurence. |
||
328 | * |
||
329 | * @param string $search needle |
||
330 | * @param string $replace new needle |
||
331 | * @param string $subject haystack |
||
332 | * |
||
333 | * @return string |
||
334 | */ |
||
335 | private function replaceLastInstance($search, $replace, $subject) |
||
346 | |||
347 | } |
||
348 |
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.