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 | * Indicates if the filename changes over time. |
||
71 | * |
||
72 | * @var boolean |
||
73 | */ |
||
74 | private $filenameIsChanging = false; |
||
75 | |||
76 | /** |
||
77 | * Target MIME type |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | private $mimeType = 'text/plain'; |
||
82 | |||
83 | /** |
||
84 | * Size in bytes |
||
85 | * |
||
86 | * @var integer |
||
87 | */ |
||
88 | private $size; |
||
89 | |||
90 | /** |
||
91 | * Should the file be compressed. |
||
92 | * |
||
93 | * @var boolean |
||
94 | */ |
||
95 | private $compress = false; |
||
96 | |||
97 | /** |
||
98 | * File compression. |
||
99 | * |
||
100 | * @var \phpbu\App\Backup\Compressor |
||
101 | */ |
||
102 | private $compressor; |
||
103 | |||
104 | /** |
||
105 | * Should the file be encrypted. |
||
106 | * |
||
107 | * @var boolean |
||
108 | */ |
||
109 | private $crypt = false; |
||
110 | |||
111 | /** |
||
112 | * File crypter. |
||
113 | * |
||
114 | * @var \phpbu\App\Backup\Crypter |
||
115 | */ |
||
116 | private $crypter; |
||
117 | |||
118 | /** |
||
119 | * Constructor. |
||
120 | * |
||
121 | * @param string $path |
||
122 | * @param string $filename |
||
123 | * @param integer $time |
||
124 | * @throws \phpbu\App\Exception |
||
125 | */ |
||
126 | 34 | public function __construct($path, $filename, $time = null) |
|
127 | { |
||
128 | 34 | $this->setPath($path, $time); |
|
129 | 34 | $this->setFile($filename, $time); |
|
130 | 34 | } |
|
131 | |||
132 | /** |
||
133 | * Directory setter. |
||
134 | * |
||
135 | * @param string $path |
||
136 | * @param integer $time |
||
137 | * @throws \phpbu\App\Exception |
||
138 | */ |
||
139 | 34 | public function setPath($path, $time = null) |
|
140 | { |
||
141 | 34 | $this->pathRaw = $path; |
|
142 | 34 | if (false !== strpos($path, '%')) { |
|
143 | 10 | $this->pathIsChanging = true; |
|
144 | // path should be absolute so we remove the root slash |
||
145 | 10 | $dirs = explode('/', substr($this->pathRaw, 1)); |
|
146 | |||
147 | 10 | $this->pathNotChanging = ''; |
|
148 | 10 | $foundChangingElement = false; |
|
149 | 10 | foreach ($dirs as $d) { |
|
150 | 10 | if ($foundChangingElement || false !== strpos($d, '%')) { |
|
151 | 10 | $this->pathElementsChanging[] = $d; |
|
152 | 10 | $foundChangingElement = true; |
|
153 | 10 | } else { |
|
154 | 10 | $this->pathNotChanging .= DIRECTORY_SEPARATOR . $d; |
|
155 | } |
||
156 | 10 | } |
|
157 | // replace potential date placeholder |
||
158 | 10 | $path = Str::replaceDatePlaceholders($path, $time); |
|
159 | 10 | } else { |
|
160 | 24 | $this->pathNotChanging = $path; |
|
161 | } |
||
162 | 34 | $this->path = rtrim($path, DIRECTORY_SEPARATOR); |
|
163 | 34 | } |
|
164 | |||
165 | /** |
||
166 | * Filename setter. |
||
167 | * |
||
168 | * @param string $file |
||
169 | * @param integer $time |
||
170 | */ |
||
171 | 34 | public function setFile($file, $time = null) |
|
172 | { |
||
173 | 34 | $this->filenameRaw = $file; |
|
174 | 34 | if (false !== strpos($file, '%')) { |
|
175 | 20 | $this->filenameIsChanging = true; |
|
176 | 20 | $file = Str::replaceDatePlaceholders($file, $time); |
|
177 | 20 | } |
|
178 | 34 | $this->filename = $file; |
|
179 | 34 | } |
|
180 | |||
181 | /** |
||
182 | * Checks if the backup target directory is writable. |
||
183 | * Creates the Directory if it doesn't exist. |
||
184 | * |
||
185 | * @throws \phpbu\App\Exception |
||
186 | */ |
||
187 | 4 | public function setupPath() |
|
203 | |||
204 | /** |
||
205 | * Target file MIME type setter. |
||
206 | * |
||
207 | * @param string $mime |
||
208 | */ |
||
209 | 1 | public function setMimeType($mime) |
|
213 | |||
214 | /** |
||
215 | * Return the path to the backup file. |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | 5 | public function getPath() |
|
223 | |||
224 | /** |
||
225 | * Return the path to the backup file. |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | 1 | public function getPathRaw() |
|
230 | { |
||
231 | 1 | return $this->pathRaw; |
|
232 | } |
||
233 | |||
234 | /** |
||
235 | * Return the name to the backup file. |
||
236 | * |
||
237 | * @param boolean $plain |
||
238 | * @return string |
||
239 | */ |
||
240 | 18 | public function getFilename($plain = false) |
|
241 | { |
||
242 | 18 | $suffix = ''; |
|
243 | 18 | if (!$plain) { |
|
244 | 16 | $suffix .= $this->shouldBeCompressed() ? '.' . $this->compressor->getSuffix() : ''; |
|
245 | 16 | $suffix .= $this->shouldBeEncrypted() ? '.' . $this->crypter->getSuffix() : ''; |
|
246 | 16 | } |
|
247 | 18 | return $this->filename . $suffix; |
|
248 | } |
||
249 | |||
250 | /** |
||
251 | * Return the name of the backup file without compressor or encryption suffix. |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | 1 | public function getFilenamePlain() |
|
256 | { |
||
257 | 1 | return $this->getFilename(true); |
|
258 | } |
||
259 | |||
260 | /** |
||
261 | * Return the raw name of the backup file incl. date placeholder. |
||
262 | * |
||
263 | * @return string |
||
264 | */ |
||
265 | 7 | public function getFilenameRaw() |
|
269 | |||
270 | /** |
||
271 | * Return file MIME type. |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | 3 | public function getMimeType() |
|
283 | |||
284 | /** |
||
285 | * Return the actual filesize in bytes. |
||
286 | * |
||
287 | * @throws Exception |
||
288 | * @return integer |
||
289 | */ |
||
290 | 2 | public function getSize() |
|
300 | |||
301 | /** |
||
302 | * Target file exists already. |
||
303 | * |
||
304 | * @param boolean $plain |
||
305 | * @return boolean |
||
306 | */ |
||
307 | 4 | public function fileExists($plain = false) |
|
311 | |||
312 | /** |
||
313 | * Return as backup file object. |
||
314 | * |
||
315 | * @return \phpbu\App\Backup\File |
||
316 | */ |
||
317 | public function toFile() |
||
321 | 1 | ||
322 | /** |
||
323 | 2 | * Deletes the target file. |
|
324 | 1 | * |
|
325 | * @param boolean $plain |
||
326 | 1 | * @throws \phpbu\App\Exception |
|
327 | 1 | */ |
|
328 | public function unlink($plain = false) |
||
338 | |||
339 | 14 | /** |
|
340 | * Return path and filename of the backup file. |
||
341 | * |
||
342 | * @param boolean $plain |
||
343 | * @return string |
||
344 | */ |
||
345 | public function getPathname($plain = false) |
||
351 | |||
352 | /** |
||
353 | * Return path and plain filename of the backup file. |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | 2 | public function getPathnamePlain() |
|
361 | |||
362 | /** |
||
363 | * Is dirname configured with any date placeholders. |
||
364 | * |
||
365 | * @return boolean |
||
366 | */ |
||
367 | 7 | public function hasChangingPath() |
|
371 | |||
372 | /** |
||
373 | * Return the part of the path that is not changing. |
||
374 | * |
||
375 | * @return string |
||
376 | */ |
||
377 | 1 | public function getPathThatIsNotChanging() |
|
381 | |||
382 | /** |
||
383 | * Changing path elements getter. |
||
384 | * |
||
385 | * @return array |
||
386 | */ |
||
387 | 9 | public function getChangingPathElements() |
|
391 | |||
392 | /** |
||
393 | * Return amount of changing path elements. |
||
394 | * |
||
395 | * @return integer |
||
396 | */ |
||
397 | 2 | public function countChangingPathElements() |
|
401 | |||
402 | /** |
||
403 | * Filename configured with any date placeholders. |
||
404 | * |
||
405 | * @return boolean |
||
406 | */ |
||
407 | public function hasChangingFilename() |
||
411 | |||
412 | /** |
||
413 | * Disable file compression. |
||
414 | */ |
||
415 | public function disableCompression() |
||
419 | |||
420 | /** |
||
421 | * Enable file compression. |
||
422 | * |
||
423 | * @throws \phpbu\App\Exception |
||
424 | */ |
||
425 | public function enableCompression() |
||
432 | 7 | ||
433 | /** |
||
434 | * Compressor setter. |
||
435 | * |
||
436 | * @param \phpbu\App\Backup\Compressor $compressor |
||
437 | */ |
||
438 | public function setCompressor(Compressor $compressor) |
||
443 | |||
444 | /** |
||
445 | * Compressor getter. |
||
446 | * |
||
447 | * @return \phpbu\App\Backup\Compressor |
||
448 | */ |
||
449 | 20 | public function getCompressor() |
|
453 | |||
454 | /** |
||
455 | * Is a compressor set? |
||
456 | * |
||
457 | * @return boolean |
||
458 | */ |
||
459 | 1 | public function shouldBeCompressed() |
|
463 | |||
464 | /** |
||
465 | * Is the target already compressed. |
||
466 | * |
||
467 | * @return boolean |
||
468 | */ |
||
469 | 2 | public function isCompressed() |
|
473 | 2 | ||
474 | /** |
||
475 | * Crypter setter. |
||
476 | * |
||
477 | * @param \phpbu\App\Backup\Crypter $crypter |
||
478 | */ |
||
479 | public function setCrypter(Crypter $crypter) |
||
484 | |||
485 | /** |
||
486 | * Crypter getter. |
||
487 | * |
||
488 | * @return \phpbu\App\Backup\Crypter |
||
489 | */ |
||
490 | public function getCrypter() |
||
494 | |||
495 | /** |
||
496 | * Disable file encryption. |
||
497 | */ |
||
498 | 16 | public function disableEncryption() |
|
502 | |||
503 | /** |
||
504 | * Is a crypter set? |
||
505 | * |
||
506 | * @return boolean |
||
507 | */ |
||
508 | 2 | public function shouldBeEncrypted() |
|
512 | |||
513 | /** |
||
514 | * Magic to string method. |
||
515 | * |
||
516 | * @return string |
||
517 | */ |
||
518 | public function __toString() |
||
522 | } |
||
523 |