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 | use Path; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Backup filename. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $filename; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Filename with potential date placeholders like %d. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $filenameRaw; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * List of custom file suffixes f.e. 'tar' |
||
| 39 | * |
||
| 40 | * @var string[] |
||
| 41 | */ |
||
| 42 | private $fileSuffixes = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Indicates if the filename changes over time. |
||
| 46 | * |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | private $filenameIsChanging = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Target MIME type |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $mimeType = 'text/plain'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Size in bytes |
||
| 60 | * |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | private $size; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Should the file be compressed. |
||
| 67 | * |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | private $compress = false; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * File compression. |
||
| 74 | * |
||
| 75 | * @var \phpbu\App\Backup\Target\Compression |
||
| 76 | */ |
||
| 77 | private $compression; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Should the file be encrypted. |
||
| 81 | * |
||
| 82 | * @var bool |
||
| 83 | */ |
||
| 84 | private $crypt = false; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * File crypter. |
||
| 88 | * |
||
| 89 | * @var \phpbu\App\Backup\Crypter |
||
| 90 | */ |
||
| 91 | private $crypter; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Constructor. |
||
| 95 | * |
||
| 96 | * @param string $path |
||
| 97 | * @param string $filename |
||
| 98 | * @param integer $time |
||
| 99 | * @throws \phpbu\App\Exception |
||
| 100 | */ |
||
| 101 | 46 | public function __construct($path, $filename, $time = null) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Filename setter. |
||
| 109 | * |
||
| 110 | * @param string $file |
||
| 111 | * @param int $time |
||
| 112 | */ |
||
| 113 | 46 | public function setFile($file, $time = null) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Append another suffix to the filename. |
||
| 125 | * |
||
| 126 | * @param string $suffix |
||
| 127 | */ |
||
| 128 | 3 | public function appendFileSuffix(string $suffix) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Checks if the backup target directory is writable. |
||
| 135 | * Creates the Directory if it doesn't exist. |
||
| 136 | * |
||
| 137 | * @throws \phpbu\App\Exception |
||
| 138 | */ |
||
| 139 | 4 | public function setupPath() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Target file MIME type setter. |
||
| 158 | * |
||
| 159 | * @param string $mime |
||
| 160 | */ |
||
| 161 | 1 | public function setMimeType(string $mime) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Return the path to the backup file. |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | 3 | public function getPath() : string |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Return the path to the backup file. |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | 1 | public function getPathRaw() : string |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Return the name to the backup file. |
||
| 188 | * |
||
| 189 | * @param bool $plain |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | 26 | public function getFilename(bool $plain = false) : string |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Return the name of the backup file without compressor or encryption suffix. |
||
| 199 | * |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | 1 | public function getFilenamePlain() : string |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Return the raw name of the backup file incl. date placeholder. |
||
| 209 | * |
||
| 210 | * @param bool $plain |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | 14 | public function getFilenameRaw($plain = false) : string |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Return custom file suffix like '.tar'. |
||
| 220 | * |
||
| 221 | * @param bool $plain |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | 26 | public function getFilenameSuffix($plain = false) : string |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Return added suffixes. |
||
| 231 | * |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | 26 | public function getSuffixToAppend() : string |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Return the compressor suffix. |
||
| 241 | * |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | 24 | public function getCompressionSuffix() : string |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Return the crypter suffix. |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | 24 | public function getCrypterSuffix() : string |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Return file MIME type. |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | 3 | public function getMimeType() : string |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Size setter. |
||
| 275 | * |
||
| 276 | * @param int $size |
||
| 277 | */ |
||
| 278 | 1 | public function setSize(int $size) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Return the actual file size in bytes. |
||
| 285 | * |
||
| 286 | * @return int |
||
| 287 | * @throws \phpbu\App\Exception |
||
| 288 | */ |
||
| 289 | 3 | public function getSize() : int |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Target file exists already. |
||
| 302 | * |
||
| 303 | * @param bool $plain |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | 1 | public function fileExists(bool $plain = false) : bool |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Return as backup file object. |
||
| 313 | * |
||
| 314 | * @return \phpbu\App\Backup\File\Local |
||
| 315 | */ |
||
| 316 | 1 | public function toFile() : Local |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Return path and filename of the backup file. |
||
| 323 | * |
||
| 324 | * @param bool $plain |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | 16 | public function getPathname(bool $plain = false) : string |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Return path and plain filename of the backup file. |
||
| 334 | * |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | 1 | public function getPathnamePlain() : string |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Is dirname configured with any date placeholders. |
||
| 344 | * |
||
| 345 | * @return bool |
||
| 346 | */ |
||
| 347 | 3 | public function hasChangingPath() : bool |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Return the part of the path that is not changing. |
||
| 354 | * |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | 10 | public function getPathThatIsNotChanging() : string |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Filename configured with any date placeholders. |
||
| 364 | * |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | 2 | public function hasChangingFilename() : bool |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Disable file compression. |
||
| 374 | */ |
||
| 375 | 2 | public function disableCompression() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Enable file compression. |
||
| 382 | * |
||
| 383 | * @throws \phpbu\App\Exception |
||
| 384 | */ |
||
| 385 | 2 | public function enableCompression() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Compression setter. |
||
| 395 | * |
||
| 396 | * @param \phpbu\App\Backup\Target\Compression $compression |
||
| 397 | */ |
||
| 398 | 12 | public function setCompression(Target\Compression $compression) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Compressor getter. |
||
| 406 | * |
||
| 407 | * @return \phpbu\App\Backup\Target\Compression |
||
| 408 | */ |
||
| 409 | 1 | public function getCompression() : Target\Compression |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Is a compressor set? |
||
| 416 | * |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | 29 | public function shouldBeCompressed() : bool |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Crypter setter. |
||
| 426 | * |
||
| 427 | * @param \phpbu\App\Backup\Crypter $crypter |
||
| 428 | */ |
||
| 429 | 3 | public function setCrypter(Crypter $crypter) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Crypter getter. |
||
| 437 | * |
||
| 438 | * @return \phpbu\App\Backup\Crypter |
||
| 439 | */ |
||
| 440 | 1 | public function getCrypter() : Crypter |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Disable file encryption. |
||
| 447 | */ |
||
| 448 | 1 | public function disableEncryption() |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Is a crypter set? |
||
| 455 | * |
||
| 456 | * @return bool |
||
| 457 | */ |
||
| 458 | 25 | public function shouldBeEncrypted() : bool |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Magic to string method. |
||
| 465 | * |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | 2 | public function __toString() : string |
|
| 472 | } |
||
| 473 |