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 FilesystemOperator |
||
| 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 FilesystemOperator |
||
| 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 FilesystemOperator $source Source file system. |
||
| 84 | * @param FilesystemOperator $cache Cache file system. |
||
| 85 | * @param ApiInterface $api Image manipulation API. |
||
| 86 | */ |
||
| 87 | public function __construct(FilesystemOperator $source, FilesystemOperator $cache, ApiInterface $api) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Set source file system. |
||
| 96 | * @param FilesystemOperator $source Source file system. |
||
| 97 | */ |
||
| 98 | public function setSource(FilesystemOperator $source) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get source file system. |
||
| 105 | * @return FilesystemOperator Source file system. |
||
| 106 | */ |
||
| 107 | public function getSource() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set source path prefix. |
||
| 114 | * @param string $sourcePathPrefix Source path prefix. |
||
| 115 | */ |
||
| 116 | public function setSourcePathPrefix($sourcePathPrefix) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get source path prefix. |
||
| 123 | * @return string Source path prefix. |
||
| 124 | */ |
||
| 125 | 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 | 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 | public function sourceFileExists($path) |
||
| 163 | { |
||
| 164 | try { |
||
| 165 | return $this->source->fileExists($this->getSourcePath($path)); |
||
| 166 | } catch (FilesystemV2Exception $exception) { |
||
| 167 | return false; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Set base URL. |
||
| 173 | * @param string $baseUrl Base URL. |
||
| 174 | */ |
||
| 175 | public function setBaseUrl($baseUrl) |
||
| 176 | { |
||
| 177 | $this->baseUrl = trim($baseUrl, '/'); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get base URL. |
||
| 182 | * @return string Base URL. |
||
| 183 | */ |
||
| 184 | public function getBaseUrl() |
||
| 185 | { |
||
| 186 | return $this->baseUrl; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Set cache file system. |
||
| 191 | * @param FilesystemOperator $cache Cache file system. |
||
| 192 | */ |
||
| 193 | public function setCache(FilesystemOperator $cache) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get cache file system. |
||
| 200 | * @return FilesystemOperator Cache file system. |
||
| 201 | */ |
||
| 202 | public function getCache() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Set cache path prefix. |
||
| 209 | * @param string $cachePathPrefix Cache path prefix. |
||
| 210 | */ |
||
| 211 | public function setCachePathPrefix($cachePathPrefix) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get cache path prefix. |
||
| 218 | * @return string Cache path prefix. |
||
| 219 | */ |
||
| 220 | public function getCachePathPrefix() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Set the group cache in folders setting. |
||
| 227 | * @param bool $groupCacheInFolders Whether to group cache in folders. |
||
| 228 | */ |
||
| 229 | public function setGroupCacheInFolders($groupCacheInFolders) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get the group cache in folders setting. |
||
| 236 | * @return bool Whether to group cache in folders. |
||
| 237 | */ |
||
| 238 | public function getGroupCacheInFolders() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Set the cache with file extensions setting. |
||
| 245 | * @param bool $cacheWithFileExtensions Whether to cache with file extensions. |
||
| 246 | */ |
||
| 247 | public function setCacheWithFileExtensions($cacheWithFileExtensions) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get the cache with file extensions setting. |
||
| 254 | * @return bool Whether to cache with file extensions. |
||
| 255 | */ |
||
| 256 | public function getCacheWithFileExtensions() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get cache path. |
||
| 263 | * @param string $path Image path. |
||
| 264 | * @param array $params Image manipulation params. |
||
| 265 | * @return string Cache path. |
||
| 266 | */ |
||
| 267 | public function getCachePath($path, array $params = []) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Check if a cache file exists. |
||
| 298 | * @param string $path Image path. |
||
| 299 | * @param array $params Image manipulation params. |
||
| 300 | * @return bool Whether the cache file exists. |
||
| 301 | */ |
||
| 302 | public function cacheFileExists($path, array $params) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Delete cached manipulations for an image. |
||
| 315 | * @param string $path Image path. |
||
| 316 | * @return bool Whether the delete succeeded. |
||
| 317 | */ |
||
| 318 | public function deleteCache($path) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Set image manipulation API. |
||
| 339 | * @param ApiInterface $api Image manipulation API. |
||
| 340 | */ |
||
| 341 | public function setApi(ApiInterface $api) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Get image manipulation API. |
||
| 348 | * @return ApiInterface Image manipulation API. |
||
| 349 | */ |
||
| 350 | public function getApi() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Set default image manipulations. |
||
| 357 | * @param array $defaults Default image manipulations. |
||
| 358 | */ |
||
| 359 | public function setDefaults(array $defaults) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Get default image manipulations. |
||
| 366 | * @return array Default image manipulations. |
||
| 367 | */ |
||
| 368 | public function getDefaults() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Set preset image manipulations. |
||
| 375 | * @param array $presets Preset image manipulations. |
||
| 376 | */ |
||
| 377 | public function setPresets(array $presets) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Get preset image manipulations. |
||
| 384 | * @return array Preset image manipulations. |
||
| 385 | */ |
||
| 386 | public function getPresets() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Get all image manipulations params, including defaults and presets. |
||
| 393 | * @param array $params Image manipulation params. |
||
| 394 | * @return array All image manipulation params. |
||
| 395 | */ |
||
| 396 | public function getAllParams(array $params) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Set response factory. |
||
| 413 | * @param ResponseFactoryInterface|null $responseFactory Response factory. |
||
| 414 | */ |
||
| 415 | public function setResponseFactory(ResponseFactoryInterface $responseFactory = null) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Get response factory. |
||
| 422 | * @return ResponseFactoryInterface Response factory. |
||
| 423 | */ |
||
| 424 | public function getResponseFactory() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Generate and return image response. |
||
| 431 | * @param string $path Image path. |
||
| 432 | * @param array $params Image manipulation params. |
||
| 433 | * @return mixed Image response. |
||
| 434 | * @throws InvalidArgumentException |
||
| 435 | */ |
||
| 436 | public function getImageResponse($path, array $params) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Generate and return Base64 encoded image. |
||
| 451 | * @param string $path Image path. |
||
| 452 | * @param array $params Image manipulation params. |
||
| 453 | * @return string Base64 encoded image. |
||
| 454 | * @throws FilesystemException |
||
| 455 | */ |
||
| 456 | public function getImageAsBase64($path, array $params) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Generate and output image. |
||
| 473 | * @param string $path Image path. |
||
| 474 | * @param array $params Image manipulation params. |
||
| 475 | * @throws InvalidArgumentException |
||
| 476 | */ |
||
| 477 | public function outputImage($path, array $params) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Generate manipulated image. |
||
| 503 | * @param string $path Image path. |
||
| 504 | * @param array $params Image manipulation params. |
||
| 505 | * @return string Cache path. |
||
| 506 | * @throws FileNotFoundException |
||
| 507 | * @throws FilesystemException |
||
| 508 | */ |
||
| 509 | public function makeImage($path, array $params) |
||
| 560 | } |
||
| 561 |