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 | 153 | 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 | 153 | 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 | 69 | public function getSourcePath($path) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Check if a source file exists. |
||
| 157 | * @param string $path Image path. |
||
| 158 | * @return bool Whether the source file exists. |
||
| 159 | */ |
||
| 160 | 18 | public function sourceFileExists($path) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Set base URL. |
||
| 167 | * @param string $baseUrl Base URL. |
||
| 168 | */ |
||
| 169 | 12 | public function setBaseUrl($baseUrl) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Get base URL. |
||
| 176 | * @return string Base URL. |
||
| 177 | */ |
||
| 178 | 6 | public function getBaseUrl() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Set cache file system. |
||
| 185 | * @param FilesystemInterface $cache Cache file system. |
||
| 186 | */ |
||
| 187 | 153 | public function setCache(FilesystemInterface $cache) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Get cache file system. |
||
| 194 | * @return FilesystemInterface Cache file system. |
||
| 195 | */ |
||
| 196 | 6 | public function getCache() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Set cache path prefix. |
||
| 203 | * @param string $cachePathPrefix Cache path prefix. |
||
| 204 | */ |
||
| 205 | 12 | public function setCachePathPrefix($cachePathPrefix) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Get cache path prefix. |
||
| 212 | * @return string Cache path prefix. |
||
| 213 | */ |
||
| 214 | 6 | public function getCachePathPrefix() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Set the group cache in folders setting. |
||
| 221 | * @param bool $groupCacheInFolders Whether to group cache in folders. |
||
| 222 | */ |
||
| 223 | 15 | public function setGroupCacheInFolders($groupCacheInFolders) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Get the group cache in folders setting. |
||
| 230 | * @return bool Whether to group cache in folders. |
||
| 231 | */ |
||
| 232 | 6 | public function getGroupCacheInFolders() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Set the cache with file extensions setting. |
||
| 239 | * @param bool $groupCacheInFolders Whether to cache with file extensions. |
||
|
|
|||
| 240 | */ |
||
| 241 | 12 | public function setCacheWithFileExtensions($cacheWithFileExtensions) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Get the cache with file extensions setting. |
||
| 248 | * @return bool Whether to cache with file extensions. |
||
| 249 | */ |
||
| 250 | 6 | public function getCacheWithFileExtensions() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Get cache path. |
||
| 257 | * @param string $path Image path. |
||
| 258 | * @param array $params Image manipulation params. |
||
| 259 | * @return string Cache path. |
||
| 260 | */ |
||
| 261 | 51 | public function getCachePath($path, array $params = []) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Check if a cache file exists. |
||
| 290 | * @param string $path Image path. |
||
| 291 | * @param array $params Image manipulation params. |
||
| 292 | * @return bool Whether the cache file exists. |
||
| 293 | */ |
||
| 294 | 33 | public function cacheFileExists($path, array $params) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Delete cached manipulations for an image. |
||
| 303 | * @param string $path Image path. |
||
| 304 | * @return bool Whether the delete succeeded. |
||
| 305 | */ |
||
| 306 | 6 | public function deleteCache($path) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Set image manipulation API. |
||
| 321 | * @param ApiInterface $api Image manipulation API. |
||
| 322 | */ |
||
| 323 | 153 | public function setApi(ApiInterface $api) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Get image manipulation API. |
||
| 330 | * @return ApiInterface Image manipulation API. |
||
| 331 | */ |
||
| 332 | 6 | public function getApi() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Set default image manipulations. |
||
| 339 | * @param array $defaults Default image manipulations. |
||
| 340 | */ |
||
| 341 | 15 | public function setDefaults(array $defaults) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Get default image manipulations. |
||
| 348 | * @return array Default image manipulations. |
||
| 349 | */ |
||
| 350 | 6 | public function getDefaults() |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Set preset image manipulations. |
||
| 357 | * @param array $presets Preset image manipulations. |
||
| 358 | */ |
||
| 359 | 15 | public function setPresets(array $presets) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Get preset image manipulations. |
||
| 366 | * @return array Preset image manipulations. |
||
| 367 | */ |
||
| 368 | 6 | public function getPresets() |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Get all image manipulations params, including defaults and presets. |
||
| 375 | * @param array $params Image manipulation params. |
||
| 376 | * @return array All image manipulation params. |
||
| 377 | */ |
||
| 378 | 54 | public function getAllParams(array $params) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Set response factory. |
||
| 395 | * @param ResponseFactoryInterface|null $responseFactory Response factory. |
||
| 396 | */ |
||
| 397 | 15 | public function setResponseFactory(ResponseFactoryInterface $responseFactory = null) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Get response factory. |
||
| 404 | * @return ResponseFactoryInterface Response factory. |
||
| 405 | */ |
||
| 406 | 6 | public function getResponseFactory() |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Generate and return image response. |
||
| 413 | * @param string $path Image path. |
||
| 414 | * @param array $params Image manipulation params. |
||
| 415 | * @return mixed Image response. |
||
| 416 | * @throws InvalidArgumentException |
||
| 417 | */ |
||
| 418 | 6 | public function getImageResponse($path, array $params) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Generate and return Base64 encoded image. |
||
| 433 | * @param string $path Image path. |
||
| 434 | * @param array $params Image manipulation params. |
||
| 435 | * @return string Base64 encoded image. |
||
| 436 | * @throws FilesystemException |
||
| 437 | */ |
||
| 438 | 6 | public function getImageAsBase64($path, array $params) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Generate and output image. |
||
| 455 | * @param string $path Image path. |
||
| 456 | * @param array $params Image manipulation params. |
||
| 457 | * @throws InvalidArgumentException |
||
| 458 | */ |
||
| 459 | 3 | public function outputImage($path, array $params) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Generate manipulated image. |
||
| 479 | * @param string $path Image path. |
||
| 480 | * @param array $params Image manipulation params. |
||
| 481 | * @return string Cache path. |
||
| 482 | * @throws FileNotFoundException |
||
| 483 | * @throws FilesystemException |
||
| 484 | */ |
||
| 485 | 30 | public function makeImage($path, array $params) |
|
| 542 | } |
||
| 543 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.