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 | * Path to store temp files. |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $tempDir; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Create Server instance. |
||
| 89 | * @param FilesystemInterface $source Source file system. |
||
| 90 | * @param FilesystemInterface $cache Cache file system. |
||
| 91 | * @param ApiInterface $api Image manipulation API. |
||
| 92 | */ |
||
| 93 | 168 | public function __construct(FilesystemInterface $source, FilesystemInterface $cache, ApiInterface $api) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Set source file system. |
||
| 103 | * @param FilesystemInterface $source Source file system. |
||
| 104 | */ |
||
| 105 | 168 | public function setSource(FilesystemInterface $source) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Get source file system. |
||
| 112 | * @return FilesystemInterface Source file system. |
||
| 113 | */ |
||
| 114 | 6 | public function getSource() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Set source path prefix. |
||
| 121 | * @param string $sourcePathPrefix Source path prefix. |
||
| 122 | */ |
||
| 123 | 15 | public function setSourcePathPrefix($sourcePathPrefix) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Get source path prefix. |
||
| 130 | * @return string Source path prefix. |
||
| 131 | */ |
||
| 132 | 6 | public function getSourcePathPrefix() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Get source path. |
||
| 139 | * @param string $path Image path. |
||
| 140 | * @return string The source path. |
||
| 141 | * @throws FileNotFoundException |
||
| 142 | */ |
||
| 143 | 78 | 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 | 18 | public function sourceFileExists($path) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Set base URL. |
||
| 174 | * @param string $baseUrl Base URL. |
||
| 175 | */ |
||
| 176 | 12 | public function setBaseUrl($baseUrl) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Get base URL. |
||
| 183 | * @return string Base URL. |
||
| 184 | */ |
||
| 185 | 6 | public function getBaseUrl() |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Set cache file system. |
||
| 192 | * @param FilesystemInterface $cache Cache file system. |
||
| 193 | */ |
||
| 194 | 168 | public function setCache(FilesystemInterface $cache) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Get cache file system. |
||
| 201 | * @return FilesystemInterface Cache file system. |
||
| 202 | */ |
||
| 203 | 6 | public function getCache() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Set cache path prefix. |
||
| 210 | * @param string $cachePathPrefix Cache path prefix. |
||
| 211 | */ |
||
| 212 | 12 | public function setCachePathPrefix($cachePathPrefix) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Get cache path prefix. |
||
| 219 | * @return string Cache path prefix. |
||
| 220 | */ |
||
| 221 | 6 | 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 | 15 | 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 | 6 | 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 | 21 | 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 | 6 | public function getCacheWithFileExtensions() |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Get cache path. |
||
| 264 | * @param string $path Image path. |
||
| 265 | * @param array $params Image manipulation params. |
||
| 266 | * @return string Cache path. |
||
| 267 | */ |
||
| 268 | 60 | public function getCachePath($path, array $params = []) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Check if a cache file exists. |
||
| 297 | * @param string $path Image path. |
||
| 298 | * @param array $params Image manipulation params. |
||
| 299 | * @return bool Whether the cache file exists. |
||
| 300 | */ |
||
| 301 | 33 | public function cacheFileExists($path, array $params) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Delete cached manipulations for an image. |
||
| 310 | * @param string $path Image path. |
||
| 311 | * @return bool Whether the delete succeeded. |
||
| 312 | */ |
||
| 313 | 6 | public function deleteCache($path) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Set image manipulation API. |
||
| 328 | * @param ApiInterface $api Image manipulation API. |
||
| 329 | */ |
||
| 330 | 168 | public function setApi(ApiInterface $api) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Get image manipulation API. |
||
| 337 | * @return ApiInterface Image manipulation API. |
||
| 338 | */ |
||
| 339 | 6 | public function getApi() |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Set default image manipulations. |
||
| 346 | * @param array $defaults Default image manipulations. |
||
| 347 | */ |
||
| 348 | 18 | public function setDefaults(array $defaults) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Get default image manipulations. |
||
| 355 | * @return array Default image manipulations. |
||
| 356 | */ |
||
| 357 | 6 | public function getDefaults() |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Set preset image manipulations. |
||
| 364 | * @param array $presets Preset image manipulations. |
||
| 365 | */ |
||
| 366 | 18 | public function setPresets(array $presets) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Get preset image manipulations. |
||
| 373 | * @return array Preset image manipulations. |
||
| 374 | */ |
||
| 375 | 6 | public function getPresets() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Get all image manipulations params, including defaults and presets. |
||
| 382 | * @param array $params Image manipulation params. |
||
| 383 | * @return array All image manipulation params. |
||
| 384 | */ |
||
| 385 | 63 | public function getAllParams(array $params) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Set response factory. |
||
| 402 | * @param ResponseFactoryInterface|null $responseFactory Response factory. |
||
| 403 | */ |
||
| 404 | 15 | public function setResponseFactory(ResponseFactoryInterface $responseFactory = null) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Get response factory. |
||
| 411 | * @return ResponseFactoryInterface Response factory. |
||
| 412 | */ |
||
| 413 | 6 | public function getResponseFactory() |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Generate and return image response. |
||
| 420 | * @param string $path Image path. |
||
| 421 | * @param array $params Image manipulation params. |
||
| 422 | * @return mixed Image response. |
||
| 423 | * @throws InvalidArgumentException |
||
| 424 | */ |
||
| 425 | 6 | public function getImageResponse($path, array $params) |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Generate and return Base64 encoded image. |
||
| 440 | * @param string $path Image path. |
||
| 441 | * @param array $params Image manipulation params. |
||
| 442 | * @return string Base64 encoded image. |
||
| 443 | * @throws FilesystemException |
||
| 444 | */ |
||
| 445 | 6 | public function getImageAsBase64($path, array $params) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Generate and output image. |
||
| 462 | * @param string $path Image path. |
||
| 463 | * @param array $params Image manipulation params. |
||
| 464 | * @throws InvalidArgumentException |
||
| 465 | */ |
||
| 466 | 3 | public function outputImage($path, array $params) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Generate manipulated image. |
||
| 486 | * @param string $path Image path. |
||
| 487 | * @param array $params Image manipulation params. |
||
| 488 | * @return string Cache path. |
||
| 489 | * @throws FileNotFoundException |
||
| 490 | * @throws FilesystemException |
||
| 491 | */ |
||
| 492 | 30 | public function makeImage($path, array $params) |
|
| 549 | |||
| 550 | /** |
||
| 551 | * Sets the temp directory used by this server to |
||
| 552 | * store images. This MUST to be a local path. This is because EXIF data |
||
| 553 | * can only be read from an actual file. |
||
| 554 | * @param string $path to store temp files. |
||
| 555 | * @throws InvalidArgumentException |
||
| 556 | */ |
||
| 557 | 6 | public function setTempDir($path) |
|
| 567 | } |
||
| 568 |