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 | * Custom cache path callable. |
||
| 82 | * @var callable|null |
||
| 83 | */ |
||
| 84 | protected $cachePathCallable; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Create Server instance. |
||
| 88 | * @param FilesystemInterface $source Source file system. |
||
| 89 | * @param FilesystemInterface $cache Cache file system. |
||
| 90 | * @param ApiInterface $api Image manipulation API. |
||
| 91 | */ |
||
| 92 | public function __construct(FilesystemInterface $source, FilesystemInterface $cache, ApiInterface $api) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Set source file system. |
||
| 101 | * @param FilesystemInterface $source Source file system. |
||
| 102 | */ |
||
| 103 | public function setSource(FilesystemInterface $source) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get source file system. |
||
| 110 | * @return FilesystemInterface Source file system. |
||
| 111 | */ |
||
| 112 | public function getSource() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Set source path prefix. |
||
| 119 | * @param string $sourcePathPrefix Source path prefix. |
||
| 120 | */ |
||
| 121 | public function setSourcePathPrefix($sourcePathPrefix) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get source path prefix. |
||
| 128 | * @return string Source path prefix. |
||
| 129 | */ |
||
| 130 | public function getSourcePathPrefix() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get source path. |
||
| 137 | * @param string $path Image path. |
||
| 138 | * @return string The source path. |
||
| 139 | * @throws FileNotFoundException |
||
| 140 | */ |
||
| 141 | public function getSourcePath($path) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Check if a source file exists. |
||
| 164 | * @param string $path Image path. |
||
| 165 | * @return bool Whether the source file exists. |
||
| 166 | */ |
||
| 167 | public function sourceFileExists($path) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Set base URL. |
||
| 174 | * @param string $baseUrl Base URL. |
||
| 175 | */ |
||
| 176 | public function setBaseUrl($baseUrl) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get base URL. |
||
| 183 | * @return string Base URL. |
||
| 184 | */ |
||
| 185 | public function getBaseUrl() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Set cache file system. |
||
| 192 | * @param FilesystemInterface $cache Cache file system. |
||
| 193 | */ |
||
| 194 | public function setCache(FilesystemInterface $cache) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get cache file system. |
||
| 201 | * @return FilesystemInterface Cache file system. |
||
| 202 | */ |
||
| 203 | public function getCache() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Set cache path prefix. |
||
| 210 | * @param string $cachePathPrefix Cache path prefix. |
||
| 211 | */ |
||
| 212 | public function setCachePathPrefix($cachePathPrefix) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Get cache path prefix. |
||
| 219 | * @return string Cache path prefix. |
||
| 220 | */ |
||
| 221 | public function getCachePathPrefix() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Set the group cache in folders setting. |
||
| 228 | * @param bool $groupCacheInFolders Whether to group cache in folders. |
||
| 229 | */ |
||
| 230 | public function setGroupCacheInFolders($groupCacheInFolders) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get the group cache in folders setting. |
||
| 237 | * @return bool Whether to group cache in folders. |
||
| 238 | */ |
||
| 239 | public function getGroupCacheInFolders() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Set the cache with file extensions setting. |
||
| 246 | * @param bool $cacheWithFileExtensions Whether to cache with file extensions. |
||
| 247 | */ |
||
| 248 | public function setCacheWithFileExtensions($cacheWithFileExtensions) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the cache with file extensions setting. |
||
| 255 | * @return bool Whether to cache with file extensions. |
||
| 256 | */ |
||
| 257 | public function getCacheWithFileExtensions() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Set a custom cachePathCallable |
||
| 264 | * @param callable|null $cachePathCallable The custom cache path callable. It receives the same arguments as @see getCachePath |
||
| 265 | */ |
||
| 266 | public function setCachePathCallable($cachePathCallable) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Gets the custom cachePathCallable |
||
| 273 | * @return callable|null The custom cache path callable. It receives the same arguments as @see getCachePath |
||
| 274 | */ |
||
| 275 | public function getCachePathCallable() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get cache path. |
||
| 282 | * @param string $path Image path. |
||
| 283 | * @param array $params Image manipulation params. |
||
| 284 | * @return string Cache path. |
||
| 285 | */ |
||
| 286 | public function getCachePath($path, array $params = []) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Check if a cache file exists. |
||
| 322 | * @param string $path Image path. |
||
| 323 | * @param array $params Image manipulation params. |
||
| 324 | * @return bool Whether the cache file exists. |
||
| 325 | */ |
||
| 326 | public function cacheFileExists($path, array $params) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Delete cached manipulations for an image. |
||
| 335 | * @param string $path Image path. |
||
| 336 | * @return bool Whether the delete succeeded. |
||
| 337 | */ |
||
| 338 | public function deleteCache($path) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Set image manipulation API. |
||
| 353 | * @param ApiInterface $api Image manipulation API. |
||
| 354 | */ |
||
| 355 | public function setApi(ApiInterface $api) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Get image manipulation API. |
||
| 362 | * @return ApiInterface Image manipulation API. |
||
| 363 | */ |
||
| 364 | public function getApi() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Set default image manipulations. |
||
| 371 | * @param array $defaults Default image manipulations. |
||
| 372 | */ |
||
| 373 | public function setDefaults(array $defaults) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Get default image manipulations. |
||
| 380 | * @return array Default image manipulations. |
||
| 381 | */ |
||
| 382 | public function getDefaults() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Set preset image manipulations. |
||
| 389 | * @param array $presets Preset image manipulations. |
||
| 390 | */ |
||
| 391 | public function setPresets(array $presets) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Get preset image manipulations. |
||
| 398 | * @return array Preset image manipulations. |
||
| 399 | */ |
||
| 400 | public function getPresets() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Get all image manipulations params, including defaults and presets. |
||
| 407 | * @param array $params Image manipulation params. |
||
| 408 | * @return array All image manipulation params. |
||
| 409 | */ |
||
| 410 | public function getAllParams(array $params) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Set response factory. |
||
| 427 | * @param ResponseFactoryInterface|null $responseFactory Response factory. |
||
| 428 | */ |
||
| 429 | public function setResponseFactory(ResponseFactoryInterface $responseFactory = null) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Get response factory. |
||
| 436 | * @return ResponseFactoryInterface Response factory. |
||
| 437 | */ |
||
| 438 | public function getResponseFactory() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Generate and return image response. |
||
| 445 | * @param string $path Image path. |
||
| 446 | * @param array $params Image manipulation params. |
||
| 447 | * @return mixed Image response. |
||
| 448 | * @throws InvalidArgumentException |
||
| 449 | */ |
||
| 450 | public function getImageResponse($path, array $params) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Generate and return Base64 encoded image. |
||
| 465 | * @param string $path Image path. |
||
| 466 | * @param array $params Image manipulation params. |
||
| 467 | * @return string Base64 encoded image. |
||
| 468 | * @throws FilesystemException |
||
| 469 | */ |
||
| 470 | public function getImageAsBase64($path, array $params) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Generate and output image. |
||
| 487 | * @param string $path Image path. |
||
| 488 | * @param array $params Image manipulation params. |
||
| 489 | * @throws InvalidArgumentException |
||
| 490 | */ |
||
| 491 | public function outputImage($path, array $params) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Generate manipulated image. |
||
| 511 | * @param string $path Image path. |
||
| 512 | * @param array $params Image manipulation params. |
||
| 513 | * @return string Cache path. |
||
| 514 | * @throws FileNotFoundException |
||
| 515 | * @throws FilesystemException |
||
| 516 | */ |
||
| 517 | public function makeImage($path, array $params) |
||
| 574 | } |
||
| 575 | |||
| 576 |