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 CImageResizer 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 CImageResizer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class CImageResizer |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Log function. |
||
| 16 | */ |
||
| 17 | private $log; |
||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | /** |
||
| 22 | * Source image dimensions, calculated from loaded image. |
||
| 23 | */ |
||
| 24 | private $srcWidth; |
||
| 25 | private $srcHeight; |
||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | /** |
||
| 30 | * Set as expected target image/canvas dimensions. |
||
| 31 | */ |
||
| 32 | private $targetWidth; |
||
| 33 | private $targetHeight; |
||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * Where should the image go on the canvas. |
||
| 39 | */ |
||
| 40 | private $destinationX; |
||
| 41 | private $destinationY; |
||
| 42 | private $destinationWidth; |
||
| 43 | private $destinationHeight; |
||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * Which parts to crop from the source. |
||
| 49 | */ |
||
| 50 | private $cropX; |
||
| 51 | private $cropY; |
||
| 52 | private $cropWidth; |
||
| 53 | private $cropHeight; |
||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Change target height & width when different dpr, dpr 2 means double |
||
| 59 | * image dimensions. |
||
| 60 | */ |
||
| 61 | private $dpr = null; |
||
| 62 | |||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * Set aspect ratio for the target image. |
||
| 67 | */ |
||
| 68 | private $aspectRatio; |
||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * Array with details on how to crop. |
||
| 74 | * Array contains xxxxxx |
||
| 75 | */ |
||
| 76 | public $crop; |
||
| 77 | public $cropOrig; // Save original value? |
||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * Area to use for target image, crop out parts not in area. |
||
| 83 | * Array with top, right, bottom, left percentage values to crop out. |
||
| 84 | */ |
||
| 85 | private $area; |
||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * Pixel offset in source image to decide which part of image is used. |
||
| 91 | * Array with top, right, bottom, left percentage values to crop out. |
||
| 92 | */ |
||
| 93 | private $offset; |
||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * Resize strategy, image should keep its original ratio. |
||
| 99 | */ |
||
| 100 | const KEEP_RATIO = 1; |
||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * Resize strategy, image should crop and fill area. |
||
| 106 | */ |
||
| 107 | const CROP_TO_FIT = 2; |
||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * Resize strategy, image should fit in area and fill remains. |
||
| 113 | */ |
||
| 114 | const FILL_TO_FIT = 3; |
||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * Resize strategy, image should stretch to fit in area. |
||
| 120 | */ |
||
| 121 | const STRETCH = 4; |
||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * The currently selected resize strategy. |
||
| 127 | */ |
||
| 128 | private $resizeStrategy = self::KEEP_RATIO; |
||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * Allow upscale of smaller images by default, set to false to disallow. |
||
| 134 | */ |
||
| 135 | private $upscale = true; |
||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * Constructor, set log function to use for verbose logging or null |
||
| 141 | * to disable logging. |
||
| 142 | 113 | * |
|
| 143 | * @param callable $log function to call for logging. |
||
| 144 | 113 | */ |
|
| 145 | 113 | public function __construct($log = null) |
|
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * Log string using logger. |
||
| 154 | 104 | * |
|
| 155 | * @param string $str to log. |
||
| 156 | 104 | */ |
|
| 157 | 1 | public function log($str) |
|
| 163 | |||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * Set source dimensions. |
||
| 168 | * |
||
| 169 | * @param integer $width of source image. |
||
| 170 | * @param integer $height of source image. |
||
| 171 | * |
||
| 172 | * @throws Exception |
||
| 173 | 94 | * |
|
| 174 | * @return $this |
||
| 175 | 94 | */ |
|
| 176 | 94 | public function setSource($width, $height) |
|
| 184 | |||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * Get resize strategy as string. |
||
| 189 | 70 | * |
|
| 190 | * @return string |
||
| 191 | 70 | */ |
|
| 192 | 70 | public function getResizeStrategyAsString() |
|
| 215 | |||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * Set resize strategy as KEEP_RATIO, CROP_TO_FIT or FILL_TO_FIT. |
||
| 220 | * |
||
| 221 | * @param integer $strategy |
||
| 222 | 70 | * |
|
| 223 | * @return $this |
||
| 224 | 70 | */ |
|
| 225 | 70 | public function setResizeStrategy($strategy) |
|
| 232 | |||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * Allow or disallow upscale smaller images. |
||
| 237 | * |
||
| 238 | * @param boolean $upscale |
||
| 239 | 22 | * |
|
| 240 | * @return $this |
||
| 241 | 22 | */ |
|
| 242 | 22 | public function allowUpscale($upscale) |
|
| 249 | |||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * Check if a value is upscaled or not by compare $current to $orig, |
||
| 254 | * if $current is larger than $orig then check if upscaled is allowed. |
||
| 255 | * |
||
| 256 | * @param float &$current value to check and change. |
||
| 257 | * @param float $orig value to check against. |
||
| 258 | 33 | * |
|
| 259 | * @return boolean true if upscaled changed values, else false. |
||
| 260 | 33 | */ |
|
| 261 | 6 | public function respectUpscale(&$current, $orig) |
|
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * Set base for requested width and height. |
||
| 276 | * |
||
| 277 | * @param numeric|null $width as requested target width |
||
| 278 | 98 | * @param numeric|null $height as requested target height |
|
| 279 | * |
||
| 280 | 98 | * @throws Exception |
|
| 281 | * |
||
| 282 | 98 | * @return $this |
|
| 283 | 98 | */ |
|
| 284 | public function setBaseWidthHeight($width = null, $height = null) |
||
| 315 | |||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * Set base for requested aspect ratio. |
||
| 320 | * |
||
| 321 | 15 | * @param float|null $aspectRatio as requested aspect ratio |
|
| 322 | * |
||
| 323 | 15 | * @throws Exception |
|
| 324 | * |
||
| 325 | 15 | * @return $this |
|
| 326 | */ |
||
| 327 | 15 | View Code Duplication | public function setBaseAspecRatio($aspectRatio = null) |
| 341 | |||
| 342 | |||
| 343 | |||
| 344 | /** |
||
| 345 | * Set base for requested device pixel ratio. |
||
| 346 | * |
||
| 347 | 14 | * @param float $dpr as requested density pixel rate |
|
| 348 | * |
||
| 349 | 14 | * @throws Exception |
|
| 350 | * |
||
| 351 | 14 | * @return $this |
|
| 352 | */ |
||
| 353 | 14 | View Code Duplication | public function setBaseDevicePixelRate($dpr = null) |
| 367 | |||
| 368 | |||
| 369 | |||
| 370 | /** |
||
| 371 | * Calculate target width and height by considering the selected |
||
| 372 | 26 | * aspect ratio. |
|
| 373 | * |
||
| 374 | 26 | * @throws Exception |
|
| 375 | * |
||
| 376 | 26 | * @return $this |
|
| 377 | 14 | */ |
|
| 378 | public function prepareByConsiderAspectRatio() |
||
| 420 | 12 | ||
| 421 | |||
| 422 | |||
| 423 | /** |
||
| 424 | * Calculate target width and height by considering the selected |
||
| 425 | * dpr. |
||
| 426 | * |
||
| 427 | * @throws Exception |
||
| 428 | * |
||
| 429 | * @return $this |
||
| 430 | */ |
||
| 431 | public function prepareByConsiderDpr() |
||
| 457 | 12 | ||
| 458 | |||
| 459 | |||
| 460 | /** |
||
| 461 | * Calculate target width and height and do sanity checks on constraints. |
||
| 462 | * After this method the $targetWidth and $targetHeight will have |
||
| 463 | * the expected dimensions on the target image. |
||
| 464 | * |
||
| 465 | * @throws Exception |
||
| 466 | * |
||
| 467 | * @return $this |
||
| 468 | */ |
||
| 469 | public function prepareTargetDimensions() |
||
| 480 | 26 | ||
| 481 | |||
| 482 | |||
| 483 | /** |
||
| 484 | * Calculate new width and height of image. |
||
| 485 | * |
||
| 486 | * @return $this |
||
| 487 | */ |
||
| 488 | public function calculateTargetWidthAndHeight() |
||
| 694 | |||
| 695 | |||
| 696 | |||
| 697 | 1 | /** |
|
| 698 | * Get source width. |
||
| 699 | 1 | * |
|
| 700 | * @return integer as source width |
||
| 701 | */ |
||
| 702 | public function getSourceWidth() |
||
| 706 | |||
| 707 | |||
| 708 | |||
| 709 | 93 | /** |
|
| 710 | * Get source height. |
||
| 711 | 93 | * |
|
| 712 | * @return integer as source height |
||
| 713 | */ |
||
| 714 | public function getSourceHeight() |
||
| 718 | |||
| 719 | |||
| 720 | |||
| 721 | 93 | /** |
|
| 722 | * Get target width. |
||
| 723 | 93 | * |
|
| 724 | * @return integer as target width |
||
| 725 | */ |
||
| 726 | public function getTargetWidth() |
||
| 730 | |||
| 731 | |||
| 732 | |||
| 733 | 16 | /** |
|
| 734 | * Get target height. |
||
| 735 | 16 | * |
|
| 736 | * @return integer as target height |
||
| 737 | */ |
||
| 738 | public function getTargetHeight() |
||
| 742 | |||
| 743 | |||
| 744 | |||
| 745 | 16 | /** |
|
| 746 | * Get destination x. |
||
| 747 | 16 | * |
|
| 748 | * @return integer as destination x |
||
| 749 | */ |
||
| 750 | public function getDestinationX() |
||
| 754 | |||
| 755 | |||
| 756 | |||
| 757 | 16 | /** |
|
| 758 | * Get destination y. |
||
| 759 | 16 | * |
|
| 760 | * @return integer as destination y |
||
| 761 | */ |
||
| 762 | public function getDestinationY() |
||
| 766 | |||
| 767 | |||
| 768 | |||
| 769 | 16 | /** |
|
| 770 | * Get destination width. |
||
| 771 | 16 | * |
|
| 772 | * @return integer as destination width |
||
| 773 | */ |
||
| 774 | public function getDestinationWidth() |
||
| 778 | |||
| 779 | |||
| 780 | |||
| 781 | 42 | /** |
|
| 782 | * Get destination height. |
||
| 783 | 42 | * |
|
| 784 | * @return integer as destination height |
||
| 785 | */ |
||
| 786 | public function getDestinationHeight() |
||
| 790 | |||
| 791 | |||
| 792 | |||
| 793 | 42 | /** |
|
| 794 | * Get crop position x. |
||
| 795 | 42 | * |
|
| 796 | * @return integer |
||
| 797 | */ |
||
| 798 | public function getCropX() |
||
| 802 | |||
| 803 | |||
| 804 | |||
| 805 | 42 | /** |
|
| 806 | * Get crop position y. |
||
| 807 | 42 | * |
|
| 808 | * @return integer |
||
| 809 | */ |
||
| 810 | public function getCropY() |
||
| 814 | |||
| 815 | |||
| 816 | |||
| 817 | 42 | /** |
|
| 818 | * Get crop width. |
||
| 819 | 42 | * |
|
| 820 | * @return integer |
||
| 821 | */ |
||
| 822 | public function getCropWidth() |
||
| 826 | |||
| 827 | |||
| 828 | |||
| 829 | /** |
||
| 830 | * Get crop height. |
||
| 831 | * |
||
| 832 | * @return integer |
||
| 833 | */ |
||
| 834 | public function getCropHeight() |
||
| 838 | } |
||
| 839 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.