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 | 12 | public function localUrl($preset, $file) { |
|
| 61 | |||
| 62 | 3 | public function url($preset, $file) |
|
| 66 | |||
| 67 | 3 | public function imageUrl($file) |
|
| 71 | |||
| 72 | 36 | public function isRetina($file) |
|
| 76 | |||
| 77 | 39 | public function getOriginalFilename($file) { |
|
| 82 | |||
| 83 | 36 | protected function getPresetActions($preset_key, $file) |
|
| 110 | |||
| 111 | 27 | protected function generateRetinaAction($action) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Take a preset and a file and return a transformed image |
||
| 124 | * |
||
| 125 | * @param $preset_key string |
||
| 126 | * @param $file string |
||
| 127 | * @throws Exceptions\InvalidPresetException |
||
| 128 | * @throws Exceptions\NotFoundException |
||
| 129 | * @throws \RuntimeException |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | 42 | public function handleRequest($preset_key, $file) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Create the folder containing the cached images if it doesn't exist |
||
| 161 | * |
||
| 162 | * @param $base |
||
| 163 | * @param $cacheDir |
||
| 164 | */ |
||
| 165 | 15 | protected function verifyDirectoryExistence($base, $cacheDir) |
|
| 180 | |||
| 181 | 15 | protected function loadImage($src) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Create a new image based on an image preset. |
||
| 188 | * |
||
| 189 | * @param array $actions An image preset array. |
||
| 190 | * @param Image $image Path of the source file. |
||
| 191 | * @param string $dst Path of the destination file. |
||
| 192 | * @throws \RuntimeException |
||
| 193 | * @return Image |
||
| 194 | */ |
||
| 195 | 78 | protected function buildImage($actions, Image $image, $dst) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Accept a percentage and return it in pixels. |
||
| 224 | * |
||
| 225 | * @param string $value |
||
| 226 | * @param int $current_pixels |
||
| 227 | * @return mixed |
||
| 228 | */ |
||
| 229 | 51 | public function percent($value, $current_pixels) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Accept a keyword (center, top, left, etc) and return it as an offset in pixels. |
||
| 240 | * |
||
| 241 | * @param $value |
||
| 242 | * @param $current_pixels |
||
| 243 | * @param $new_pixels |
||
| 244 | * @return float|int |
||
| 245 | */ |
||
| 246 | 27 | public function keywords($value, $current_pixels, $new_pixels) |
|
| 264 | } |
||
| 265 |