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 |
||
| 27 | class PrinterCli implements Listener |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Verbose output |
||
| 31 | * |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | protected $verbose = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Output with colors |
||
| 38 | * |
||
| 39 | * @var bool |
||
| 40 | */ |
||
| 41 | protected $colors = false; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Is debug active |
||
| 45 | * |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | protected $debug = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Amount of executed backups |
||
| 52 | * |
||
| 53 | * @var integer |
||
| 54 | */ |
||
| 55 | private $numBackups = 0; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Amount of executed checks |
||
| 59 | * |
||
| 60 | * @var integer |
||
| 61 | */ |
||
| 62 | private $numChecks = 0; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Amount of executed crypts |
||
| 66 | * |
||
| 67 | * @var integer |
||
| 68 | */ |
||
| 69 | private $numCrypts = 0; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Amount of executed Syncs |
||
| 73 | * |
||
| 74 | * @var integer |
||
| 75 | */ |
||
| 76 | private $numSyncs = 0; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Amount of executed Cleanups |
||
| 80 | * |
||
| 81 | * @var integer |
||
| 82 | */ |
||
| 83 | private $numCleanups = 0; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Console |
||
| 87 | * |
||
| 88 | * @var \SebastianBergmann\Environment\Console |
||
| 89 | */ |
||
| 90 | private $console; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * PHP Runtime |
||
| 94 | * |
||
| 95 | * @var \SebastianBergmann\Environment\Runtime |
||
| 96 | */ |
||
| 97 | private $runtime; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Returns an array of event names this subscriber wants to listen to. |
||
| 101 | * |
||
| 102 | * The array keys are event names and the value can be: |
||
| 103 | * |
||
| 104 | * * The method name to call (priority defaults to 0) |
||
| 105 | * * An array composed of the method name to call and the priority |
||
| 106 | * * An array of arrays composed of the method names to call and respective |
||
| 107 | * priorities, or 0 if unset |
||
| 108 | * |
||
| 109 | * @return array The event names to listen to |
||
| 110 | */ |
||
| 111 | public static function getSubscribedEvents() |
||
| 137 | 2 | ||
| 138 | /** |
||
| 139 | * Constructor |
||
| 140 | * |
||
| 141 | * @param bool $verbose |
||
| 142 | * @param bool $colors |
||
| 143 | * @param bool $debug |
||
| 144 | * @throws \InvalidArgumentException |
||
| 145 | */ |
||
| 146 | public function __construct(bool $verbose = false, bool $colors = false, bool $debug = false) |
||
| 154 | |||
| 155 | /** |
||
| 156 | 29 | * phpbu start event. |
|
| 157 | 28 | * |
|
| 158 | 28 | * @param \phpbu\App\Event\App\Start $event |
|
| 159 | 28 | */ |
|
| 160 | 1 | public function onPhpbuStart(Event\App\Start $event) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Backup start event. |
||
| 174 | * |
||
| 175 | 1 | * @param \phpbu\App\Event\Backup\Start $event |
|
| 176 | */ |
||
| 177 | 1 | public function onBackupStart(Event\Backup\Start $event) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Backup failed event. |
||
| 188 | * |
||
| 189 | * @param \phpbu\App\Event\Backup\Failed $event |
||
| 190 | */ |
||
| 191 | 3 | public function onBackupFailed(Event\Backup\Failed $event) |
|
| 197 | 2 | ||
| 198 | 3 | /** |
|
| 199 | * Backup end event. |
||
| 200 | * |
||
| 201 | * @param \phpbu\App\Event\Backup\End $event |
||
| 202 | */ |
||
| 203 | public function onBackupEnd(Event\Backup\End $event) |
||
| 209 | 1 | ||
| 210 | /** |
||
| 211 | 1 | * Check start event. |
|
| 212 | 1 | * |
|
| 213 | 1 | * @param \phpbu\App\Event\Check\Start $event |
|
| 214 | */ |
||
| 215 | public function onCheckStart(Event\Check\Start $event) |
||
| 224 | 1 | ||
| 225 | 2 | /** |
|
| 226 | * Check failed event. |
||
| 227 | * |
||
| 228 | * @param \phpbu\App\Event\Check\Failed $event |
||
| 229 | */ |
||
| 230 | public function onCheckFailed(Event\Check\Failed $event) |
||
| 236 | 3 | ||
| 237 | 2 | /** |
|
| 238 | 2 | * Check end event. |
|
| 239 | 3 | * |
|
| 240 | * @param \phpbu\App\Event\Check\End $event |
||
| 241 | */ |
||
| 242 | public function onCheckEnd(Event\Check\End $event) |
||
| 248 | 1 | ||
| 249 | 1 | /** |
|
| 250 | 1 | * Crypt start event. |
|
| 251 | * |
||
| 252 | 1 | * @param \phpbu\App\Event\Crypt\Start $event |
|
| 253 | 1 | */ |
|
| 254 | 1 | public function onCryptStart(Event\Crypt\Start $event) |
|
| 262 | |||
| 263 | 2 | /** |
|
| 264 | 1 | * Crypt skipped event. |
|
| 265 | 1 | * |
|
| 266 | 2 | * @param \phpbu\App\Event\Crypt\Skipped $event |
|
| 267 | */ |
||
| 268 | public function onCryptSkipped(Event\Crypt\Skipped $event) |
||
| 274 | |||
| 275 | 4 | /** |
|
| 276 | 4 | * Crypt failed event. |
|
| 277 | 4 | * |
|
| 278 | 3 | * @param \phpbu\App\Event\Crypt\Failed $event |
|
| 279 | 3 | */ |
|
| 280 | 4 | public function onCryptFailed(Event\Crypt\Failed $event) |
|
| 286 | |||
| 287 | 1 | /** |
|
| 288 | * Crypt end event. |
||
| 289 | 1 | * |
|
| 290 | 1 | * @param \phpbu\App\Event\Crypt\End $event |
|
| 291 | 1 | */ |
|
| 292 | public function onCryptEnd(Event\Crypt\End $event) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Sync start event. |
||
| 301 | * |
||
| 302 | 1 | * @param \phpbu\App\Event\Sync\Start $event |
|
| 303 | */ |
||
| 304 | 1 | public function onSyncStart(Event\Sync\Start $event) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Sync skipped event. |
||
| 315 | * |
||
| 316 | * @param \phpbu\App\Event\Sync\Skipped $event |
||
| 317 | 2 | */ |
|
| 318 | public function onSyncSkipped(Event\Sync\Skipped $event) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Sync failed event. |
||
| 327 | * |
||
| 328 | * @param \phpbu\App\Event\Sync\Failed $event |
||
| 329 | 4 | */ |
|
| 330 | public function onSyncFailed(Event\Sync\Failed $event) |
||
| 336 | 4 | ||
| 337 | /** |
||
| 338 | * Sync end event. |
||
| 339 | * |
||
| 340 | * @param \phpbu\App\Event\Sync\End $event |
||
| 341 | */ |
||
| 342 | public function onSyncEnd(Event\Sync\End $event) |
||
| 348 | |||
| 349 | 1 | /** |
|
| 350 | 1 | * Cleanup start event. |
|
| 351 | 1 | * |
|
| 352 | * @param \phpbu\App\Event\Cleanup\Start $event |
||
| 353 | */ |
||
| 354 | public function onCleanupStart(Event\Cleanup\Start $event) |
||
| 362 | 1 | ||
| 363 | /** |
||
| 364 | 1 | * Cleanup skipped event. |
|
| 365 | 1 | * |
|
| 366 | 1 | * @param \phpbu\App\Event\Cleanup\Skipped $event |
|
| 367 | */ |
||
| 368 | public function onCleanupSkipped(Event\Cleanup\Skipped $event) |
||
| 374 | |||
| 375 | 2 | /** |
|
| 376 | 1 | * Cleanup failed event. |
|
| 377 | 1 | * |
|
| 378 | 2 | * @param \phpbu\App\Event\Cleanup\Failed $event |
|
| 379 | */ |
||
| 380 | public function onCleanupFailed(Event\Cleanup\Failed $event) |
||
| 386 | |||
| 387 | 4 | /** |
|
| 388 | 4 | * Cleanup end event. |
|
| 389 | 4 | * |
|
| 390 | 3 | * @param \phpbu\App\Event\Cleanup\End $event |
|
| 391 | 3 | */ |
|
| 392 | 4 | public function onCleanupEnd(Event\Cleanup\End $event) |
|
| 398 | |||
| 399 | 1 | /** |
|
| 400 | * Debugging. |
||
| 401 | 1 | * |
|
| 402 | 1 | * @param \phpbu\App\Event\Debug $event |
|
| 403 | 1 | */ |
|
| 404 | public function onDebug(Event\Debug $event) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * phpbu end event. |
||
| 413 | * |
||
| 414 | 1 | * @param \phpbu\App\Event\App\End $event |
|
| 415 | */ |
||
| 416 | 1 | public function onPhpbuEnd(Event\App\End $event) |
|
| 421 | 1 | ||
| 422 | 1 | /** |
|
| 423 | * Prints a result summary. |
||
| 424 | * |
||
| 425 | * @param \phpbu\App\Result $result |
||
| 426 | */ |
||
| 427 | public function printResult(Result $result) |
||
| 439 | |||
| 440 | /** |
||
| 441 | 1 | * Prints the result header with memory usage info. |
|
| 442 | */ |
||
| 443 | 1 | protected function printHeader() |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Print error information. |
||
| 450 | * |
||
| 451 | * @param \phpbu\App\Result $result |
||
| 452 | */ |
||
| 453 | 1 | protected function printErrors(Result $result) |
|
| 468 | |||
| 469 | 5 | /** |
|
| 470 | 3 | * Prints verbose backup information. |
|
| 471 | 3 | * |
|
| 472 | 3 | * @param \phpbu\App\Result\Backup $backup |
|
| 473 | 3 | */ |
|
| 474 | 5 | protected function printBackupVerbose(Result\Backup $backup) |
|
| 515 | 1 | ||
| 516 | 1 | /** |
|
| 517 | * Prints 'OK' or 'FAILURE' footer. |
||
| 518 | 1 | * |
|
| 519 | 3 | * @param Result $result |
|
| 520 | 1 | */ |
|
| 521 | 1 | protected function printFooter(Result $result) |
|
| 575 | 1 | ||
| 576 | 1 | /** |
|
| 577 | 1 | * Writes a buffer out with a color sequence if colors are enabled. |
|
| 578 | 1 | * |
|
| 579 | 1 | * @author Sebastian Bergmann <[email protected]> |
|
| 580 | 1 | * @param string $color |
|
| 581 | 1 | * @param string $buffer |
|
| 582 | 3 | */ |
|
| 583 | 1 | protected function writeWithColor($color, $buffer) |
|
| 590 | 1 | ||
| 591 | 1 | /** |
|
| 592 | 1 | * Writes a buffer with Ansible like asterisk decoration. |
|
| 593 | 1 | * |
|
| 594 | 1 | * @param string $buffer |
|
| 595 | 1 | */ |
|
| 596 | 1 | protected function writeWithAsterisk($buffer) |
|
| 600 | 1 | ||
| 601 | /** |
||
| 602 | 1 | * Writes a buffer. |
|
| 603 | 1 | * |
|
| 604 | 1 | * @param string $buffer |
|
| 605 | 1 | */ |
|
| 606 | 1 | public function write($buffer) |
|
| 613 | } |
||
| 614 |