Complex classes like ImagickBackend 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 ImagickBackend, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class ImagickBackend extends Imagick implements Image_Backend |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * @config |
||
21 | * @var int |
||
22 | */ |
||
23 | private static $default_quality = 75; |
||
24 | |||
25 | /** |
||
26 | * Create a new backend with the given object |
||
27 | * |
||
28 | * @param AssetContainer $assetContainer Object to load from |
||
29 | */ |
||
30 | public function __construct(AssetContainer $assetContainer = null) |
||
38 | |||
39 | public function loadFromContainer(AssetContainer $assetContainer) |
||
46 | |||
47 | public function loadFrom($path) |
||
52 | |||
53 | protected function setDefaultQuality() |
||
57 | |||
58 | public function writeToStore(AssetStore $assetStore, $filename, $hash = null, $variant = null, $config = array()) |
||
70 | |||
71 | public function writeTo($path) |
||
78 | |||
79 | public function setQuality($quality) |
||
83 | |||
84 | public function resize($width, $height) |
||
119 | |||
120 | public function resizeRatio($maxWidth, $maxHeight, $useAsMinimum = false) |
||
141 | |||
142 | public function resizeByWidth($width) |
||
153 | |||
154 | public function resizeByHeight($height) |
||
165 | |||
166 | /** |
||
167 | * paddedResize |
||
168 | * |
||
169 | * @param int $width |
||
170 | * @param int $height |
||
171 | * @param string $backgroundColor |
||
172 | * @param int $transparencyPercent |
||
173 | * @return Image_Backend |
||
174 | */ |
||
175 | public function paddedResize($width, $height, $backgroundColor = "FFFFFF", $transparencyPercent = 0) |
||
196 | |||
197 | /** |
||
198 | * Convert a percentage (or 'true') to a two char hex code to signifiy the level of an alpha channel |
||
199 | * |
||
200 | * @param $percent |
||
201 | * @return string |
||
202 | */ |
||
203 | public function calculateAlphaHex($percent) |
||
215 | |||
216 | |||
217 | /** |
||
218 | * croppedResize |
||
219 | * |
||
220 | * @param int $width |
||
221 | * @param int $height |
||
222 | * @return Image_Backend |
||
223 | */ |
||
224 | public function croppedResize($width, $height) |
||
260 | |||
261 | /** |
||
262 | * Crop's part of image. |
||
263 | * @param int $top y position of left upper corner of crop rectangle |
||
264 | * @param int $left x position of left upper corner of crop rectangle |
||
265 | * @param int $width rectangle width |
||
266 | * @param int $height rectangle height |
||
267 | * @return Image_Backend |
||
268 | */ |
||
269 | public function crop($top, $left, $width, $height) |
||
276 | } |
||
277 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.