Complex classes like ImagineImagick 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 ImagineImagick, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 8 | class ImagineImagick implements Imagine | ||
| 9 | { | ||
| 10 | /** | ||
| 11 | * @var Imagick | ||
| 12 | */ | ||
| 13 | protected $image; | ||
| 14 | |||
| 15 | /** | ||
| 16 | * Construct | ||
| 17 | * | ||
| 18 | * @param string|blob $data | ||
| 19 | */ | ||
| 20 | public function __construct($data) | ||
| 31 | |||
| 32 | /** | ||
| 33 | * Determines if current source data is binary data | ||
| 34 | * | ||
| 35 | * @return boolean | ||
| 36 | */ | ||
| 37 | protected function isBinary($data) | ||
| 46 | |||
| 47 | /** | ||
| 48 | * Execute in Canvas | ||
| 49 | * | ||
| 50 | * @param \Closure $callback | ||
| 51 | * | ||
| 52 | * @return Imagine | ||
| 53 | */ | ||
| 54 | protected function execute($callback) | ||
| 71 | |||
| 72 | /** | ||
| 73 | * Filesize | ||
| 74 | * | ||
| 75 | * @return int | ||
| 76 | */ | ||
| 77 | public function filesize() | ||
| 81 | |||
| 82 | /** | ||
| 83 | * Define Resize | ||
| 84 | * | ||
| 85 | * @param int $maxWidth | ||
| 86 | * @param int $maxHeight | ||
| 87 | * @param boolean $force | ||
| 88 | * | ||
| 89 | * @return Imagine | ||
| 90 | */ | ||
| 91 | public function resize($maxWidth, $maxHeight, $force = false) | ||
| 115 | |||
| 116 | /** | ||
| 117 | * Opacity | ||
| 118 | * | ||
| 119 | * @return Imagine | ||
| 120 | */ | ||
| 121 | public function opacity($opacity) | ||
| 133 | |||
| 134 | /** | ||
| 135 | * Watermark | ||
| 136 | * | ||
| 137 | * @param string $watermark | ||
| 138 | * @param string $position | ||
| 139 | * @param integer $opacity | ||
| 140 | * | ||
| 141 | * @return Imagine | ||
| 142 | */ | ||
| 143 | public function watermark($watermark, $position = 'center', $opacity = null) | ||
| 164 | |||
| 165 | protected function watermarkCanvas(Imagick $image, Imagick $watermark, $position = 'center') | ||
| 231 | |||
| 232 | /** | ||
| 233 | * Is Image | ||
| 234 | * | ||
| 235 | * @param string $path | ||
| 236 | * | ||
| 237 | * @return boolean | ||
| 238 | */ | ||
| 239 | protected function isImage($path) | ||
| 243 | |||
| 244 | /** | ||
| 245 | * Crop | ||
| 246 | * | ||
| 247 | * @param integer $width | ||
| 248 | * @param integer $height | ||
| 249 | * @param integer $x | ||
| 250 | * @param integer $y | ||
| 251 | * | ||
| 252 | * @return binary | ||
| 253 | */ | ||
| 254 | public function crop($width, $height, $x, $y) | ||
| 260 | |||
| 261 | /** | ||
| 262 | * Rotate Image | ||
| 263 | * | ||
| 264 | * @param integer $angle | ||
| 265 | * | ||
| 266 | * @return binary | ||
| 267 | */ | ||
| 268 | public function rotate($angle) | ||
| 280 | |||
| 281 | /** | ||
| 282 | * Strip Profiles | ||
| 283 | * | ||
| 284 | * @param string $except | ||
| 285 | * | ||
| 286 | * @return this | ||
| 287 | */ | ||
| 288 | public function stripProfiles() | ||
| 299 | |||
| 300 | /** | ||
| 301 | * Encode | ||
| 302 | * | ||
| 303 | * @param string $format | ||
| 304 | * @param integer $quality | ||
| 305 | * | ||
| 306 | * @return binary | ||
| 307 | */ | ||
| 308 | public function encode($format = null, $quality = null) | ||
| 312 | |||
| 313 | /** | ||
| 314 | * Save | ||
| 315 | * | ||
| 316 | * @param string $path | ||
| 317 | * @param integer $quality | ||
| 318 | * | ||
| 319 | * @return binary | ||
| 320 | */ | ||
| 321 | public function save($path, $quality = null) | ||
| 331 | } | ||
| 332 | 
If you suppress an error, we recommend checking for the error condition explicitly: