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 |
||
18 | class Target |
||
19 | { |
||
20 | /** |
||
21 | * Absolute path to the directory where to store the backup. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | private $path; |
||
26 | |||
27 | /** |
||
28 | * Path to the backup with potential date placeholders like %d. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | private $pathRaw; |
||
33 | |||
34 | /** |
||
35 | * Indicates if the path changes over time. |
||
36 | * |
||
37 | * @var boolean |
||
38 | */ |
||
39 | private $pathIsChanging = false; |
||
40 | |||
41 | /** |
||
42 | * Part of the path without placeholders |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | private $pathNotChanging; |
||
47 | |||
48 | /** |
||
49 | * List of directories containing date placeholders |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | private $pathElementsChanging = []; |
||
54 | |||
55 | /** |
||
56 | * Backup filename. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | private $filename; |
||
61 | |||
62 | /** |
||
63 | * Filename with potential date placeholders like %d. |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | private $filenameRaw; |
||
68 | |||
69 | /** |
||
70 | * List of custom file suffixes f.e. 'tar' |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | private $fileSuffixes = []; |
||
75 | |||
76 | /** |
||
77 | * Indicates if the filename changes over time. |
||
78 | * |
||
79 | * @var boolean |
||
80 | */ |
||
81 | private $filenameIsChanging = false; |
||
82 | |||
83 | /** |
||
84 | * Target MIME type |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | private $mimeType = 'text/plain'; |
||
89 | |||
90 | /** |
||
91 | * Size in bytes |
||
92 | * |
||
93 | * @var integer |
||
94 | */ |
||
95 | private $size; |
||
96 | |||
97 | /** |
||
98 | * Should the file be compressed. |
||
99 | * |
||
100 | * @var boolean |
||
101 | */ |
||
102 | private $compress = false; |
||
103 | |||
104 | /** |
||
105 | * File compression. |
||
106 | * |
||
107 | * @var \phpbu\App\Backup\Compressor |
||
108 | */ |
||
109 | private $compressor; |
||
110 | |||
111 | /** |
||
112 | * Should the file be encrypted. |
||
113 | * |
||
114 | * @var boolean |
||
115 | */ |
||
116 | private $crypt = false; |
||
117 | |||
118 | /** |
||
119 | * File crypter. |
||
120 | * |
||
121 | * @var \phpbu\App\Backup\Crypter |
||
122 | */ |
||
123 | private $crypter; |
||
124 | |||
125 | /** |
||
126 | 34 | * Constructor. |
|
127 | * |
||
128 | 34 | * @param string $path |
|
129 | 34 | * @param string $filename |
|
130 | 34 | * @param integer $time |
|
131 | * @throws \phpbu\App\Exception |
||
132 | */ |
||
133 | public function __construct($path, $filename, $time = null) |
||
138 | |||
139 | 34 | /** |
|
140 | * Directory setter. |
||
141 | 34 | * |
|
142 | 34 | * @param string $path |
|
143 | 10 | * @param integer $time |
|
144 | * @throws \phpbu\App\Exception |
||
145 | 10 | */ |
|
146 | public function setPath($path, $time = null) |
||
171 | 34 | ||
172 | /** |
||
173 | 34 | * Does the path contain a date placeholder. |
|
174 | 34 | * |
|
175 | 20 | * @param string $path |
|
176 | 20 | * @return bool |
|
177 | 20 | */ |
|
178 | 34 | public function isContainingPlaceholder($path) |
|
182 | |||
183 | /** |
||
184 | * Filename setter. |
||
185 | * |
||
186 | * @param string $file |
||
187 | 4 | * @param integer $time |
|
188 | */ |
||
189 | public function setFile($file, $time = null) |
||
198 | 2 | ||
199 | 3 | /** |
|
200 | 1 | * Append another suffix to the filename. |
|
201 | * |
||
202 | 2 | * @param string $suffix |
|
203 | */ |
||
204 | public function appendFileSuffix($suffix) |
||
208 | |||
209 | 1 | /** |
|
210 | * Checks if the backup target directory is writable. |
||
211 | 1 | * Creates the Directory if it doesn't exist. |
|
212 | 1 | * |
|
213 | * @throws \phpbu\App\Exception |
||
214 | */ |
||
215 | public function setupPath() |
||
231 | 1 | ||
232 | /** |
||
233 | * Target file MIME type setter. |
||
234 | * |
||
235 | * @param string $mime |
||
236 | */ |
||
237 | public function setMimeType($mime) |
||
241 | |||
242 | 18 | /** |
|
243 | 18 | * Return the path to the backup file. |
|
244 | 16 | * |
|
245 | 16 | * @return string |
|
246 | 16 | */ |
|
247 | 18 | public function getPath() |
|
251 | |||
252 | /** |
||
253 | * Return the path to the backup file. |
||
254 | * |
||
255 | 1 | * @return string |
|
256 | */ |
||
257 | 1 | public function getPathRaw() |
|
261 | |||
262 | /** |
||
263 | * Return the name to the backup file. |
||
264 | * |
||
265 | 7 | * @return string |
|
266 | */ |
||
267 | 7 | public function getFilename() |
|
274 | |||
275 | 3 | /** |
|
276 | * Return the name of the backup file without compressor or encryption suffix. |
||
277 | 3 | * |
|
278 | 3 | * @return string |
|
279 | 1 | */ |
|
280 | 1 | public function getFilenamePlain() |
|
284 | |||
285 | /** |
||
286 | * Return the raw name of the backup file incl. date placeholder. |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | 2 | public function getFilenameRaw() |
|
297 | 1 | ||
298 | 1 | /** |
|
299 | * Return custom file suffix like '.tar'. |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | public function getFilenameSuffix() |
||
307 | 4 | ||
308 | /** |
||
309 | 4 | * Return the compressor suffix. |
|
310 | * |
||
311 | * @return string |
||
312 | */ |
||
313 | public function getCompressorSuffix() |
||
317 | |||
318 | 3 | /** |
|
319 | * Return the crypter suffix. |
||
320 | 3 | * |
|
321 | 1 | * @return string |
|
322 | */ |
||
323 | 2 | public function getCrypterSuffix() |
|
327 | 1 | ||
328 | /** |
||
329 | * Return file MIME type. |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | public function getMimeType() |
||
341 | |||
342 | /** |
||
343 | * Size setter. |
||
344 | * |
||
345 | * @param int $size |
||
346 | */ |
||
347 | 1 | public function setSize($size) |
|
351 | |||
352 | /** |
||
353 | * Return the actual file size in bytes. |
||
354 | * |
||
355 | * @throws Exception |
||
356 | * @return integer |
||
357 | 2 | */ |
|
358 | public function getSize() |
||
368 | |||
369 | 7 | /** |
|
370 | * Target file exists already. |
||
371 | * |
||
372 | * @param boolean $plain |
||
373 | * @return boolean |
||
374 | */ |
||
375 | public function fileExists($plain = false) |
||
379 | 1 | ||
380 | /** |
||
381 | * Return as backup file object. |
||
382 | * |
||
383 | * @return \phpbu\App\Backup\File |
||
384 | */ |
||
385 | public function toFile() |
||
389 | 9 | ||
390 | /** |
||
391 | * Deletes the target file. |
||
392 | * |
||
393 | * @param boolean $plain |
||
394 | * @throws \phpbu\App\Exception |
||
395 | */ |
||
396 | public function unlink($plain = false) |
||
406 | |||
407 | /** |
||
408 | * Return path and filename of the backup file. |
||
409 | * |
||
410 | * @return string |
||
411 | */ |
||
412 | public function getPathname() |
||
416 | |||
417 | /** |
||
418 | * Return path and plain filename of the backup file. |
||
419 | * |
||
420 | * @return string |
||
421 | */ |
||
422 | public function getPathnamePlain() |
||
426 | |||
427 | /** |
||
428 | 7 | * Is dirname configured with any date placeholders. |
|
429 | * |
||
430 | 7 | * @return boolean |
|
431 | 7 | */ |
|
432 | 7 | public function hasChangingPath() |
|
436 | |||
437 | /** |
||
438 | * Return the part of the path that is not changing. |
||
439 | 2 | * |
|
440 | * @return string |
||
441 | 2 | */ |
|
442 | public function getPathThatIsNotChanging() |
||
446 | |||
447 | /** |
||
448 | * Changing path elements getter. |
||
449 | 20 | * |
|
450 | * @return array |
||
451 | 20 | */ |
|
452 | public function getChangingPathElements() |
||
456 | |||
457 | /** |
||
458 | * Return amount of changing path elements. |
||
459 | 1 | * |
|
460 | * @return integer |
||
461 | 1 | */ |
|
462 | public function countChangingPathElements() |
||
466 | |||
467 | /** |
||
468 | * Filename configured with any date placeholders. |
||
469 | 2 | * |
|
470 | * @return boolean |
||
471 | 2 | */ |
|
472 | 2 | public function hasChangingFilename() |
|
476 | |||
477 | /** |
||
478 | * Disable file compression. |
||
479 | */ |
||
480 | 1 | public function disableCompression() |
|
484 | |||
485 | /** |
||
486 | * Enable file compression. |
||
487 | * |
||
488 | * @throws \phpbu\App\Exception |
||
489 | */ |
||
490 | public function enableCompression() |
||
497 | |||
498 | 16 | /** |
|
499 | * Compressor setter. |
||
500 | 16 | * |
|
501 | * @param \phpbu\App\Backup\Compressor $compressor |
||
502 | */ |
||
503 | public function setCompressor(Compressor $compressor) |
||
508 | 2 | ||
509 | /** |
||
510 | 2 | * Compressor getter. |
|
511 | * |
||
512 | * @return \phpbu\App\Backup\Compressor |
||
513 | */ |
||
514 | public function getCompressor() |
||
518 | |||
519 | /** |
||
520 | * Is a compressor set? |
||
521 | * |
||
522 | * @return boolean |
||
523 | */ |
||
524 | public function shouldBeCompressed() |
||
528 | |||
529 | /** |
||
530 | * Crypter setter. |
||
531 | * |
||
532 | * @param \phpbu\App\Backup\Crypter $crypter |
||
533 | */ |
||
534 | public function setCrypter(Crypter $crypter) |
||
539 | |||
540 | /** |
||
541 | * Crypter getter. |
||
542 | * |
||
543 | * @return \phpbu\App\Backup\Crypter |
||
544 | */ |
||
545 | public function getCrypter() |
||
549 | |||
550 | /** |
||
551 | * Disable file encryption. |
||
552 | */ |
||
553 | public function disableEncryption() |
||
557 | |||
558 | /** |
||
559 | * Is a crypter set? |
||
560 | * |
||
561 | * @return boolean |
||
562 | */ |
||
563 | public function shouldBeEncrypted() |
||
567 | |||
568 | /** |
||
569 | * Magic to string method. |
||
570 | * |
||
571 | * @return string |
||
572 | */ |
||
573 | public function __toString() |
||
577 | } |
||
578 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.