Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Manager |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var array Contains configurations and presets |
||
| 20 | */ |
||
| 21 | protected $options; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var MethodCaller |
||
| 25 | */ |
||
| 26 | protected $methodCaller; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $retinaRegex = '/(.*)@2x\\.(jpe?g|png|webp|gif)/'; |
||
| 32 | |||
| 33 | 153 | public function __construct($options) |
|
| 37 | |||
| 38 | /** |
||
| 39 | * @return MethodCaller |
||
| 40 | */ |
||
| 41 | 54 | public function getMethodCaller() |
|
| 49 | |||
| 50 | /** |
||
| 51 | * @param MethodCaller $methodCaller |
||
| 52 | */ |
||
| 53 | 3 | public function setMethodCaller(MethodCaller $methodCaller) |
|
| 57 | |||
| 58 | 15 | public function url($preset, $file) |
|
| 62 | |||
| 63 | 21 | public function imageUrl($file) |
|
| 67 | |||
| 68 | 36 | public function isRetina($file) |
|
| 72 | |||
| 73 | 39 | public function getOriginalFilename($file) { |
|
| 78 | |||
| 79 | 36 | protected function getPresetActions($preset_key, $file) |
|
| 106 | |||
| 107 | 27 | protected function generateRetinaAction($action) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Take a preset and a file and return a transformed image |
||
| 120 | * |
||
| 121 | * @param $preset_key string |
||
| 122 | * @param $file string |
||
| 123 | * @throws Exceptions\InvalidPresetException |
||
| 124 | * @throws Exceptions\NotFoundException |
||
| 125 | * @throws \RuntimeException |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | 42 | public function handleRequest($preset_key, $file) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Create the folder containing the cached images if it doesn't exist |
||
| 157 | * |
||
| 158 | * @param $base |
||
| 159 | * @param $cacheDir |
||
| 160 | */ |
||
| 161 | 15 | protected function verifyDirectoryExistence($base, $cacheDir) |
|
| 176 | |||
| 177 | 15 | protected function loadImage($src) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Create a new image based on an image preset. |
||
| 184 | * |
||
| 185 | * @param array $actions An image preset array. |
||
| 186 | * @param Image $image Path of the source file. |
||
| 187 | * @param string $dst Path of the destination file. |
||
| 188 | * @throws \RuntimeException |
||
| 189 | * @return Image |
||
| 190 | */ |
||
| 191 | 78 | protected function buildImage($actions, Image $image, $dst) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Accept a percentage and return it in pixels. |
||
| 220 | * |
||
| 221 | * @param string $value |
||
| 222 | * @param int $current_pixels |
||
| 223 | * @return mixed |
||
| 224 | */ |
||
| 225 | 51 | public function percent($value, $current_pixels) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Accept a keyword (center, top, left, etc) and return it as an offset in pixels. |
||
| 236 | * |
||
| 237 | * @param $value |
||
| 238 | * @param $current_pixels |
||
| 239 | * @param $new_pixels |
||
| 240 | * @return float|int |
||
| 241 | */ |
||
| 242 | 27 | public function keywords($value, $current_pixels, $new_pixels) |
|
| 260 | } |
||
| 261 |