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 $path |
||
| 19 | */ |
||
| 20 | public function __construct($path) |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Execute in Canvas |
||
| 27 | * |
||
| 28 | * @param \Closure $callback |
||
| 29 | * |
||
| 30 | * @return Imagine |
||
| 31 | */ |
||
| 32 | protected function execute($callback) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Filesize |
||
| 52 | * |
||
| 53 | * @return int |
||
| 54 | */ |
||
| 55 | public function filesize() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Define Resize |
||
| 62 | * |
||
| 63 | * @param int $maxWidth |
||
| 64 | * @param int $maxHeight |
||
| 65 | * @param boolean $force |
||
| 66 | * |
||
| 67 | * @return Imagine |
||
| 68 | */ |
||
| 69 | public function resize($maxWidth, $maxHeight, $force = false) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Opacity |
||
| 96 | * |
||
| 97 | * @return Imagine |
||
| 98 | */ |
||
| 99 | public function opacity($opacity) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Watermark |
||
| 114 | * |
||
| 115 | * @param string $path |
||
| 116 | * @param string $position |
||
| 117 | * @param integer $opacity |
||
| 118 | * |
||
| 119 | * @return Imagine |
||
| 120 | */ |
||
| 121 | public function watermark($path, $position = 'center', $opacity = null) |
||
| 140 | |||
| 141 | protected function watermarkCanvas(Imagick $image, Imagick $watermark, $position = 'center') |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Is Image |
||
| 210 | * |
||
| 211 | * @param string $path |
||
| 212 | * |
||
| 213 | * @return boolean |
||
| 214 | */ |
||
| 215 | protected function isImage($path) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Crop |
||
| 222 | * |
||
| 223 | * @param integer $width |
||
| 224 | * @param integer $height |
||
| 225 | * @param integer $x |
||
| 226 | * @param integer $y |
||
| 227 | * |
||
| 228 | * @return binary |
||
| 229 | */ |
||
| 230 | public function crop($width, $height, $x, $y) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Rotate Image |
||
| 239 | * |
||
| 240 | * @param integer $angle |
||
| 241 | * |
||
| 242 | * @return binary |
||
| 243 | */ |
||
| 244 | public function rotate($angle) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Encode |
||
| 259 | * |
||
| 260 | * @param string $format |
||
| 261 | * @param integer $quality |
||
| 262 | * |
||
| 263 | * @return binary |
||
| 264 | */ |
||
| 265 | public function encode($format = null, $quality = null) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Save |
||
| 272 | * |
||
| 273 | * @param string $path |
||
| 274 | * @param integer $quality |
||
| 275 | * |
||
| 276 | * @return binary |
||
| 277 | */ |
||
| 278 | public function save($path, $quality = null) |
||
| 288 | } |
||
| 289 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.