Complex classes like PrinterCli 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 PrinterCli, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class PrinterCli implements Listener |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Verbose output |
||
| 29 | * |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | protected $verbose = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Output with colors |
||
| 36 | * |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | protected $colors = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Is debug active |
||
| 43 | * |
||
| 44 | * @var bool |
||
| 45 | */ |
||
| 46 | protected $debug = false; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Amount of executed backups |
||
| 50 | * |
||
| 51 | * @var integer |
||
| 52 | */ |
||
| 53 | private $numBackups = 0; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Amount of executed checks |
||
| 57 | * |
||
| 58 | * @var integer |
||
| 59 | */ |
||
| 60 | private $numChecks = 0; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Amount of executed crypts |
||
| 64 | * |
||
| 65 | * @var integer |
||
| 66 | */ |
||
| 67 | private $numCrypts = 0; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Amount of executed Syncs |
||
| 71 | * |
||
| 72 | * @var integer |
||
| 73 | */ |
||
| 74 | private $numSyncs = 0; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Amount of executed Cleanups |
||
| 78 | * |
||
| 79 | * @var integer |
||
| 80 | */ |
||
| 81 | private $numCleanups = 0; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Console |
||
| 85 | * |
||
| 86 | * @var \SebastianBergmann\Environment\Console |
||
| 87 | */ |
||
| 88 | private $console; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * PHP Runtime |
||
| 92 | * |
||
| 93 | * @var \SebastianBergmann\Environment\Runtime |
||
| 94 | */ |
||
| 95 | private $runtime; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Returns an array of event names this subscriber wants to listen to. |
||
| 99 | * |
||
| 100 | * The array keys are event names and the value can be: |
||
| 101 | * |
||
| 102 | * * The method name to call (priority defaults to 0) |
||
| 103 | * * An array composed of the method name to call and the priority |
||
| 104 | * * An array of arrays composed of the method names to call and respective |
||
| 105 | * priorities, or 0 if unset |
||
| 106 | * |
||
| 107 | * @return array The event names to listen to |
||
| 108 | */ |
||
| 109 | 3 | public static function getSubscribedEvents() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Constructor |
||
| 138 | * |
||
| 139 | * @param bool $verbose |
||
| 140 | * @param bool $colors |
||
| 141 | * @param bool $debug |
||
| 142 | * @throws \InvalidArgumentException |
||
| 143 | */ |
||
| 144 | 29 | public function __construct(bool $verbose = false, bool $colors = false, bool $debug = false) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * phpbu start event. |
||
| 155 | * |
||
| 156 | * @param \phpbu\App\Event\App\Start $event |
||
| 157 | */ |
||
| 158 | 2 | public function onPhpbuStart(Event\App\Start $event) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Backup start event. |
||
| 172 | * |
||
| 173 | * @param \phpbu\App\Event\Backup\Start $event |
||
| 174 | */ |
||
| 175 | 3 | public function onBackupStart(Event\Backup\Start $event) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Backup failed event. |
||
| 186 | * |
||
| 187 | * @param \phpbu\App\Event\Backup\Failed $event |
||
| 188 | */ |
||
| 189 | 1 | public function onBackupFailed(Event\Backup\Failed $event) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Backup end event. |
||
| 198 | * |
||
| 199 | * @param \phpbu\App\Event\Backup\End $event |
||
| 200 | */ |
||
| 201 | 2 | public function onBackupEnd(Event\Backup\End $event) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Check start event. |
||
| 210 | * |
||
| 211 | * @param \phpbu\App\Event\Check\Start $event |
||
| 212 | */ |
||
| 213 | 3 | public function onCheckStart(Event\Check\Start $event) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Check failed event. |
||
| 225 | * |
||
| 226 | * @param \phpbu\App\Event\Check\Failed $event |
||
| 227 | */ |
||
| 228 | 1 | public function onCheckFailed(Event\Check\Failed $event) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Check end event. |
||
| 237 | * |
||
| 238 | * @param \phpbu\App\Event\Check\End $event |
||
| 239 | */ |
||
| 240 | 2 | public function onCheckEnd(Event\Check\End $event) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Crypt start event. |
||
| 249 | * |
||
| 250 | * @param \phpbu\App\Event\Crypt\Start $event |
||
| 251 | */ |
||
| 252 | 4 | public function onCryptStart(Event\Crypt\Start $event) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Crypt skipped event. |
||
| 263 | * |
||
| 264 | * @param \phpbu\App\Event\Crypt\Skipped $event |
||
| 265 | */ |
||
| 266 | 1 | public function onCryptSkipped(Event\Crypt\Skipped $event) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Crypt failed event. |
||
| 275 | * |
||
| 276 | * @param \phpbu\App\Event\Crypt\Failed $event |
||
| 277 | */ |
||
| 278 | 1 | public function onCryptFailed(Event\Crypt\Failed $event) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Crypt end event. |
||
| 287 | * |
||
| 288 | * @param \phpbu\App\Event\Crypt\End $event |
||
| 289 | */ |
||
| 290 | 2 | public function onCryptEnd(Event\Crypt\End $event) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Sync start event. |
||
| 299 | * |
||
| 300 | * @param \phpbu\App\Event\Sync\Start $event |
||
| 301 | */ |
||
| 302 | 4 | public function onSyncStart(Event\Sync\Start $event) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Sync skipped event. |
||
| 313 | * |
||
| 314 | * @param \phpbu\App\Event\Sync\Skipped $event |
||
| 315 | */ |
||
| 316 | 1 | public function onSyncSkipped(Event\Sync\Skipped $event) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Sync failed event. |
||
| 325 | * |
||
| 326 | * @param \phpbu\App\Event\Sync\Failed $event |
||
| 327 | */ |
||
| 328 | 1 | public function onSyncFailed(Event\Sync\Failed $event) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Sync end event. |
||
| 337 | * |
||
| 338 | * @param \phpbu\App\Event\Sync\End $event |
||
| 339 | */ |
||
| 340 | 2 | public function onSyncEnd(Event\Sync\End $event) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Cleanup start event. |
||
| 349 | * |
||
| 350 | * @param \phpbu\App\Event\Cleanup\Start $event |
||
| 351 | */ |
||
| 352 | 4 | public function onCleanupStart(Event\Cleanup\Start $event) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Cleanup skipped event. |
||
| 363 | * |
||
| 364 | * @param \phpbu\App\Event\Cleanup\Skipped $event |
||
| 365 | */ |
||
| 366 | 1 | public function onCleanupSkipped(Event\Cleanup\Skipped $event) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Cleanup failed event. |
||
| 375 | * |
||
| 376 | * @param \phpbu\App\Event\Cleanup\Failed $event |
||
| 377 | */ |
||
| 378 | 1 | public function onCleanupFailed(Event\Cleanup\Failed $event) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Cleanup end event. |
||
| 387 | * |
||
| 388 | * @param \phpbu\App\Event\Cleanup\End $event |
||
| 389 | */ |
||
| 390 | 2 | public function onCleanupEnd(Event\Cleanup\End $event) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Debugging. |
||
| 399 | * |
||
| 400 | * @param \phpbu\App\Event\Debug $event |
||
| 401 | */ |
||
| 402 | 1 | public function onDebug(Event\Debug $event) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * phpbu end event. |
||
| 411 | * |
||
| 412 | * @param \phpbu\App\Event\App\End $event |
||
| 413 | */ |
||
| 414 | 1 | public function onPhpbuEnd(Event\App\End $event) |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Prints a result summary. |
||
| 422 | * |
||
| 423 | * @param \phpbu\App\Result $result |
||
| 424 | */ |
||
| 425 | 5 | public function printResult(Result $result) |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Prints the result header with memory usage info. |
||
| 440 | */ |
||
| 441 | 5 | protected function printHeader() |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Print error information. |
||
| 448 | * |
||
| 449 | * @param \phpbu\App\Result $result |
||
| 450 | */ |
||
| 451 | 5 | protected function printErrors(Result $result) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Prints verbose backup information. |
||
| 469 | * |
||
| 470 | * @param \phpbu\App\Result\Backup $backup |
||
| 471 | */ |
||
| 472 | 3 | protected function printBackupVerbose(Result\Backup $backup) |
|
| 513 | |||
| 514 | /** |
||
| 515 | * Prints 'OK' or 'FAILURE' footer. |
||
| 516 | * |
||
| 517 | * @param Result $result |
||
| 518 | */ |
||
| 519 | 5 | protected function printFooter(Result $result) |
|
| 577 | |||
| 578 | /** |
||
| 579 | * Writes a buffer out with a color sequence if colors are enabled. |
||
| 580 | * |
||
| 581 | * @author Sebastian Bergmann <[email protected]> |
||
| 582 | * @param string $color |
||
| 583 | * @param string $buffer |
||
| 584 | */ |
||
| 585 | 18 | protected function writeWithColor($color, $buffer) |
|
| 592 | |||
| 593 | /** |
||
| 594 | * Writes a buffer with Ansible like asterisk decoration. |
||
| 595 | * |
||
| 596 | * @param string $buffer |
||
| 597 | */ |
||
| 598 | 13 | protected function writeWithAsterisk($buffer) |
|
| 602 | |||
| 603 | /** |
||
| 604 | * Writes a buffer. |
||
| 605 | * |
||
| 606 | * @param string $buffer |
||
| 607 | */ |
||
| 608 | 20 | public function write($buffer) |
|
| 615 | } |
||
| 616 |