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 | * Image manipulation API. |
||
47 | * @var ApiInterface |
||
48 | */ |
||
49 | protected $api; |
||
50 | |||
51 | /** |
||
52 | * Response factory. |
||
53 | * @var ResponseFactoryInterface|null |
||
54 | */ |
||
55 | protected $responseFactory; |
||
56 | |||
57 | /** |
||
58 | * Base URL. |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $baseUrl; |
||
62 | |||
63 | /** |
||
64 | * Default image manipulations. |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $defaults = []; |
||
68 | |||
69 | /** |
||
70 | * Preset image manipulations. |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $presets = []; |
||
74 | |||
75 | /** |
||
76 | * Create Server instance. |
||
77 | * @param FilesystemInterface $source Source file system. |
||
78 | * @param FilesystemInterface $cache Cache file system. |
||
79 | * @param ApiInterface $api Image manipulation API. |
||
80 | */ |
||
81 | 141 | public function __construct(FilesystemInterface $source, FilesystemInterface $cache, ApiInterface $api) |
|
87 | |||
88 | /** |
||
89 | * Set source file system. |
||
90 | * @param FilesystemInterface $source Source file system. |
||
91 | */ |
||
92 | 141 | public function setSource(FilesystemInterface $source) |
|
96 | |||
97 | /** |
||
98 | * Get source file system. |
||
99 | * @return FilesystemInterface Source file system. |
||
100 | */ |
||
101 | 6 | public function getSource() |
|
105 | |||
106 | /** |
||
107 | * Set source path prefix. |
||
108 | * @param string $sourcePathPrefix Source path prefix. |
||
109 | */ |
||
110 | 15 | public function setSourcePathPrefix($sourcePathPrefix) |
|
114 | |||
115 | /** |
||
116 | * Get source path prefix. |
||
117 | * @return string Source path prefix. |
||
118 | */ |
||
119 | 6 | public function getSourcePathPrefix() |
|
123 | |||
124 | /** |
||
125 | * Get source path. |
||
126 | * @param string $path Image path. |
||
127 | * @return string The source path. |
||
128 | * @throws FileNotFoundException |
||
129 | */ |
||
130 | 66 | public function getSourcePath($path) |
|
148 | |||
149 | /** |
||
150 | * Check if a source file exists. |
||
151 | * @param string $path Image path. |
||
152 | * @return bool Whether the source file exists. |
||
153 | */ |
||
154 | 18 | public function sourceFileExists($path) |
|
158 | |||
159 | /** |
||
160 | * Set base URL. |
||
161 | * @param string $baseUrl Base URL. |
||
162 | */ |
||
163 | 12 | public function setBaseUrl($baseUrl) |
|
167 | |||
168 | /** |
||
169 | * Get base URL. |
||
170 | * @return string Base URL. |
||
171 | */ |
||
172 | 6 | public function getBaseUrl() |
|
176 | |||
177 | /** |
||
178 | * Set cache file system. |
||
179 | * @param FilesystemInterface $cache Cache file system. |
||
180 | */ |
||
181 | 141 | public function setCache(FilesystemInterface $cache) |
|
185 | |||
186 | /** |
||
187 | * Get cache file system. |
||
188 | * @return FilesystemInterface Cache file system. |
||
189 | */ |
||
190 | 6 | public function getCache() |
|
194 | |||
195 | /** |
||
196 | * Set cache path prefix. |
||
197 | * @param string $cachePathPrefix Cache path prefix. |
||
198 | */ |
||
199 | 12 | public function setCachePathPrefix($cachePathPrefix) |
|
203 | |||
204 | /** |
||
205 | * Get cache path prefix. |
||
206 | * @return string Cache path prefix. |
||
207 | */ |
||
208 | 6 | public function getCachePathPrefix() |
|
212 | |||
213 | /** |
||
214 | * Set the group cache in folders setting. |
||
215 | * @param bool $groupCacheInFolders Whether to group cache in folders. |
||
216 | */ |
||
217 | 12 | public function setGroupCacheInFolders($groupCacheInFolders) |
|
221 | |||
222 | /** |
||
223 | * Get the group cache in folders setting. |
||
224 | * @return bool Whether to group cache in folders. |
||
225 | */ |
||
226 | 6 | public function getGroupCacheInFolders() |
|
230 | |||
231 | /** |
||
232 | * Get cache path. |
||
233 | * @param string $path Image path. |
||
234 | * @param array $params Image manipulation params. |
||
235 | * @return string Cache path. |
||
236 | */ |
||
237 | 48 | public function getCachePath($path, array $params = []) |
|
259 | |||
260 | /** |
||
261 | * Check if a cache file exists. |
||
262 | * @param string $path Image path. |
||
263 | * @param array $params Image manipulation params. |
||
264 | * @return bool Whether the cache file exists. |
||
265 | */ |
||
266 | 33 | public function cacheFileExists($path, array $params) |
|
272 | |||
273 | /** |
||
274 | * Delete cached manipulations for an image. |
||
275 | * @param string $path Image path. |
||
276 | * @return bool Whether the delete succeeded. |
||
277 | */ |
||
278 | 3 | public function deleteCache($path) |
|
284 | |||
285 | /** |
||
286 | * Set image manipulation API. |
||
287 | * @param ApiInterface $api Image manipulation API. |
||
288 | */ |
||
289 | 141 | public function setApi(ApiInterface $api) |
|
293 | |||
294 | /** |
||
295 | * Get image manipulation API. |
||
296 | * @return ApiInterface Image manipulation API. |
||
297 | */ |
||
298 | 6 | public function getApi() |
|
302 | |||
303 | /** |
||
304 | * Set default image manipulations. |
||
305 | * @param array $defaults Default image manipulations. |
||
306 | */ |
||
307 | 15 | public function setDefaults(array $defaults) |
|
311 | |||
312 | /** |
||
313 | * Get default image manipulations. |
||
314 | * @return array Default image manipulations. |
||
315 | */ |
||
316 | 6 | public function getDefaults() |
|
320 | |||
321 | /** |
||
322 | * Set preset image manipulations. |
||
323 | * @param array $presets Preset image manipulations. |
||
324 | */ |
||
325 | 15 | public function setPresets(array $presets) |
|
329 | |||
330 | /** |
||
331 | * Get preset image manipulations. |
||
332 | * @return array Preset image manipulations. |
||
333 | */ |
||
334 | 6 | public function getPresets() |
|
338 | |||
339 | /** |
||
340 | * Get all image manipulations params, including defaults and presets. |
||
341 | * @param array $params Image manipulation params. |
||
342 | * @return array All image manipulation params. |
||
343 | */ |
||
344 | 51 | public function getAllParams(array $params) |
|
358 | |||
359 | /** |
||
360 | * Set response factory. |
||
361 | * @param ResponseFactoryInterface|null $responseFactory Response factory. |
||
362 | */ |
||
363 | 15 | public function setResponseFactory(ResponseFactoryInterface $responseFactory = null) |
|
367 | |||
368 | /** |
||
369 | * Get response factory. |
||
370 | * @return ResponseFactoryInterface Response factory. |
||
371 | */ |
||
372 | 6 | public function getResponseFactory() |
|
376 | |||
377 | /** |
||
378 | * Generate and return image response. |
||
379 | * @param string $path Image path. |
||
380 | * @param array $params Image manipulation params. |
||
381 | * @return mixed Image response. |
||
382 | * @throws InvalidArgumentException |
||
383 | */ |
||
384 | 6 | public function getImageResponse($path, array $params) |
|
396 | |||
397 | /** |
||
398 | * Generate and return Base64 encoded image. |
||
399 | * @param string $path Image path. |
||
400 | * @param array $params Image manipulation params. |
||
401 | * @return string Base64 encoded image. |
||
402 | * @throws FilesystemException |
||
403 | */ |
||
404 | 6 | public function getImageAsBase64($path, array $params) |
|
418 | |||
419 | /** |
||
420 | * Generate and output image. |
||
421 | * @param string $path Image path. |
||
422 | * @param array $params Image manipulation params. |
||
423 | * @throws InvalidArgumentException |
||
424 | */ |
||
425 | 3 | public function outputImage($path, array $params) |
|
440 | |||
441 | /** |
||
442 | * Generate manipulated image. |
||
443 | * @param string $path Image path. |
||
444 | * @param array $params Image manipulation params. |
||
445 | * @return string Cache path. |
||
446 | * @throws FileNotFoundException |
||
447 | * @throws FilesystemException |
||
448 | */ |
||
449 | 30 | public function makeImage($path, array $params) |
|
506 | } |
||
507 |