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 |
||
| 7 | class ImagineImagick implements Imagine |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var Imagick |
||
| 11 | */ |
||
| 12 | protected $image; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Construct |
||
| 16 | * |
||
| 17 | * @param string $path |
||
| 18 | */ |
||
| 19 | public function __construct($path) |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Execute in Canvas |
||
| 26 | * |
||
| 27 | * @param \Closure $callback |
||
| 28 | * |
||
| 29 | * @return Imagine |
||
| 30 | */ |
||
| 31 | protected function execute($callback) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Filesize |
||
| 51 | * |
||
| 52 | * @return int |
||
| 53 | */ |
||
| 54 | public function filesize() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Define Resize |
||
| 61 | * |
||
| 62 | * @param int $maxWidth |
||
| 63 | * @param int $maxHeight |
||
| 64 | * @param boolean $force |
||
| 65 | * |
||
| 66 | * @return Imagine |
||
| 67 | */ |
||
| 68 | public function resize($maxWidth, $maxHeight, $force = false) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Opacity |
||
| 95 | * |
||
| 96 | * @return Imagine |
||
| 97 | */ |
||
| 98 | public function opacity($opacity) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Watermark |
||
| 113 | * |
||
| 114 | * @param string $path |
||
| 115 | * @param string $position |
||
| 116 | * @param integer $opacity |
||
| 117 | * |
||
| 118 | * @return Imagine |
||
| 119 | */ |
||
| 120 | public function watermark($path, $position = 'center', $opacity = null) |
||
| 139 | |||
| 140 | protected function watermarkCanvas(Imagick $image, Imagick $watermark, $position = 'center') |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Is Image |
||
| 209 | * |
||
| 210 | * @param string $path |
||
| 211 | * |
||
| 212 | * @return boolean |
||
| 213 | */ |
||
| 214 | protected function isImage($path) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Crop |
||
| 221 | * |
||
| 222 | * @param integer $width |
||
| 223 | * @param integer $height |
||
| 224 | * @param integer $x |
||
| 225 | * @param integer $y |
||
| 226 | * |
||
| 227 | * @return binary |
||
| 228 | */ |
||
| 229 | public function crop($width, $height, $x, $y) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Encode |
||
| 238 | * |
||
| 239 | * @param string $format |
||
| 240 | * @param integer $quality |
||
| 241 | * |
||
| 242 | * @return binary |
||
| 243 | */ |
||
| 244 | public function encode($format = null, $quality = null) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Save |
||
| 251 | * |
||
| 252 | * @param string $path |
||
| 253 | * @param integer $quality |
||
| 254 | * |
||
| 255 | * @return binary |
||
| 256 | */ |
||
| 257 | public function save($path, $quality = null) |
||
| 267 | } |
||
| 268 |
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.