Complex classes like Target 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 Target, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Target |
||
20 | { |
||
21 | /** |
||
22 | * Absolute path to the directory where to store the backup. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | private $path; |
||
27 | |||
28 | /** |
||
29 | * Path to the backup with potential date placeholders like %d. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | private $pathRaw; |
||
34 | |||
35 | /** |
||
36 | * Indicates if the path changes over time. |
||
37 | * |
||
38 | * @var bool |
||
39 | */ |
||
40 | private $pathIsChanging = false; |
||
41 | |||
42 | /** |
||
43 | * Part of the path without placeholders |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | private $pathNotChanging; |
||
48 | |||
49 | /** |
||
50 | * List of directories containing date placeholders |
||
51 | * |
||
52 | * @var string[] |
||
53 | */ |
||
54 | private $pathElementsChanging = []; |
||
55 | |||
56 | /** |
||
57 | * Backup filename. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | private $filename; |
||
62 | |||
63 | /** |
||
64 | * Filename with potential date placeholders like %d. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | private $filenameRaw; |
||
69 | |||
70 | /** |
||
71 | * List of custom file suffixes f.e. 'tar' |
||
72 | * |
||
73 | * @var string[] |
||
74 | */ |
||
75 | private $fileSuffixes = []; |
||
76 | |||
77 | /** |
||
78 | * Indicates if the filename changes over time. |
||
79 | * |
||
80 | * @var bool |
||
81 | */ |
||
82 | private $filenameIsChanging = false; |
||
83 | |||
84 | /** |
||
85 | * Target MIME type |
||
86 | * |
||
87 | * @var string |
||
88 | */ |
||
89 | private $mimeType = 'text/plain'; |
||
90 | |||
91 | /** |
||
92 | * Size in bytes |
||
93 | * |
||
94 | * @var int |
||
95 | */ |
||
96 | private $size; |
||
97 | |||
98 | /** |
||
99 | * Should the file be compressed. |
||
100 | * |
||
101 | * @var bool |
||
102 | */ |
||
103 | private $compress = false; |
||
104 | |||
105 | /** |
||
106 | * File compression. |
||
107 | * |
||
108 | * @var \phpbu\App\Backup\Target\Compression |
||
109 | */ |
||
110 | private $compression; |
||
111 | |||
112 | /** |
||
113 | * Should the file be encrypted. |
||
114 | * |
||
115 | * @var bool |
||
116 | */ |
||
117 | private $crypt = false; |
||
118 | |||
119 | /** |
||
120 | * File crypter. |
||
121 | * |
||
122 | * @var \phpbu\App\Backup\Crypter |
||
123 | */ |
||
124 | private $crypter; |
||
125 | |||
126 | 34 | /** |
|
127 | * Constructor. |
||
128 | 34 | * |
|
129 | 34 | * @param string $path |
|
130 | 34 | * @param string $filename |
|
131 | * @param integer $time |
||
132 | * @throws \phpbu\App\Exception |
||
133 | */ |
||
134 | public function __construct($path, $filename, $time = null) |
||
139 | 34 | ||
140 | /** |
||
141 | 34 | * Directory setter. |
|
142 | 34 | * |
|
143 | 10 | * @param string $path |
|
144 | * @param int $time |
||
145 | 10 | * @throws \phpbu\App\Exception |
|
146 | */ |
||
147 | 10 | public function setPath($path, $time = null) |
|
160 | 24 | ||
161 | /** |
||
162 | 34 | * Find path elements that can change because of placeholder usage. |
|
163 | 34 | * |
|
164 | * @param string $path |
||
165 | */ |
||
166 | private function detectChangingPathElements(string $path) |
||
186 | |||
187 | 4 | /** |
|
188 | * Filename setter. |
||
189 | * |
||
190 | 4 | * @param string $file |
|
191 | 3 | * @param int $time |
|
192 | 3 | */ |
|
193 | 3 | public function setFile($file, $time = null) |
|
202 | 2 | ||
203 | /** |
||
204 | * Append another suffix to the filename. |
||
205 | * |
||
206 | * @param string $suffix |
||
207 | */ |
||
208 | public function appendFileSuffix(string $suffix) |
||
212 | 1 | ||
213 | /** |
||
214 | * Checks if the backup target directory is writable. |
||
215 | * Creates the Directory if it doesn't exist. |
||
216 | * |
||
217 | * @throws \phpbu\App\Exception |
||
218 | */ |
||
219 | 5 | public function setupPath() |
|
235 | |||
236 | /** |
||
237 | * Target file MIME type setter. |
||
238 | * |
||
239 | * @param string $mime |
||
240 | 18 | */ |
|
241 | public function setMimeType(string $mime) |
||
245 | 16 | ||
246 | 16 | /** |
|
247 | 18 | * Return the path to the backup file. |
|
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | public function getPath() : string |
||
255 | 1 | ||
256 | /** |
||
257 | 1 | * Return the path to the backup file. |
|
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | public function getPathRaw() : string |
||
265 | 7 | ||
266 | /** |
||
267 | 7 | * Return the name to the backup file. |
|
268 | * |
||
269 | * @param bool $plain |
||
270 | * @return string |
||
271 | */ |
||
272 | public function getFilename(bool $plain = false) : string |
||
276 | |||
277 | 3 | /** |
|
278 | 3 | * Return the name of the backup file without compressor or encryption suffix. |
|
279 | 1 | * |
|
280 | 1 | * @return string |
|
281 | 3 | */ |
|
282 | public function getFilenamePlain() : string |
||
286 | |||
287 | /** |
||
288 | * Return the raw name of the backup file incl. date placeholder. |
||
289 | * |
||
290 | 2 | * @param bool $plain |
|
291 | * @return string |
||
292 | 2 | */ |
|
293 | 2 | public function getFilenameRaw($plain = false) : string |
|
297 | 1 | ||
298 | 1 | /** |
|
299 | * Return custom file suffix like '.tar'. |
||
300 | * |
||
301 | * @param bool $plain |
||
302 | * @return string |
||
303 | */ |
||
304 | public function getFilenameSuffix($plain = false) : string |
||
308 | |||
309 | 4 | /** |
|
310 | * Return added suffixes. |
||
311 | * |
||
312 | * @return string |
||
313 | */ |
||
314 | public function getSuffixToAppend() : string |
||
318 | 3 | ||
319 | /** |
||
320 | 3 | * Return the compressor suffix. |
|
321 | 1 | * |
|
322 | * @return string |
||
323 | 2 | */ |
|
324 | 1 | public function getCompressionSuffix() : string |
|
328 | |||
329 | /** |
||
330 | * Return the crypter suffix. |
||
331 | * |
||
332 | * @return string |
||
333 | */ |
||
334 | public function getCrypterSuffix() : string |
||
338 | |||
339 | 14 | /** |
|
340 | * Return file MIME type. |
||
341 | * |
||
342 | * @return string |
||
343 | */ |
||
344 | public function getMimeType() : string |
||
352 | |||
353 | /** |
||
354 | * Size setter. |
||
355 | * |
||
356 | * @param int $size |
||
357 | 2 | */ |
|
358 | public function setSize(int $size) |
||
362 | |||
363 | /** |
||
364 | * Return the actual file size in bytes. |
||
365 | * |
||
366 | * @return int |
||
367 | 7 | * @throws \phpbu\App\Exception |
|
368 | */ |
||
369 | 7 | public function getSize() : int |
|
379 | 1 | ||
380 | /** |
||
381 | * Target file exists already. |
||
382 | * |
||
383 | * @param bool $plain |
||
384 | * @return bool |
||
385 | */ |
||
386 | public function fileExists(bool $plain = false) : bool |
||
390 | |||
391 | /** |
||
392 | * Return as backup file object. |
||
393 | * |
||
394 | * @return \phpbu\App\Backup\File |
||
395 | */ |
||
396 | public function toFile() : File |
||
400 | |||
401 | /** |
||
402 | * Return path and filename of the backup file. |
||
403 | * |
||
404 | * @param bool $plain |
||
405 | * @return string |
||
406 | */ |
||
407 | public function getPathname(bool $plain = false) : string |
||
411 | |||
412 | /** |
||
413 | * Return path and plain filename of the backup file. |
||
414 | * |
||
415 | * @return string |
||
416 | */ |
||
417 | public function getPathnamePlain() : string |
||
421 | |||
422 | /** |
||
423 | * Is dirname configured with any date placeholders. |
||
424 | * |
||
425 | * @return bool |
||
426 | */ |
||
427 | public function hasChangingPath() : bool |
||
431 | 7 | ||
432 | 7 | /** |
|
433 | * Return the part of the path that is not changing. |
||
434 | * |
||
435 | * @return string |
||
436 | */ |
||
437 | public function getPathThatIsNotChanging() : string |
||
441 | 2 | ||
442 | /** |
||
443 | * Changing path elements getter. |
||
444 | * |
||
445 | * @return array |
||
446 | */ |
||
447 | public function getChangingPathElements() : array |
||
451 | 20 | ||
452 | /** |
||
453 | * Return amount of changing path elements. |
||
454 | * |
||
455 | * @return int |
||
456 | */ |
||
457 | public function countChangingPathElements() : int |
||
461 | 1 | ||
462 | /** |
||
463 | * Filename configured with any date placeholders. |
||
464 | * |
||
465 | * @return bool |
||
466 | */ |
||
467 | public function hasChangingFilename() : bool |
||
471 | 2 | ||
472 | 2 | /** |
|
473 | 2 | * Disable file compression. |
|
474 | */ |
||
475 | public function disableCompression() |
||
479 | |||
480 | 1 | /** |
|
481 | * Enable file compression. |
||
482 | 1 | * |
|
483 | * @throws \phpbu\App\Exception |
||
484 | */ |
||
485 | public function enableCompression() |
||
492 | |||
493 | /** |
||
494 | * Compression setter. |
||
495 | * |
||
496 | * @param \phpbu\App\Backup\Target\Compression $compression |
||
497 | */ |
||
498 | 16 | public function setCompression(Target\Compression $compression) |
|
503 | |||
504 | /** |
||
505 | * Compressor getter. |
||
506 | * |
||
507 | * @return \phpbu\App\Backup\Target\Compression |
||
508 | 2 | */ |
|
509 | public function getCompression() : Target\Compression |
||
513 | |||
514 | /** |
||
515 | * Is a compressor set? |
||
516 | * |
||
517 | * @return bool |
||
518 | */ |
||
519 | public function shouldBeCompressed() : bool |
||
523 | |||
524 | /** |
||
525 | * Crypter setter. |
||
526 | * |
||
527 | * @param \phpbu\App\Backup\Crypter $crypter |
||
528 | */ |
||
529 | public function setCrypter(Crypter $crypter) |
||
534 | |||
535 | /** |
||
536 | * Crypter getter. |
||
537 | * |
||
538 | * @return \phpbu\App\Backup\Crypter |
||
539 | */ |
||
540 | public function getCrypter() : Crypter |
||
544 | |||
545 | /** |
||
546 | * Disable file encryption. |
||
547 | */ |
||
548 | public function disableEncryption() |
||
552 | |||
553 | /** |
||
554 | * Is a crypter set? |
||
555 | * |
||
556 | * @return bool |
||
557 | */ |
||
558 | public function shouldBeEncrypted() : bool |
||
562 | |||
563 | /** |
||
564 | * Magic to string method. |
||
565 | * |
||
566 | * @return string |
||
567 | */ |
||
568 | public function __toString() : string |
||
572 | } |
||
573 |