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 | * The path to the original file |
||
| 124 | * @param $source_file |
||
| 125 | * @return string |
||
| 126 | * @throws Exceptions\NotFoundException |
||
| 127 | */ |
||
| 128 | public function getOriginalFile($source_file) |
||
| 137 | 36 | ||
| 138 | /** |
||
| 139 | 36 | * Take a preset and a file and return a transformed image |
|
| 140 | 36 | * |
|
| 141 | 9 | * @param $preset_key string |
|
| 142 | * @param $file string |
||
| 143 | * @throws Exceptions\InvalidPresetException |
||
| 144 | 27 | * @throws Exceptions\NotFoundException |
|
| 145 | * @throws \RuntimeException |
||
| 146 | 27 | * @return string |
|
| 147 | */ |
||
| 148 | 27 | public function handleRequest($preset_key, $file) |
|
| 171 | 15 | ||
| 172 | 15 | /** |
|
| 173 | 15 | * Create the folder containing the cached images if it doesn't exist |
|
| 174 | 15 | * |
|
| 175 | 15 | * @param $base |
|
| 176 | 15 | * @param $cacheDir |
|
| 177 | 10 | */ |
|
| 178 | 10 | protected function verifyDirectoryExistence($base, $cacheDir) |
|
| 193 | |||
| 194 | protected function loadImage($src) |
||
| 198 | |||
| 199 | /** |
||
| 200 | 78 | * Create a new image based on an image preset. |
|
| 201 | 60 | * |
|
| 202 | 40 | * @param array $actions An image preset array. |
|
| 203 | * @param Image $image Path of the source file. |
||
| 204 | 78 | * @param string $dst Path of the destination file. |
|
| 205 | 51 | * @throws \RuntimeException |
|
| 206 | 34 | * @return Image |
|
| 207 | */ |
||
| 208 | 78 | protected function buildImage($actions, Image $image, $dst) |
|
| 234 | |||
| 235 | 51 | /** |
|
| 236 | * Accept a percentage and return it in pixels. |
||
| 237 | * |
||
| 238 | * @param string $value |
||
| 239 | * @param int $current_pixels |
||
| 240 | * @return mixed |
||
| 241 | */ |
||
| 242 | public function percent($value, $current_pixels) |
||
| 250 | 26 | ||
| 251 | 6 | /** |
|
| 252 | 6 | * Accept a keyword (center, top, left, etc) and return it as an offset in pixels. |
|
| 253 | 21 | * |
|
| 254 | 20 | * @param $value |
|
| 255 | 6 | * @param $current_pixels |
|
| 256 | 6 | * @param $new_pixels |
|
| 257 | 15 | * @return float|int |
|
| 258 | 3 | */ |
|
| 259 | 3 | public function keywords($value, $current_pixels, $new_pixels) |
|
| 277 | } |
||
| 278 |