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 | * Path object. |
||
23 | * |
||
24 | * @var Path |
||
25 | */ |
||
26 | private $path; |
||
27 | |||
28 | /** |
||
29 | * Backup filename. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | private $filename; |
||
34 | |||
35 | /** |
||
36 | * Filename with potential date placeholders like %d. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $filenameRaw; |
||
41 | |||
42 | /** |
||
43 | * List of custom file suffixes f.e. 'tar' |
||
44 | * |
||
45 | * @var string[] |
||
46 | */ |
||
47 | private $fileSuffixes = []; |
||
48 | |||
49 | /** |
||
50 | * Indicates if the filename changes over time. |
||
51 | * |
||
52 | * @var bool |
||
53 | */ |
||
54 | private $filenameIsChanging = false; |
||
55 | |||
56 | /** |
||
57 | * Target MIME type |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | private $mimeType = 'text/plain'; |
||
62 | |||
63 | /** |
||
64 | * Size in bytes |
||
65 | * |
||
66 | * @var int |
||
67 | */ |
||
68 | private $size; |
||
69 | |||
70 | /** |
||
71 | * Should the file be compressed. |
||
72 | * |
||
73 | * @var bool |
||
74 | */ |
||
75 | private $compress = false; |
||
76 | |||
77 | /** |
||
78 | * File compression. |
||
79 | * |
||
80 | * @var \phpbu\App\Backup\Target\Compression |
||
81 | */ |
||
82 | private $compression; |
||
83 | |||
84 | /** |
||
85 | * Should the file be encrypted. |
||
86 | * |
||
87 | * @var bool |
||
88 | */ |
||
89 | private $crypt = false; |
||
90 | |||
91 | /** |
||
92 | * File crypter. |
||
93 | * |
||
94 | * @var \phpbu\App\Backup\Crypter |
||
95 | */ |
||
96 | private $crypter; |
||
97 | |||
98 | /** |
||
99 | * Constructor. |
||
100 | * |
||
101 | * @param string $path |
||
102 | * @param string $filename |
||
103 | * @param integer $time |
||
104 | */ |
||
105 | 46 | public function __construct($path, $filename, $time = null) |
|
110 | |||
111 | /** |
||
112 | * Filename setter. |
||
113 | * |
||
114 | * @param string $file |
||
115 | * @param int $time |
||
116 | */ |
||
117 | 46 | public function setFile($file, $time = null) |
|
126 | |||
127 | /** |
||
128 | * Append another suffix to the filename. |
||
129 | * |
||
130 | * @param string $suffix |
||
131 | */ |
||
132 | 3 | public function appendFileSuffix(string $suffix) |
|
136 | |||
137 | /** |
||
138 | * Checks if the backup target directory is writable. |
||
139 | * Creates the Directory if it doesn't exist. |
||
140 | * |
||
141 | * @throws \phpbu\App\Exception |
||
142 | */ |
||
143 | 4 | public function setupPath() |
|
159 | |||
160 | /** |
||
161 | * Target file MIME type setter. |
||
162 | * |
||
163 | * @param string $mime |
||
164 | */ |
||
165 | 1 | public function setMimeType(string $mime) |
|
169 | |||
170 | /** |
||
171 | * Return the path to the backup file. |
||
172 | * |
||
173 | * @return Path |
||
174 | */ |
||
175 | 17 | public function getPath() : Path |
|
179 | |||
180 | /** |
||
181 | * Return the name to the backup file. |
||
182 | * |
||
183 | * @param bool $plain |
||
184 | * @return string |
||
185 | */ |
||
186 | 26 | public function getFilename(bool $plain = false) : string |
|
190 | |||
191 | /** |
||
192 | * Return the name of the backup file without compressor or encryption suffix. |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | 1 | public function getFilenamePlain() : string |
|
200 | |||
201 | /** |
||
202 | * Return the raw name of the backup file incl. date placeholder. |
||
203 | * |
||
204 | * @param bool $plain |
||
205 | * @return string |
||
206 | */ |
||
207 | 14 | public function getFilenameRaw($plain = false) : string |
|
211 | |||
212 | /** |
||
213 | * Return custom file suffix like '.tar'. |
||
214 | * |
||
215 | * @param bool $plain |
||
216 | * @return string |
||
217 | */ |
||
218 | 26 | public function getFilenameSuffix($plain = false) : string |
|
222 | |||
223 | /** |
||
224 | * Return added suffixes. |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | 26 | public function getSuffixToAppend() : string |
|
232 | |||
233 | /** |
||
234 | * Return the compressor suffix. |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | 24 | public function getCompressionSuffix() : string |
|
242 | |||
243 | /** |
||
244 | * Return the crypter suffix. |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | 24 | public function getCrypterSuffix() : string |
|
252 | |||
253 | /** |
||
254 | * Return file MIME type. |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | 3 | public function getMimeType() : string |
|
266 | |||
267 | /** |
||
268 | * Size setter. |
||
269 | * |
||
270 | * @param int $size |
||
271 | */ |
||
272 | 1 | public function setSize(int $size) |
|
276 | |||
277 | /** |
||
278 | * Return the actual file size in bytes. |
||
279 | * |
||
280 | * @return int |
||
281 | * @throws \phpbu\App\Exception |
||
282 | */ |
||
283 | 3 | public function getSize() : int |
|
293 | |||
294 | /** |
||
295 | * Target file exists already. |
||
296 | * |
||
297 | * @param bool $plain |
||
298 | * @return bool |
||
299 | */ |
||
300 | 1 | public function fileExists(bool $plain = false) : bool |
|
304 | |||
305 | /** |
||
306 | * Return as backup file object. |
||
307 | * |
||
308 | * @return \phpbu\App\Backup\File\Local |
||
309 | */ |
||
310 | 1 | public function toFile() : Local |
|
314 | |||
315 | /** |
||
316 | * Return path and filename of the backup file. |
||
317 | * |
||
318 | * @param bool $plain |
||
319 | * @return string |
||
320 | */ |
||
321 | 16 | public function getPathname(bool $plain = false) : string |
|
325 | |||
326 | /** |
||
327 | * Return path and plain filename of the backup file. |
||
328 | * |
||
329 | * @return string |
||
330 | */ |
||
331 | 1 | public function getPathnamePlain() : string |
|
335 | |||
336 | /** |
||
337 | * Filename configured with any date placeholders. |
||
338 | * |
||
339 | * @return bool |
||
340 | */ |
||
341 | 2 | public function hasChangingFilename() : bool |
|
345 | |||
346 | /** |
||
347 | * Disable file compression. |
||
348 | */ |
||
349 | 2 | public function disableCompression() |
|
353 | |||
354 | /** |
||
355 | * Enable file compression. |
||
356 | * |
||
357 | * @throws \phpbu\App\Exception |
||
358 | */ |
||
359 | 2 | public function enableCompression() |
|
366 | |||
367 | /** |
||
368 | * Compression setter. |
||
369 | * |
||
370 | * @param \phpbu\App\Backup\Target\Compression $compression |
||
371 | */ |
||
372 | 12 | public function setCompression(Target\Compression $compression) |
|
377 | |||
378 | /** |
||
379 | * Compressor getter. |
||
380 | * |
||
381 | * @return \phpbu\App\Backup\Target\Compression |
||
382 | */ |
||
383 | 1 | public function getCompression() : Target\Compression |
|
387 | |||
388 | /** |
||
389 | * Is a compressor set? |
||
390 | * |
||
391 | * @return bool |
||
392 | */ |
||
393 | 29 | public function shouldBeCompressed() : bool |
|
397 | |||
398 | /** |
||
399 | * Crypter setter. |
||
400 | * |
||
401 | * @param \phpbu\App\Backup\Crypter $crypter |
||
402 | */ |
||
403 | 3 | public function setCrypter(Crypter $crypter) |
|
408 | |||
409 | /** |
||
410 | * Crypter getter. |
||
411 | * |
||
412 | * @return \phpbu\App\Backup\Crypter |
||
413 | */ |
||
414 | 1 | public function getCrypter() : Crypter |
|
418 | |||
419 | /** |
||
420 | * Disable file encryption. |
||
421 | */ |
||
422 | 1 | public function disableEncryption() |
|
426 | |||
427 | /** |
||
428 | * Is a crypter set? |
||
429 | * |
||
430 | * @return bool |
||
431 | */ |
||
432 | 25 | public function shouldBeEncrypted() : bool |
|
436 | |||
437 | /** |
||
438 | * Magic to string method. |
||
439 | * |
||
440 | * @return string |
||
441 | */ |
||
442 | 2 | public function __toString() : string |
|
446 | } |
||
447 |