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 |
||
| 26 | class PrinterCli implements Listener |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Verbose output |
||
| 30 | * |
||
| 31 | * @var boolean |
||
| 32 | */ |
||
| 33 | protected $verbose; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Output with colors |
||
| 37 | * |
||
| 38 | * @var boolean |
||
| 39 | */ |
||
| 40 | protected $colors; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Is debug active |
||
| 44 | * |
||
| 45 | * @var boolean |
||
| 46 | */ |
||
| 47 | protected $debug; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Amount of executed backups |
||
| 51 | * |
||
| 52 | * @var integer |
||
| 53 | */ |
||
| 54 | private $numBackups = 0; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Amount of executed checks |
||
| 58 | * |
||
| 59 | * @var integer |
||
| 60 | */ |
||
| 61 | private $numChecks = 0; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Amount of executed crypts |
||
| 65 | * |
||
| 66 | * @var integer |
||
| 67 | */ |
||
| 68 | private $numCrypts = 0; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Amount of executed Syncs |
||
| 72 | * |
||
| 73 | * @var integer |
||
| 74 | */ |
||
| 75 | private $numSyncs = 0; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Amount of executed Cleanups |
||
| 79 | * |
||
| 80 | * @var integer |
||
| 81 | */ |
||
| 82 | private $numCleanups = 0; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Returns an array of event names this subscriber wants to listen to. |
||
| 86 | * |
||
| 87 | * The array keys are event names and the value can be: |
||
| 88 | * |
||
| 89 | * * The method name to call (priority defaults to 0) |
||
| 90 | * * An array composed of the method name to call and the priority |
||
| 91 | * * An array of arrays composed of the method names to call and respective |
||
| 92 | * priorities, or 0 if unset |
||
| 93 | * |
||
| 94 | * @return array The event names to listen to |
||
| 95 | */ |
||
| 96 | public static function getSubscribedEvents() |
||
| 122 | 2 | ||
| 123 | 2 | /** |
|
| 124 | 2 | * Constructor |
|
| 125 | 2 | * |
|
| 126 | 2 | * @param boolean $verbose |
|
| 127 | 2 | * @param boolean $colors |
|
| 128 | 2 | * @param boolean $debug |
|
| 129 | 2 | * @throws \InvalidArgumentException |
|
| 130 | 2 | */ |
|
| 131 | 2 | public function __construct($verbose = false, $colors = false, $debug = false) |
|
| 148 | 30 | ||
| 149 | /** |
||
| 150 | 30 | * phpbu start event. |
|
| 151 | 29 | * |
|
| 152 | 29 | * @param \phpbu\App\Event\App\Start $event |
|
| 153 | 1 | */ |
|
| 154 | public function onPhpbuStart(Event\App\Start $event) |
||
| 164 | 27 | ||
| 165 | 27 | /** |
|
| 166 | 1 | * Backup start event. |
|
| 167 | * |
||
| 168 | 27 | * @param \phpbu\App\Event\Backup\Start $event |
|
| 169 | */ |
||
| 170 | public function onBackupStart(Event\Backup\Start $event) |
||
| 171 | { |
||
| 172 | $backup = $event->getConfiguration(); |
||
| 173 | $this->numBackups++; |
||
| 174 | if ($this->debug) { |
||
| 175 | 1 | $this->writeWithAsterisk('backup: [' . $backup->getSource()->type . '] '); |
|
| 176 | } |
||
| 177 | 1 | } |
|
| 178 | 1 | ||
| 179 | 1 | /** |
|
| 180 | 1 | * Backup failed event. |
|
| 181 | 1 | * |
|
| 182 | * @param \phpbu\App\Event\Backup\Failed $event |
||
| 183 | 1 | */ |
|
| 184 | 1 | public function onBackupFailed(Event\Backup\Failed $event) |
|
| 190 | |||
| 191 | 3 | /** |
|
| 192 | * Backup end event. |
||
| 193 | 3 | * |
|
| 194 | 3 | * @param \phpbu\App\Event\Backup\End $event |
|
| 195 | 3 | */ |
|
| 196 | 2 | public function onBackupEnd(Event\Backup\End $event) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Check start event. |
||
| 205 | 1 | * |
|
| 206 | * @param \phpbu\App\Event\Check\Start $event |
||
| 207 | 1 | */ |
|
| 208 | 1 | public function onCheckStart(Event\Check\Start $event) |
|
| 209 | 1 | { |
|
| 210 | $check = $event->getConfiguration(); |
||
| 211 | 1 | $this->numChecks++; |
|
| 212 | 1 | if ($this->debug) { |
|
| 213 | 1 | $this->writeWithAsterisk('check: [' . $check->type . '] '); |
|
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Check failed event. |
||
| 219 | * |
||
| 220 | 2 | * @param \phpbu\App\Event\Check\Failed $event |
|
| 221 | */ |
||
| 222 | 2 | public function onCheckFailed(Event\Check\Failed $event) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Check end event. |
||
| 231 | * |
||
| 232 | 3 | * @param \phpbu\App\Event\Check\End $event |
|
| 233 | */ |
||
| 234 | 3 | public function onCheckEnd(Event\Check\End $event) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Crypt start event. |
||
| 243 | * |
||
| 244 | * @param \phpbu\App\Event\Crypt\Start $event |
||
| 245 | */ |
||
| 246 | 1 | public function onCryptStart(Event\Crypt\Start $event) |
|
| 247 | { |
||
| 248 | 1 | $crypt = $event->getConfiguration(); |
|
| 249 | 1 | $this->numCrypts++; |
|
| 250 | 1 | if ($this->debug) { |
|
| 251 | $this->writeWithAsterisk('crypt: [' . $crypt->type . '] '); |
||
| 252 | 1 | } |
|
| 253 | 1 | } |
|
| 254 | 1 | ||
| 255 | /** |
||
| 256 | * Crypt skipped event. |
||
| 257 | * |
||
| 258 | * @param \phpbu\App\Event\Crypt\Skipped $event |
||
| 259 | */ |
||
| 260 | public function onCryptSkipped(Event\Crypt\Skipped $event) |
||
| 266 | 2 | ||
| 267 | /** |
||
| 268 | * Crypt failed event. |
||
| 269 | * |
||
| 270 | * @param \phpbu\App\Event\Crypt\Failed $event |
||
| 271 | */ |
||
| 272 | public function onCryptFailed(Event\Crypt\Failed $event) |
||
| 278 | 3 | ||
| 279 | 3 | /** |
|
| 280 | 4 | * Crypt end event. |
|
| 281 | * |
||
| 282 | * @param \phpbu\App\Event\Crypt\End $event |
||
| 283 | */ |
||
| 284 | public function onCryptEnd(Event\Crypt\End $event) |
||
| 290 | 1 | ||
| 291 | 1 | /** |
|
| 292 | * Sync start event. |
||
| 293 | 1 | * |
|
| 294 | 1 | * @param \phpbu\App\Event\Sync\Start $event |
|
| 295 | 1 | */ |
|
| 296 | public function onSyncStart(Event\Sync\Start $event) |
||
| 297 | { |
||
| 298 | $sync = $event->getConfiguration(); |
||
| 299 | $this->numSyncs++; |
||
| 300 | if ($this->debug) { |
||
| 301 | $this->writeWithAsterisk('sync: [' . $sync->type . '] '); |
||
| 302 | 1 | } |
|
| 303 | } |
||
| 304 | 1 | ||
| 305 | 1 | /** |
|
| 306 | 1 | * Sync skipped event. |
|
| 307 | * |
||
| 308 | 1 | * @param \phpbu\App\Event\Sync\Skipped $event |
|
| 309 | 1 | */ |
|
| 310 | 1 | public function onSyncSkipped(Event\Sync\Skipped $event) |
|
| 316 | |||
| 317 | 2 | /** |
|
| 318 | * Sync failed event. |
||
| 319 | 2 | * |
|
| 320 | 1 | * @param \phpbu\App\Event\Sync\Failed $event |
|
| 321 | 1 | */ |
|
| 322 | 2 | public function onSyncFailed(Event\Sync\Failed $event) |
|
| 328 | |||
| 329 | 4 | /** |
|
| 330 | * Sync end event. |
||
| 331 | 4 | * |
|
| 332 | 4 | * @param \phpbu\App\Event\Sync\End $event |
|
| 333 | 4 | */ |
|
| 334 | 3 | public function onSyncEnd(Event\Sync\End $event) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Cleanup start event. |
||
| 343 | 1 | * |
|
| 344 | * @param \phpbu\App\Event\Cleanup\Start $event |
||
| 345 | 1 | */ |
|
| 346 | 1 | public function onCleanupStart(Event\Cleanup\Start $event) |
|
| 347 | 1 | { |
|
| 348 | $cleanup = $event->getConfiguration(); |
||
| 349 | 1 | $this->numCleanups++; |
|
| 350 | 1 | if ($this->debug) { |
|
| 351 | 1 | $this->writeWithAsterisk('cleanup: [' . $cleanup->type . '] '); |
|
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Cleanup skipped event. |
||
| 357 | * |
||
| 358 | 1 | * @param \phpbu\App\Event\Cleanup\Skipped $event |
|
| 359 | */ |
||
| 360 | 1 | public function onCleanupSkipped(Event\Cleanup\Skipped $event) |
|
| 366 | 1 | ||
| 367 | /** |
||
| 368 | * Cleanup failed event. |
||
| 369 | * |
||
| 370 | * @param \phpbu\App\Event\Cleanup\Failed $event |
||
| 371 | */ |
||
| 372 | public function onCleanupFailed(Event\Cleanup\Failed $event) |
||
| 378 | 2 | ||
| 379 | /** |
||
| 380 | * Cleanup end event. |
||
| 381 | * |
||
| 382 | * @param \phpbu\App\Event\Cleanup\End $event |
||
| 383 | */ |
||
| 384 | public function onCleanupEnd(Event\Cleanup\End $event) |
||
| 390 | 3 | ||
| 391 | 3 | /** |
|
| 392 | 4 | * Debugging. |
|
| 393 | * |
||
| 394 | * @param \phpbu\App\Event\Debug $event |
||
| 395 | */ |
||
| 396 | public function onDebug(Event\Debug $event) |
||
| 402 | 1 | ||
| 403 | 1 | /** |
|
| 404 | * phpbu end event. |
||
| 405 | 1 | * |
|
| 406 | 1 | * @param \phpbu\App\Event\App\End $event |
|
| 407 | 1 | */ |
|
| 408 | public function onPhpbuEnd(Event\App\End $event) |
||
| 413 | |||
| 414 | 1 | /** |
|
| 415 | * Prints a result summary. |
||
| 416 | 1 | * |
|
| 417 | 1 | * @param \phpbu\App\Result $result |
|
| 418 | 1 | */ |
|
| 419 | public function printResult(Result $result) |
||
| 431 | 2 | ||
| 432 | 1 | /** |
|
| 433 | 1 | * Prints the result header with memory usage info. |
|
| 434 | 2 | */ |
|
| 435 | protected function printHeader() |
||
| 439 | |||
| 440 | /** |
||
| 441 | 1 | * Print error information. |
|
| 442 | * |
||
| 443 | 1 | * @param \phpbu\App\Result $result |
|
| 444 | 1 | */ |
|
| 445 | 1 | protected function printErrors(Result $result) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Prints verbose backup information. |
||
| 463 | * |
||
| 464 | 5 | * @param \phpbu\App\Result\Backup $backup |
|
| 465 | */ |
||
| 466 | 5 | protected function printBackupVerbose(Result\Backup $backup) |
|
| 507 | |||
| 508 | /** |
||
| 509 | * Prints 'OK' or 'FAILURE' footer. |
||
| 510 | * |
||
| 511 | 3 | * @param Result $result |
|
| 512 | */ |
||
| 513 | 3 | protected function printFooter(Result $result) |
|
| 567 | 1 | ||
| 568 | 1 | /** |
|
| 569 | 1 | * Writes a buffer out with a color sequence if colors are enabled. |
|
| 570 | 1 | * |
|
| 571 | 1 | * @author Sebastian Bergmann <[email protected]> |
|
| 572 | 1 | * @param string $color |
|
| 573 | 1 | * @param string $buffer |
|
| 574 | 1 | */ |
|
| 575 | 1 | protected function writeWithColor($color, $buffer) |
|
| 582 | 3 | ||
| 583 | 1 | /** |
|
| 584 | 1 | * Writes a buffer with Ansible like asterisk decoration. |
|
| 585 | 1 | * |
|
| 586 | * @param string $buffer |
||
| 587 | 1 | */ |
|
| 588 | 1 | protected function writeWithAsterisk($buffer) |
|
| 592 | 1 | ||
| 593 | 1 | /** |
|
| 594 | 1 | * Writes a buffer. |
|
| 595 | 1 | * |
|
| 596 | 1 | * @param string $buffer |
|
| 597 | 1 | */ |
|
| 598 | 1 | public function write($buffer) |
|
| 605 | } |
||
| 606 |