Complex classes like Server 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 Server, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Server |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Source file system. |
||
| 17 | * @var FilesystemInterface |
||
| 18 | */ |
||
| 19 | protected $source; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Source path prefix. |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $sourcePathPrefix; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Cache file system. |
||
| 29 | * @var FilesystemInterface |
||
| 30 | */ |
||
| 31 | protected $cache; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Cache path prefix. |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $cachePathPrefix; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Whether to group cache in folders. |
||
| 41 | * @var bool |
||
| 42 | */ |
||
| 43 | protected $groupCacheInFolders = true; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Image manipulation API. |
||
| 47 | * @var ApiInterface |
||
| 48 | */ |
||
| 49 | protected $api; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Response factory. |
||
| 53 | * @var ResponseFactoryInterface|null |
||
| 54 | */ |
||
| 55 | protected $responseFactory; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Base URL. |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $baseUrl; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Default image manipulations. |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $defaults = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Preset image manipulations. |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $presets = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Temp image |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $tmp; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Create Server instance. |
||
| 83 | * @param FilesystemInterface $source Source file system. |
||
| 84 | * @param FilesystemInterface $cache Cache file system. |
||
| 85 | * @param ApiInterface $api Image manipulation API. |
||
| 86 | */ |
||
| 87 | 144 | public function __construct(FilesystemInterface $source, FilesystemInterface $cache, ApiInterface $api) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Delete temp image |
||
| 96 | */ |
||
| 97 | 6 | public function __destruct() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Set source file system. |
||
| 104 | * @param FilesystemInterface $source Source file system. |
||
| 105 | */ |
||
| 106 | 144 | public function setSource(FilesystemInterface $source) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Get source file system. |
||
| 113 | * @return FilesystemInterface Source file system. |
||
| 114 | */ |
||
| 115 | 6 | public function getSource() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Set source path prefix. |
||
| 122 | * @param string $sourcePathPrefix Source path prefix. |
||
| 123 | */ |
||
| 124 | 15 | public function setSourcePathPrefix($sourcePathPrefix) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Get source path prefix. |
||
| 131 | * @return string Source path prefix. |
||
| 132 | */ |
||
| 133 | 6 | public function getSourcePathPrefix() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Get source path. |
||
| 140 | * @param string $path Image path. |
||
| 141 | * @return string The source path. |
||
| 142 | * @throws FileNotFoundException |
||
| 143 | */ |
||
| 144 | 66 | public function getSourcePath($path) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Check if a source file exists. |
||
| 165 | * @param string $path Image path. |
||
| 166 | * @return bool Whether the source file exists. |
||
| 167 | */ |
||
| 168 | 18 | public function sourceFileExists($path) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Set base URL. |
||
| 175 | * @param string $baseUrl Base URL. |
||
| 176 | */ |
||
| 177 | 12 | public function setBaseUrl($baseUrl) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Get base URL. |
||
| 184 | * @return string Base URL. |
||
| 185 | */ |
||
| 186 | 6 | public function getBaseUrl() |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Set cache file system. |
||
| 193 | * @param FilesystemInterface $cache Cache file system. |
||
| 194 | */ |
||
| 195 | 144 | public function setCache(FilesystemInterface $cache) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Get cache file system. |
||
| 202 | * @return FilesystemInterface Cache file system. |
||
| 203 | */ |
||
| 204 | 6 | public function getCache() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Set cache path prefix. |
||
| 211 | * @param string $cachePathPrefix Cache path prefix. |
||
| 212 | */ |
||
| 213 | 12 | public function setCachePathPrefix($cachePathPrefix) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Get cache path prefix. |
||
| 220 | * @return string Cache path prefix. |
||
| 221 | */ |
||
| 222 | 6 | public function getCachePathPrefix() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Set the group cache in folders setting. |
||
| 229 | * @param bool $groupCacheInFolders Whether to group cache in folders. |
||
| 230 | */ |
||
| 231 | 15 | public function setGroupCacheInFolders($groupCacheInFolders) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Get the group cache in folders setting. |
||
| 238 | * @return bool Whether to group cache in folders. |
||
| 239 | */ |
||
| 240 | 6 | public function getGroupCacheInFolders() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Get cache path. |
||
| 247 | * @param string $path Image path. |
||
| 248 | * @param array $params Image manipulation params. |
||
| 249 | * @return string Cache path. |
||
| 250 | */ |
||
| 251 | 48 | public function getCachePath($path, array $params = []) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Check if a cache file exists. |
||
| 276 | * @param string $path Image path. |
||
| 277 | * @param array $params Image manipulation params. |
||
| 278 | * @return bool Whether the cache file exists. |
||
| 279 | */ |
||
| 280 | 33 | public function cacheFileExists($path, array $params) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Delete cached manipulations for an image. |
||
| 289 | * @param string $path Image path. |
||
| 290 | * @return bool Whether the delete succeeded. |
||
| 291 | */ |
||
| 292 | 6 | public function deleteCache($path) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Set image manipulation API. |
||
| 307 | * @param ApiInterface $api Image manipulation API. |
||
| 308 | */ |
||
| 309 | 144 | public function setApi(ApiInterface $api) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Get image manipulation API. |
||
| 316 | * @return ApiInterface Image manipulation API. |
||
| 317 | */ |
||
| 318 | 6 | public function getApi() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Set default image manipulations. |
||
| 325 | * @param array $defaults Default image manipulations. |
||
| 326 | */ |
||
| 327 | 15 | public function setDefaults(array $defaults) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Get default image manipulations. |
||
| 334 | * @return array Default image manipulations. |
||
| 335 | */ |
||
| 336 | 6 | public function getDefaults() |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Set preset image manipulations. |
||
| 343 | * @param array $presets Preset image manipulations. |
||
| 344 | */ |
||
| 345 | 15 | public function setPresets(array $presets) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Get preset image manipulations. |
||
| 352 | * @return array Preset image manipulations. |
||
| 353 | */ |
||
| 354 | 6 | public function getPresets() |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Get all image manipulations params, including defaults and presets. |
||
| 361 | * @param array $params Image manipulation params. |
||
| 362 | * @return array All image manipulation params. |
||
| 363 | */ |
||
| 364 | 51 | public function getAllParams(array $params) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Set response factory. |
||
| 381 | * @param ResponseFactoryInterface|null $responseFactory Response factory. |
||
| 382 | */ |
||
| 383 | 15 | public function setResponseFactory(ResponseFactoryInterface $responseFactory = null) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Get response factory. |
||
| 390 | * @return ResponseFactoryInterface Response factory. |
||
| 391 | */ |
||
| 392 | 6 | public function getResponseFactory() |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Generate and return image response. |
||
| 399 | * @param string $path Image path. |
||
| 400 | * @param array $params Image manipulation params. |
||
| 401 | * @return mixed Image response. |
||
| 402 | * @throws InvalidArgumentException |
||
| 403 | */ |
||
| 404 | 6 | public function getImageResponse($path, array $params) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Generate and return Base64 encoded image. |
||
| 419 | * @param string $path Image path. |
||
| 420 | * @param array $params Image manipulation params. |
||
| 421 | * @return string Base64 encoded image. |
||
| 422 | * @throws FilesystemException |
||
| 423 | */ |
||
| 424 | 6 | public function getImageAsBase64($path, array $params) |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Generate and output image. |
||
| 441 | * @param string $path Image path. |
||
| 442 | * @param array $params Image manipulation params. |
||
| 443 | * @throws InvalidArgumentException |
||
| 444 | */ |
||
| 445 | 3 | public function outputImage($path, array $params) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Generate manipulated image. |
||
| 463 | * @param string $path Image path. |
||
| 464 | * @param array $params Image manipulation params. |
||
| 465 | * @return string Cache path. |
||
| 466 | * @throws FileNotFoundException |
||
| 467 | * @throws FilesystemException |
||
| 468 | */ |
||
| 469 | 30 | public function makeImage($path, array $params) |
|
| 526 | } |
||
| 527 |
If you suppress an error, we recommend checking for the error condition explicitly: