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 | * @var string $name name of Image Field template |
||
| 56 | * @return string (link) |
||
| 57 | */ |
||
| 58 | public function PerfectCMSAbsoluteImageLink($name) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * |
||
| 66 | * @param string $name |
||
| 67 | * @return string (HTML) |
||
| 68 | */ |
||
| 69 | public function PerfectCMSImageTag($name) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $name |
||
| 95 | * @param object (optional) $backupObject |
||
| 96 | * @param string (optional) $backupField |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | public function PerfectCMSImageLink( |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $name |
||
| 228 | * |
||
| 229 | * @return boolean |
||
| 230 | */ |
||
| 231 | public static function image_info_available($name) |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * @param string $name |
||
| 241 | * |
||
| 242 | * @return boolean |
||
| 243 | */ |
||
| 244 | public static function use_retina($name) |
||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $name |
||
| 252 | * |
||
| 253 | * @return boolean |
||
| 254 | */ |
||
| 255 | public static function crop($name) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $name |
||
| 262 | * @param bool $forceInteger |
||
| 263 | * |
||
| 264 | * @return int |
||
| 265 | */ |
||
| 266 | View Code Duplication | public static function get_width($name, $forceInteger = false) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @param string $name |
||
| 278 | * @param bool $forceInteger |
||
| 279 | * |
||
| 280 | * @return int |
||
| 281 | */ |
||
| 282 | View Code Duplication | public static function get_height($name, $forceInteger) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $name |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public static function get_folder($name) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param string $name |
||
| 304 | * |
||
| 305 | * @return int |
||
| 306 | */ |
||
| 307 | public static function max_size_in_kilobytes($name) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param string $name |
||
| 314 | * |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | public static function get_file_type($name) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param string $name |
||
| 324 | * |
||
| 325 | * @return boolean |
||
| 326 | */ |
||
| 327 | public static function get_enforce_size($name) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $name |
||
| 334 | * |
||
| 335 | * @return boolean |
||
| 336 | */ |
||
| 337 | public static function get_padding_bg_colour($name) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param string $name |
||
| 348 | * @param int $key |
||
| 349 | * @param mixed $default |
||
| 350 | * |
||
| 351 | * @return mixed |
||
| 352 | */ |
||
| 353 | private static function get_one_value_for_image($name, $key, $default = '') |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | private static function get_all_values_for_images() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * replace the last instance of a string occurence. |
||
| 378 | * |
||
| 379 | * @param string $search needle |
||
| 380 | * @param string $replace new needle |
||
| 381 | * @param string $subject haystack |
||
| 382 | * |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | private function replaceLastInstance($search, $replace, $subject) |
||
| 395 | } |
||
| 396 |
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.