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 Imagick 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 Imagick, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Imagick extends AbstractLib implements LibInterface |
||
| 13 | { |
||
| 14 | protected $image; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * {@inheritdoc} |
||
| 18 | */ |
||
| 19 | public static function checkCompatibility() |
||
| 20 | { |
||
| 21 | return extension_loaded('imagick') && !empty(BaseImagick::queryformats()); |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * {@inheritdoc} |
||
| 26 | * |
||
| 27 | * @return Imagick |
||
| 28 | */ |
||
| 29 | public static function createFromFile($filename) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | * |
||
| 43 | * @return Imagick |
||
| 44 | */ |
||
| 45 | public static function createFromString($string) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Constructor of the class. |
||
| 56 | * |
||
| 57 | * @param BaseImagick $image The Imagick instance |
||
| 58 | */ |
||
| 59 | public function __construct(BaseImagick $image) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Destroy the image. |
||
| 80 | */ |
||
| 81 | public function __destruct() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * {@inheritdoc} |
||
| 88 | */ |
||
| 89 | public function flip() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | public function flop() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | public function save($filename) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Gets the original image object. |
||
| 128 | * |
||
| 129 | * @return object |
||
| 130 | */ |
||
| 131 | public function getImage() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * {@inheritdoc} |
||
| 138 | */ |
||
| 139 | public function getString() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * {@inheritdoc} |
||
| 164 | */ |
||
| 165 | public function getMimeType() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | public function getWidth() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * {@inheritdoc} |
||
| 188 | */ |
||
| 189 | public function getHeight() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * {@inheritdoc} |
||
| 200 | */ |
||
| 201 | public function format($format) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * {@inheritdoc} |
||
| 217 | */ |
||
| 218 | public function resize($width, $height) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritdoc} |
||
| 239 | */ |
||
| 240 | public function getCropOffsets($width, $height, $method) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * {@inheritdoc} |
||
| 253 | */ |
||
| 254 | public function crop($width, $height, $x, $y) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * {@inheritdoc} |
||
| 276 | */ |
||
| 277 | public function rotate($angle) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * {@inheritdoc} |
||
| 288 | */ |
||
| 289 | public function blur($loops) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Returns a copy of the image compressed and ready to save or print. |
||
| 307 | * |
||
| 308 | * @return BaseImagick The instance of the image |
||
| 309 | */ |
||
| 310 | private function getCompressed() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * {@inheritdoc} |
||
| 352 | */ |
||
| 353 | public function watermark(LibInterface $image, $x, $y) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * {@inheritdoc} |
||
| 364 | */ |
||
| 365 | public function opacity($opacity) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * {@inheritdoc} |
||
| 381 | */ |
||
| 382 | public function setProgressive($progressive) |
||
| 390 | } |
||
| 391 |