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) |
|
| 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) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Filename setter. |
||
| 167 | * |
||
| 168 | * @param string $file |
||
| 169 | * @param integer $time |
||
| 170 | */ |
||
| 171 | 34 | public function setFile($file, $time = null) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Append another suffix to the filename. |
||
| 183 | * |
||
| 184 | * @param string $suffix |
||
| 185 | */ |
||
| 186 | public function appendFileSuffix($suffix) |
||
| 190 | 4 | ||
| 191 | 3 | /** |
|
| 192 | 3 | * Checks if the backup target directory is writable. |
|
| 193 | 3 | * Creates the Directory if it doesn't exist. |
|
| 194 | 3 | * |
|
| 195 | 3 | * @throws \phpbu\App\Exception |
|
| 196 | 1 | */ |
|
| 197 | public function setupPath() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Target file MIME type setter. |
||
| 216 | * |
||
| 217 | * @param string $mime |
||
| 218 | */ |
||
| 219 | 5 | public function setMimeType($mime) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Return the path to the backup file. |
||
| 226 | * |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | 1 | public function getPath() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Return the path to the backup file. |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function getPathRaw() |
||
| 243 | 18 | ||
| 244 | 16 | /** |
|
| 245 | 16 | * Return the name to the backup file. |
|
| 246 | 16 | * |
|
| 247 | 18 | * @param boolean $plain |
|
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | public function getFilename($plain = false) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Return the name of the backup file without compressor or encryption suffix. |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | 7 | public function getFilenamePlain() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Return the raw name of the backup file incl. date placeholder. |
||
| 272 | * |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | 3 | public function getFilenameRaw() |
|
| 279 | 1 | ||
| 280 | 1 | /** |
|
| 281 | 3 | * Return file MIME type. |
|
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | public function getMimeType() |
||
| 293 | 2 | ||
| 294 | 1 | /** |
|
| 295 | * Size setter. |
||
| 296 | 1 | * |
|
| 297 | 1 | * @param int $size |
|
| 298 | 1 | */ |
|
| 299 | public function setSize($size) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Return the actual filesize in bytes. |
||
| 306 | * |
||
| 307 | 4 | * @throws Exception |
|
| 308 | * @return integer |
||
| 309 | 4 | */ |
|
| 310 | public function getSize() |
||
| 320 | 3 | ||
| 321 | 1 | /** |
|
| 322 | * Target file exists already. |
||
| 323 | 2 | * |
|
| 324 | 1 | * @param boolean $plain |
|
| 325 | * @return boolean |
||
| 326 | 1 | */ |
|
| 327 | 1 | public function fileExists($plain = false) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Return as backup file object. |
||
| 334 | * |
||
| 335 | 14 | * @return \phpbu\App\Backup\File |
|
| 336 | */ |
||
| 337 | 14 | public function toFile() |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Deletes the target file. |
||
| 344 | * |
||
| 345 | * @param boolean $plain |
||
| 346 | * @throws \phpbu\App\Exception |
||
| 347 | 1 | */ |
|
| 348 | public function unlink($plain = false) |
||
| 358 | |||
| 359 | 2 | /** |
|
| 360 | * Return path and filename of the backup file. |
||
| 361 | * |
||
| 362 | * @param boolean $plain |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | public function getPathname($plain = false) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Return path and plain filename of the backup file. |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | 1 | public function getPathnamePlain() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Is dirname configured with any date placeholders. |
||
| 384 | * |
||
| 385 | * @return boolean |
||
| 386 | */ |
||
| 387 | 9 | public function hasChangingPath() |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Return the part of the path that is not changing. |
||
| 394 | * |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | 2 | public function getPathThatIsNotChanging() |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Changing path elements getter. |
||
| 404 | * |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | public function getChangingPathElements() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Return amount of changing path elements. |
||
| 414 | * |
||
| 415 | * @return integer |
||
| 416 | */ |
||
| 417 | public function countChangingPathElements() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Filename configured with any date placeholders. |
||
| 424 | * |
||
| 425 | * @return boolean |
||
| 426 | */ |
||
| 427 | public function hasChangingFilename() |
||
| 431 | 7 | ||
| 432 | 7 | /** |
|
| 433 | * Disable file compression. |
||
| 434 | */ |
||
| 435 | public function disableCompression() |
||
| 439 | 2 | ||
| 440 | /** |
||
| 441 | 2 | * Enable file compression. |
|
| 442 | * |
||
| 443 | * @throws \phpbu\App\Exception |
||
| 444 | */ |
||
| 445 | public function enableCompression() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Compressor setter. |
||
| 455 | * |
||
| 456 | * @param \phpbu\App\Backup\Compressor $compressor |
||
| 457 | */ |
||
| 458 | public function setCompressor(Compressor $compressor) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Compressor getter. |
||
| 466 | * |
||
| 467 | * @return \phpbu\App\Backup\Compressor |
||
| 468 | */ |
||
| 469 | 2 | public function getCompressor() |
|
| 473 | 2 | ||
| 474 | /** |
||
| 475 | * Is a compressor set? |
||
| 476 | * |
||
| 477 | * @return boolean |
||
| 478 | */ |
||
| 479 | public function shouldBeCompressed() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Is the target already compressed. |
||
| 486 | * |
||
| 487 | * @return boolean |
||
| 488 | */ |
||
| 489 | public function isCompressed() |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Crypter setter. |
||
| 496 | * |
||
| 497 | * @param \phpbu\App\Backup\Crypter $crypter |
||
| 498 | 16 | */ |
|
| 499 | public function setCrypter(Crypter $crypter) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Crypter getter. |
||
| 507 | * |
||
| 508 | 2 | * @return \phpbu\App\Backup\Crypter |
|
| 509 | */ |
||
| 510 | 2 | public function getCrypter() |
|
| 514 | |||
| 515 | /** |
||
| 516 | * Disable file encryption. |
||
| 517 | */ |
||
| 518 | public function disableEncryption() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Is a crypter set? |
||
| 525 | * |
||
| 526 | * @return boolean |
||
| 527 | */ |
||
| 528 | public function shouldBeEncrypted() |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Magic to string method. |
||
| 535 | * |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | public function __toString() |
||
| 542 | } |
||
| 543 |