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 $path |
||
| 138 | * @param string $position |
||
| 139 | * @param integer $opacity |
||
| 140 | * |
||
| 141 | * @return Imagine |
||
| 142 | */ |
||
| 143 | public function watermark($path, $position = 'center', $opacity = null) |
||
| 162 | |||
| 163 | protected function watermarkCanvas(Imagick $image, Imagick $watermark, $position = 'center') |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Is Image |
||
| 232 | * |
||
| 233 | * @param string $path |
||
| 234 | * |
||
| 235 | * @return boolean |
||
| 236 | */ |
||
| 237 | protected function isImage($path) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Crop |
||
| 244 | * |
||
| 245 | * @param integer $width |
||
| 246 | * @param integer $height |
||
| 247 | * @param integer $x |
||
| 248 | * @param integer $y |
||
| 249 | * |
||
| 250 | * @return binary |
||
| 251 | */ |
||
| 252 | public function crop($width, $height, $x, $y) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Rotate Image |
||
| 261 | * |
||
| 262 | * @param integer $angle |
||
| 263 | * |
||
| 264 | * @return binary |
||
| 265 | */ |
||
| 266 | public function rotate($angle) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Strip Profiles |
||
| 281 | * |
||
| 282 | * @param string $except |
||
| 283 | * |
||
| 284 | * @return this |
||
| 285 | */ |
||
| 286 | public function stripProfiles() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Encode |
||
| 300 | * |
||
| 301 | * @param string $format |
||
| 302 | * @param integer $quality |
||
| 303 | * |
||
| 304 | * @return binary |
||
| 305 | */ |
||
| 306 | public function encode($format = null, $quality = null) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Save |
||
| 313 | * |
||
| 314 | * @param string $path |
||
| 315 | * @param integer $quality |
||
| 316 | * |
||
| 317 | * @return binary |
||
| 318 | */ |
||
| 319 | public function save($path, $quality = null) |
||
| 329 | } |
||
| 330 |
If you suppress an error, we recommend checking for the error condition explicitly: