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 | public function __construct($path, $filename, $time = null) |
||
106 | { |
||
107 | $this->path = new Path($path, $time); |
||
108 | $this->setFile($filename, $time); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Filename setter. |
||
113 | * |
||
114 | * @param string $file |
||
115 | * @param int $time |
||
116 | */ |
||
117 | public function setFile($file, $time = null) |
||
118 | { |
||
119 | $this->filenameRaw = $file; |
||
120 | if (Util\Path::isContainingPlaceholder($file)) { |
||
121 | $this->filenameIsChanging = true; |
||
122 | $file = Util\Path::replaceDatePlaceholders($file, $time); |
||
123 | } |
||
124 | $this->filename = $file; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Append another suffix to the filename. |
||
129 | * |
||
130 | * @param string $suffix |
||
131 | */ |
||
132 | public function appendFileSuffix(string $suffix) |
||
133 | { |
||
134 | 46 | $this->fileSuffixes[] = $suffix; |
|
135 | } |
||
136 | 46 | ||
137 | 46 | /** |
|
138 | 46 | * 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 | public function setupPath() |
||
144 | { |
||
145 | // if directory doesn't exist, create it |
||
146 | 46 | if (!is_dir($this->path->getPath())) { |
|
147 | $reporting = error_reporting(); |
||
148 | error_reporting(0); |
||
149 | 46 | $created = mkdir($this->path->getPath(), 0755, true); |
|
150 | 46 | error_reporting($reporting); |
|
151 | 46 | if (!$created) { |
|
152 | throw new Exception(sprintf('cant\'t create directory: %s', $this->path->getPath())); |
||
153 | 46 | } |
|
154 | 11 | } |
|
155 | 11 | if (!is_writable($this->path->getPath())) { |
|
156 | throw new Exception(sprintf('no write permission for directory: %s', $this->path->getPath())); |
||
157 | 11 | } |
|
158 | } |
||
159 | |||
160 | 46 | /** |
|
161 | 46 | * Target file MIME type setter. |
|
162 | * |
||
163 | * @param string $mime |
||
164 | */ |
||
165 | public function setMimeType(string $mime) |
||
166 | { |
||
167 | $this->mimeType = $mime; |
||
168 | } |
||
169 | 6 | ||
170 | /** |
||
171 | 6 | * Return the path to the backup file. |
|
172 | * |
||
173 | * @return Path |
||
174 | */ |
||
175 | public function getPath() : Path |
||
176 | { |
||
177 | return $this->path; |
||
178 | } |
||
179 | 10 | ||
180 | /** |
||
181 | 10 | * Return the name to the backup file. |
|
182 | * |
||
183 | * @param bool $plain |
||
184 | * @return string |
||
185 | */ |
||
186 | public function getFilename(bool $plain = false) : string |
||
187 | { |
||
188 | return $this->filename . $this->getFilenameSuffix($plain); |
||
189 | 11 | } |
|
190 | |||
191 | 11 | /** |
|
192 | 11 | * Return the name of the backup file without compressor or encryption suffix. |
|
193 | * |
||
194 | 11 | * @return string |
|
195 | 11 | */ |
|
196 | public function getFilenamePlain() : string |
||
197 | { |
||
198 | return $this->getFilename(true); |
||
199 | 11 | } |
|
200 | 11 | ||
201 | 11 | /** |
|
202 | * Return the raw name of the backup file incl. date placeholder. |
||
203 | * |
||
204 | 11 | * @param bool $plain |
|
205 | 11 | * @return string |
|
206 | */ |
||
207 | public function getFilenameRaw($plain = false) : string |
||
211 | |||
212 | /** |
||
213 | * Return custom file suffix like '.tar'. |
||
214 | * |
||
215 | * @param bool $plain |
||
216 | * @return string |
||
217 | 46 | */ |
|
218 | public function getFilenameSuffix($plain = false) : string |
||
219 | 46 | { |
|
220 | 46 | return $this->getSuffixToAppend() . ($plain ? '' : $this->getCompressionSuffix() . $this->getCrypterSuffix()); |
|
221 | 32 | } |
|
222 | 32 | ||
223 | /** |
||
224 | 46 | * Return added suffixes. |
|
225 | 46 | * |
|
226 | * @return string |
||
227 | */ |
||
228 | public function getSuffixToAppend() : string |
||
229 | { |
||
230 | return count($this->fileSuffixes) ? '.' . implode('.', $this->fileSuffixes) : ''; |
||
231 | } |
||
232 | 3 | ||
233 | /** |
||
234 | 3 | * Return the compressor suffix. |
|
235 | 3 | * |
|
236 | * @return string |
||
237 | */ |
||
238 | public function getCompressionSuffix() : string |
||
239 | { |
||
240 | return $this->shouldBeCompressed() ? '.' . $this->compression->getSuffix() : ''; |
||
241 | } |
||
242 | |||
243 | 4 | /** |
|
244 | * Return the crypter suffix. |
||
245 | * |
||
246 | 4 | * @return string |
|
247 | 3 | */ |
|
248 | 3 | public function getCrypterSuffix() : string |
|
249 | 3 | { |
|
250 | 3 | return $this->shouldBeEncrypted() ? '.' . $this->crypter->getSuffix() : ''; |
|
251 | 3 | } |
|
252 | 1 | ||
253 | /** |
||
254 | * Return file MIME type. |
||
255 | 3 | * |
|
256 | 1 | * @return string |
|
257 | */ |
||
258 | 2 | public function getMimeType() : string |
|
259 | { |
||
260 | $mimeType = $this->mimeType; |
||
261 | if ($this->shouldBeCompressed()) { |
||
262 | $mimeType = $this->compression->getMimeType(); |
||
263 | } |
||
264 | return $mimeType; |
||
265 | 1 | } |
|
266 | |||
267 | 1 | /** |
|
268 | 1 | * Size setter. |
|
269 | * |
||
270 | * @param int $size |
||
271 | */ |
||
272 | public function setSize(int $size) |
||
273 | { |
||
274 | $this->size = $size; |
||
275 | 3 | } |
|
276 | |||
277 | 3 | /** |
|
278 | * Return the actual file size in bytes. |
||
279 | * |
||
280 | * @return int |
||
281 | * @throws \phpbu\App\Exception |
||
282 | */ |
||
283 | public function getSize() : int |
||
284 | { |
||
285 | 1 | if (null === $this->size) { |
|
286 | if (!file_exists($this)) { |
||
287 | 1 | throw new Exception(sprintf('target file \'%s\' doesn\'t exist', $this->getFilename())); |
|
288 | } |
||
289 | $this->size = filesize($this); |
||
290 | } |
||
291 | return $this->size; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Target file exists already. |
||
296 | 26 | * |
|
297 | * @param bool $plain |
||
298 | 26 | * @return bool |
|
299 | */ |
||
300 | public function fileExists(bool $plain = false) : bool |
||
301 | { |
||
302 | return file_exists($this->getPathname($plain)); |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | 1 | * Return as backup file object. |
|
307 | * |
||
308 | 1 | * @return \phpbu\App\Backup\File\Local |
|
309 | */ |
||
310 | public function toFile() : Local |
||
311 | { |
||
312 | return new Local(new \SplFileInfo($this->getPathname())); |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Return path and filename of the backup file. |
||
317 | 14 | * |
|
318 | * @param bool $plain |
||
319 | 14 | * @return string |
|
320 | */ |
||
321 | public function getPathname(bool $plain = false) : string |
||
322 | { |
||
323 | return $this->path->getPath() . DIRECTORY_SEPARATOR . $this->getFilename($plain); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Return path and plain filename of the backup file. |
||
328 | 26 | * |
|
329 | * @return string |
||
330 | 26 | */ |
|
331 | public function getPathnamePlain() : string |
||
332 | { |
||
333 | return $this->getPathname(true); |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Filename configured with any date placeholders. |
||
338 | 26 | * |
|
339 | * @return bool |
||
340 | 26 | */ |
|
341 | public function hasChangingFilename() : bool |
||
342 | { |
||
343 | return $this->filenameIsChanging; |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Disable file compression. |
||
348 | 24 | */ |
|
349 | public function disableCompression() |
||
350 | 24 | { |
|
351 | $this->compress = false; |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Enable file compression. |
||
356 | * |
||
357 | * @throws \phpbu\App\Exception |
||
358 | 24 | */ |
|
359 | public function enableCompression() |
||
360 | 24 | { |
|
361 | if (null == $this->compression) { |
||
362 | throw new Exception('can\'t enable compression without a compressor'); |
||
363 | } |
||
364 | $this->compress = true; |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | 3 | * Compression setter. |
|
369 | * |
||
370 | 3 | * @param \phpbu\App\Backup\Target\Compression $compression |
|
371 | 3 | */ |
|
372 | 1 | public function setCompression(Target\Compression $compression) |
|
373 | { |
||
374 | 3 | $this->compression = $compression; |
|
375 | $this->compress = true; |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Compressor getter. |
||
380 | * |
||
381 | * @return \phpbu\App\Backup\Target\Compression |
||
382 | 1 | */ |
|
383 | public function getCompression() : Target\Compression |
||
387 | |||
388 | /** |
||
389 | * Is a compressor set? |
||
390 | * |
||
391 | * @return bool |
||
392 | */ |
||
393 | 3 | public function shouldBeCompressed() : bool |
|
394 | { |
||
395 | 3 | return $this->compress !== false; |
|
396 | 2 | } |
|
397 | 1 | ||
398 | /** |
||
399 | 1 | * Crypter setter. |
|
400 | * |
||
401 | 2 | * @param \phpbu\App\Backup\Crypter $crypter |
|
402 | */ |
||
403 | public function setCrypter(Crypter $crypter) |
||
404 | { |
||
405 | $this->crypter = $crypter; |
||
406 | $this->crypt = true; |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | 1 | * Crypter getter. |
|
411 | * |
||
412 | 1 | * @return \phpbu\App\Backup\Crypter |
|
413 | */ |
||
414 | public function getCrypter() : Crypter |
||
418 | |||
419 | /** |
||
420 | 1 | * Disable file encryption. |
|
421 | */ |
||
422 | 1 | public function disableEncryption() |
|
426 | |||
427 | /** |
||
428 | * Is a crypter set? |
||
429 | * |
||
430 | * @return bool |
||
431 | 16 | */ |
|
432 | public function shouldBeEncrypted() : bool |
||
436 | |||
437 | /** |
||
438 | * Magic to string method. |
||
439 | * |
||
440 | * @return string |
||
441 | 1 | */ |
|
442 | public function __toString() : string |
||
446 | } |
||
447 |