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 | * Whether to cache with file extensions. |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | protected $cacheWithFileExtensions = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Image manipulation API. |
||
| 53 | * @var ApiInterface |
||
| 54 | */ |
||
| 55 | protected $api; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Response factory. |
||
| 59 | * @var ResponseFactoryInterface|null |
||
| 60 | */ |
||
| 61 | protected $responseFactory; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Base URL. |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $baseUrl; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Default image manipulations. |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $defaults = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Preset image manipulations. |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $presets = []; |
||
| 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 | 171 | public function __construct(FilesystemInterface $source, FilesystemInterface $cache, ApiInterface $api) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Set source file system. |
||
| 96 | * @param FilesystemInterface $source Source file system. |
||
| 97 | */ |
||
| 98 | 171 | public function setSource(FilesystemInterface $source) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Get source file system. |
||
| 105 | * @return FilesystemInterface Source file system. |
||
| 106 | */ |
||
| 107 | 6 | public function getSource() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Set source path prefix. |
||
| 114 | * @param string $sourcePathPrefix Source path prefix. |
||
| 115 | */ |
||
| 116 | 15 | public function setSourcePathPrefix($sourcePathPrefix) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Get source path prefix. |
||
| 123 | * @return string Source path prefix. |
||
| 124 | */ |
||
| 125 | 6 | public function getSourcePathPrefix() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Get source path. |
||
| 132 | * @param string $path Image path. |
||
| 133 | * @return string The source path. |
||
| 134 | * @throws FileNotFoundException |
||
| 135 | */ |
||
| 136 | 87 | public function getSourcePath($path) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Check if a source file exists. |
||
| 159 | * @param string $path Image path. |
||
| 160 | * @return bool Whether the source file exists. |
||
| 161 | */ |
||
| 162 | 18 | public function sourceFileExists($path) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Set base URL. |
||
| 169 | * @param string $baseUrl Base URL. |
||
| 170 | */ |
||
| 171 | 12 | public function setBaseUrl($baseUrl) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Get base URL. |
||
| 178 | * @return string Base URL. |
||
| 179 | */ |
||
| 180 | 6 | public function getBaseUrl() |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Set cache file system. |
||
| 187 | * @param FilesystemInterface $cache Cache file system. |
||
| 188 | */ |
||
| 189 | 171 | public function setCache(FilesystemInterface $cache) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Get cache file system. |
||
| 196 | * @return FilesystemInterface Cache file system. |
||
| 197 | */ |
||
| 198 | 6 | public function getCache() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Set cache path prefix. |
||
| 205 | * @param string $cachePathPrefix Cache path prefix. |
||
| 206 | */ |
||
| 207 | 12 | public function setCachePathPrefix($cachePathPrefix) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Get cache path prefix. |
||
| 214 | * @return string Cache path prefix. |
||
| 215 | */ |
||
| 216 | 6 | public function getCachePathPrefix() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Set the group cache in folders setting. |
||
| 223 | * @param bool $groupCacheInFolders Whether to group cache in folders. |
||
| 224 | */ |
||
| 225 | 15 | public function setGroupCacheInFolders($groupCacheInFolders) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Get the group cache in folders setting. |
||
| 232 | * @return bool Whether to group cache in folders. |
||
| 233 | */ |
||
| 234 | 6 | public function getGroupCacheInFolders() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Set the cache with file extensions setting. |
||
| 241 | * @param bool $cacheWithFileExtensions Whether to cache with file extensions. |
||
| 242 | */ |
||
| 243 | 30 | public function setCacheWithFileExtensions($cacheWithFileExtensions) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Get the cache with file extensions setting. |
||
| 250 | * @return bool Whether to cache with file extensions. |
||
| 251 | */ |
||
| 252 | 6 | public function getCacheWithFileExtensions() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Get cache path. |
||
| 259 | * @param string $path Image path. |
||
| 260 | * @param array $params Image manipulation params. |
||
| 261 | * @return string Cache path. |
||
| 262 | */ |
||
| 263 | 69 | public function getCachePath($path, array $params = []) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Check if a cache file exists. |
||
| 294 | * @param string $path Image path. |
||
| 295 | * @param array $params Image manipulation params. |
||
| 296 | * @return bool Whether the cache file exists. |
||
| 297 | */ |
||
| 298 | 33 | public function cacheFileExists($path, array $params) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Delete cached manipulations for an image. |
||
| 307 | * @param string $path Image path. |
||
| 308 | * @return bool Whether the delete succeeded. |
||
| 309 | */ |
||
| 310 | 6 | public function deleteCache($path) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Set image manipulation API. |
||
| 325 | * @param ApiInterface $api Image manipulation API. |
||
| 326 | */ |
||
| 327 | 171 | public function setApi(ApiInterface $api) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Get image manipulation API. |
||
| 334 | * @return ApiInterface Image manipulation API. |
||
| 335 | */ |
||
| 336 | 6 | public function getApi() |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Set default image manipulations. |
||
| 343 | * @param array $defaults Default image manipulations. |
||
| 344 | */ |
||
| 345 | 21 | public function setDefaults(array $defaults) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Get default image manipulations. |
||
| 352 | * @return array Default image manipulations. |
||
| 353 | */ |
||
| 354 | 6 | public function getDefaults() |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Set preset image manipulations. |
||
| 361 | * @param array $presets Preset image manipulations. |
||
| 362 | */ |
||
| 363 | 21 | public function setPresets(array $presets) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Get preset image manipulations. |
||
| 370 | * @return array Preset image manipulations. |
||
| 371 | */ |
||
| 372 | 6 | public function getPresets() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Get all image manipulations params, including defaults and presets. |
||
| 379 | * @param array $params Image manipulation params. |
||
| 380 | * @return array All image manipulation params. |
||
| 381 | */ |
||
| 382 | 72 | public function getAllParams(array $params) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Set response factory. |
||
| 399 | * @param ResponseFactoryInterface|null $responseFactory Response factory. |
||
| 400 | */ |
||
| 401 | 15 | public function setResponseFactory(ResponseFactoryInterface $responseFactory = null) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Get response factory. |
||
| 408 | * @return ResponseFactoryInterface Response factory. |
||
| 409 | */ |
||
| 410 | 6 | public function getResponseFactory() |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Generate and return image response. |
||
| 417 | * @param string $path Image path. |
||
| 418 | * @param array $params Image manipulation params. |
||
| 419 | * @return mixed Image response. |
||
| 420 | * @throws InvalidArgumentException |
||
| 421 | */ |
||
| 422 | 6 | public function getImageResponse($path, array $params) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Generate and return Base64 encoded image. |
||
| 437 | * @param string $path Image path. |
||
| 438 | * @param array $params Image manipulation params. |
||
| 439 | * @return string Base64 encoded image. |
||
| 440 | * @throws FilesystemException |
||
| 441 | */ |
||
| 442 | 6 | public function getImageAsBase64($path, array $params) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Generate and output image. |
||
| 459 | * @param string $path Image path. |
||
| 460 | * @param array $params Image manipulation params. |
||
| 461 | * @throws InvalidArgumentException |
||
| 462 | */ |
||
| 463 | 3 | public function outputImage($path, array $params) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Generate manipulated image. |
||
| 483 | * @param string $path Image path. |
||
| 484 | * @param array $params Image manipulation params. |
||
| 485 | * @return string Cache path. |
||
| 486 | * @throws FileNotFoundException |
||
| 487 | * @throws FilesystemException |
||
| 488 | */ |
||
| 489 | 30 | public function makeImage($path, array $params) |
|
| 546 | } |
||
| 547 |