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 boolean |
||
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 array |
||
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 array |
||
74 | */ |
||
75 | private $fileSuffixes = []; |
||
76 | |||
77 | /** |
||
78 | * Indicates if the filename changes over time. |
||
79 | * |
||
80 | * @var boolean |
||
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 integer |
||
95 | */ |
||
96 | private $size; |
||
97 | |||
98 | /** |
||
99 | * Should the file be compressed. |
||
100 | * |
||
101 | * @var boolean |
||
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 boolean |
||
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) |
||
135 | { |
||
136 | $this->setPath($path, $time); |
||
137 | $this->setFile($filename, $time); |
||
138 | } |
||
139 | 34 | ||
140 | /** |
||
141 | 34 | * Directory setter. |
|
142 | 34 | * |
|
143 | 10 | * @param string $path |
|
144 | * @param integer $time |
||
145 | 10 | * @throws \phpbu\App\Exception |
|
146 | */ |
||
147 | 10 | public function setPath($path, $time = null) |
|
148 | 10 | { |
|
149 | 10 | $this->pathRaw = $path; |
|
150 | 10 | if (Str::isContainingPlaceholder($path)) { |
|
151 | 10 | $this->pathIsChanging = true; |
|
152 | 10 | // path should be absolute so we remove the root slash |
|
153 | 10 | $dirs = explode('/', substr($this->pathRaw, 1)); |
|
154 | 10 | ||
155 | $this->pathNotChanging = ''; |
||
156 | 10 | $foundChangingElement = false; |
|
157 | foreach ($dirs as $d) { |
||
158 | 10 | if ($foundChangingElement || Str::isContainingPlaceholder($d)) { |
|
159 | 10 | $this->pathElementsChanging[] = $d; |
|
160 | 24 | $foundChangingElement = true; |
|
161 | } else { |
||
162 | 34 | $this->pathNotChanging .= DIRECTORY_SEPARATOR . $d; |
|
163 | 34 | } |
|
164 | } |
||
165 | // replace potential date placeholder |
||
166 | $path = Str::replaceDatePlaceholders($path, $time); |
||
167 | } else { |
||
168 | $this->pathNotChanging = $path; |
||
169 | } |
||
170 | $this->path = rtrim($path, DIRECTORY_SEPARATOR); |
||
171 | 34 | } |
|
172 | |||
173 | 34 | /** |
|
174 | 34 | * Filename setter. |
|
175 | 20 | * |
|
176 | 20 | * @param string $file |
|
177 | 20 | * @param integer $time |
|
178 | 34 | */ |
|
179 | 34 | public function setFile($file, $time = null) |
|
180 | { |
||
181 | $this->filenameRaw = $file; |
||
182 | if (Str::isContainingPlaceholder($file)) { |
||
183 | $this->filenameIsChanging = true; |
||
184 | $file = Str::replaceDatePlaceholders($file, $time); |
||
185 | } |
||
186 | $this->filename = $file; |
||
187 | 4 | } |
|
188 | |||
189 | /** |
||
190 | 4 | * Append another suffix to the filename. |
|
191 | 3 | * |
|
192 | 3 | * @param string $suffix |
|
193 | 3 | */ |
|
194 | 3 | public function appendFileSuffix($suffix) |
|
195 | 3 | { |
|
196 | 1 | $this->fileSuffixes[] = $suffix; |
|
197 | } |
||
198 | 2 | ||
199 | 3 | /** |
|
200 | 1 | * Checks if the backup target directory is writable. |
|
201 | * Creates the Directory if it doesn't exist. |
||
202 | 2 | * |
|
203 | * @throws \phpbu\App\Exception |
||
204 | */ |
||
205 | public function setupPath() |
||
206 | { |
||
207 | // if directory doesn't exist, create it |
||
208 | if (!is_dir($this->path)) { |
||
209 | 1 | $reporting = error_reporting(); |
|
210 | error_reporting(0); |
||
211 | 1 | $created = mkdir($this->path, 0755, true); |
|
212 | 1 | error_reporting($reporting); |
|
213 | if (!$created) { |
||
214 | throw new Exception(sprintf('cant\'t create directory: %s', $this->path)); |
||
215 | } |
||
216 | } |
||
217 | if (!is_writable($this->path)) { |
||
218 | throw new Exception(sprintf('no write permission for directory: %s', $this->path)); |
||
219 | 5 | } |
|
220 | } |
||
221 | 5 | ||
222 | /** |
||
223 | * Target file MIME type setter. |
||
224 | * |
||
225 | * @param string $mime |
||
226 | */ |
||
227 | public function setMimeType($mime) |
||
228 | { |
||
229 | 1 | $this->mimeType = $mime; |
|
230 | } |
||
231 | 1 | ||
232 | /** |
||
233 | * Return the path to the backup file. |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | public function getPath() |
||
238 | { |
||
239 | return $this->path; |
||
240 | 18 | } |
|
241 | |||
242 | 18 | /** |
|
243 | 18 | * Return the path to the backup file. |
|
244 | 16 | * |
|
245 | 16 | * @return string |
|
246 | 16 | */ |
|
247 | 18 | public function getPathRaw() |
|
248 | { |
||
249 | return $this->pathRaw; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Return the name to the backup file. |
||
254 | * |
||
255 | 1 | * @param bool $plain |
|
256 | * @return string |
||
257 | 1 | */ |
|
258 | public function getFilename($plain = false) |
||
259 | { |
||
260 | return $this->filename . $this->getFilenameSuffix($plain); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Return the name of the backup file without compressor or encryption suffix. |
||
265 | 7 | * |
|
266 | * @return string |
||
267 | 7 | */ |
|
268 | public function getFilenamePlain() |
||
272 | |||
273 | /** |
||
274 | * Return the raw name of the backup file incl. date placeholder. |
||
275 | 3 | * |
|
276 | * @param bool $plain |
||
277 | 3 | * @return string |
|
278 | 3 | */ |
|
279 | 1 | public function getFilenameRaw($plain = false) |
|
280 | 1 | { |
|
281 | 3 | return $this->filenameRaw . $this->getFilenameSuffix($plain); |
|
283 | |||
284 | /** |
||
285 | * Return custom file suffix like '.tar'. |
||
286 | * |
||
287 | * @param bool $plain |
||
288 | * @return string |
||
289 | */ |
||
290 | 2 | public function getFilenameSuffix($plain = false) |
|
294 | 1 | ||
295 | /** |
||
296 | 1 | * Return added suffixes. |
|
297 | 1 | * |
|
298 | 1 | * @return string |
|
299 | */ |
||
300 | public function getSuffixToAppend() |
||
304 | |||
305 | /** |
||
306 | * Return the compressor suffix. |
||
307 | 4 | * |
|
308 | * @return string |
||
309 | 4 | */ |
|
310 | public function getCompressionSuffix() |
||
314 | |||
315 | /** |
||
316 | * Return the crypter suffix. |
||
317 | * |
||
318 | 3 | * @return string |
|
319 | */ |
||
320 | 3 | public function getCrypterSuffix() |
|
324 | 1 | ||
325 | /** |
||
326 | 1 | * Return file MIME type. |
|
327 | 1 | * |
|
328 | * @return string |
||
329 | */ |
||
330 | public function getMimeType() |
||
338 | |||
339 | 14 | /** |
|
340 | * Size setter. |
||
341 | * |
||
342 | * @param int $size |
||
343 | */ |
||
344 | public function setSize($size) |
||
348 | |||
349 | 1 | /** |
|
350 | * Return the actual file size in bytes. |
||
351 | * |
||
352 | * @throws Exception |
||
353 | * @return integer |
||
354 | */ |
||
355 | public function getSize() |
||
365 | |||
366 | /** |
||
367 | 7 | * Target file exists already. |
|
368 | * |
||
369 | 7 | * @param boolean $plain |
|
370 | * @return boolean |
||
371 | */ |
||
372 | public function fileExists($plain = false) |
||
376 | |||
377 | 1 | /** |
|
378 | * Return as backup file object. |
||
379 | 1 | * |
|
380 | * @return \phpbu\App\Backup\File |
||
381 | */ |
||
382 | public function toFile() |
||
386 | |||
387 | 9 | /** |
|
388 | * Deletes the target file. |
||
389 | 9 | * |
|
390 | * @param boolean $plain |
||
391 | * @throws \phpbu\App\Exception |
||
392 | */ |
||
393 | public function unlink($plain = false) |
||
403 | |||
404 | /** |
||
405 | * Return path and filename of the backup file. |
||
406 | * |
||
407 | * @param bool $plain |
||
408 | * @return string |
||
409 | */ |
||
410 | public function getPathname($plain = false) |
||
414 | |||
415 | /** |
||
416 | * Return path and plain filename of the backup file. |
||
417 | * |
||
418 | * @return string |
||
419 | */ |
||
420 | public function getPathnamePlain() |
||
424 | |||
425 | /** |
||
426 | * Is dirname configured with any date placeholders. |
||
427 | * |
||
428 | 7 | * @return boolean |
|
429 | */ |
||
430 | 7 | public function hasChangingPath() |
|
434 | |||
435 | /** |
||
436 | * Return the part of the path that is not changing. |
||
437 | * |
||
438 | * @return string |
||
439 | 2 | */ |
|
440 | public function getPathThatIsNotChanging() |
||
444 | |||
445 | /** |
||
446 | * Changing path elements getter. |
||
447 | * |
||
448 | * @return array |
||
449 | 20 | */ |
|
450 | public function getChangingPathElements() |
||
454 | |||
455 | /** |
||
456 | * Return amount of changing path elements. |
||
457 | * |
||
458 | * @return integer |
||
459 | 1 | */ |
|
460 | public function countChangingPathElements() |
||
464 | |||
465 | /** |
||
466 | * Filename configured with any date placeholders. |
||
467 | * |
||
468 | * @return boolean |
||
469 | 2 | */ |
|
470 | public function hasChangingFilename() |
||
474 | |||
475 | /** |
||
476 | * Disable file compression. |
||
477 | */ |
||
478 | public function disableCompression() |
||
482 | 1 | ||
483 | /** |
||
484 | * Enable file compression. |
||
485 | * |
||
486 | * @throws \phpbu\App\Exception |
||
487 | */ |
||
488 | public function enableCompression() |
||
495 | |||
496 | /** |
||
497 | * Compression setter. |
||
498 | 16 | * |
|
499 | * @param \phpbu\App\Backup\Target\Compression $compression |
||
500 | 16 | */ |
|
501 | public function setCompression(Target\Compression $compression) |
||
506 | |||
507 | /** |
||
508 | 2 | * Compressor getter. |
|
509 | * |
||
510 | 2 | * @return \phpbu\App\Backup\Target\Compression |
|
511 | */ |
||
512 | public function getCompression() |
||
516 | |||
517 | /** |
||
518 | * Is a compressor set? |
||
519 | * |
||
520 | * @return boolean |
||
521 | */ |
||
522 | public function shouldBeCompressed() |
||
526 | |||
527 | /** |
||
528 | * Crypter setter. |
||
529 | * |
||
530 | * @param \phpbu\App\Backup\Crypter $crypter |
||
531 | */ |
||
532 | public function setCrypter(Crypter $crypter) |
||
537 | |||
538 | /** |
||
539 | * Crypter getter. |
||
540 | * |
||
541 | * @return \phpbu\App\Backup\Crypter |
||
542 | */ |
||
543 | public function getCrypter() |
||
547 | |||
548 | /** |
||
549 | * Disable file encryption. |
||
550 | */ |
||
551 | public function disableEncryption() |
||
555 | |||
556 | /** |
||
557 | * Is a crypter set? |
||
558 | * |
||
559 | * @return boolean |
||
560 | */ |
||
561 | public function shouldBeEncrypted() |
||
565 | |||
566 | /** |
||
567 | * Magic to string method. |
||
568 | * |
||
569 | * @return string |
||
570 | */ |
||
571 | public function __toString() |
||
575 | } |
||
576 |