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 | 2 | public static function getSubscribedEvents() |
|
112 | { |
||
113 | return [ |
||
114 | 2 | 'phpbu.debug' => 'onDebug', |
|
115 | 'phpbu.app_start' => 'onPhpbuStart', |
||
116 | 'phpbu.backup_start' => 'onBackupStart', |
||
117 | 'phpbu.backup_failed' => 'onBackupFailed', |
||
118 | 'phpbu.backup_end' => 'onBackupEnd', |
||
119 | 'phpbu.check_start' => 'onCheckStart', |
||
120 | 'phpbu.check_failed' => 'onCheckFailed', |
||
121 | 'phpbu.check_end' => 'onCheckEnd', |
||
122 | 'phpbu.crypt_start' => 'onCryptStart', |
||
123 | 'phpbu.crypt_skipped' => 'onCryptSkipped', |
||
124 | 'phpbu.crypt_failed' => 'onCryptFailed', |
||
125 | 'phpbu.crypt_end' => 'onCryptEnd', |
||
126 | 'phpbu.sync_start' => 'onSyncStart', |
||
127 | 'phpbu.sync_skipped' => 'onSyncSkipped', |
||
128 | 'phpbu.sync_failed' => 'onSyncFailed', |
||
129 | 'phpbu.sync_end' => 'onSyncEnd', |
||
130 | 'phpbu.cleanup_start' => 'onCleanupStart', |
||
131 | 'phpbu.cleanup_skipped' => 'onCleanupSkipped', |
||
132 | 'phpbu.cleanup_failed' => 'onCleanupFailed', |
||
133 | 'phpbu.cleanup_end' => 'onCleanupEnd', |
||
134 | 'phpbu.app_end' => 'onPhpbuEnd', |
||
135 | ]; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Constructor |
||
140 | * |
||
141 | * @param bool $verbose |
||
142 | * @param bool $colors |
||
143 | * @param bool $debug |
||
144 | * @throws \InvalidArgumentException |
||
145 | */ |
||
146 | 28 | public function __construct(bool $verbose = false, bool $colors = false, bool $debug = false) |
|
147 | { |
||
148 | 28 | $this->console = new Console; |
|
149 | 28 | $this->runtime = new Runtime; |
|
150 | 28 | $this->debug = $debug; |
|
151 | 28 | $this->verbose = $verbose; |
|
152 | 28 | $this->colors = $colors && $this->console->hasColorSupport(); |
|
153 | 28 | } |
|
154 | |||
155 | /** |
||
156 | * phpbu start event. |
||
157 | * |
||
158 | * @param \phpbu\App\Event\App\Start $event |
||
159 | */ |
||
160 | 2 | public function onPhpbuStart(Event\App\Start $event) |
|
161 | { |
||
162 | 2 | $configuration = $event->getConfiguration(); |
|
163 | 2 | if ($this->verbose) { |
|
164 | 1 | $this->write( |
|
165 | 1 | 'Runtime: ' . $this->runtime->getNameWithVersion() . PHP_EOL . |
|
166 | 1 | 'Configuration: ' . $configuration->getFilename() . PHP_EOL . |
|
167 | 1 | PHP_EOL |
|
168 | ); |
||
169 | } |
||
170 | 2 | } |
|
171 | |||
172 | /** |
||
173 | * Backup start event. |
||
174 | * |
||
175 | * @param \phpbu\App\Event\Backup\Start $event |
||
176 | */ |
||
177 | 3 | public function onBackupStart(Event\Backup\Start $event) |
|
178 | { |
||
179 | 3 | $this->numBackups++; |
|
180 | 3 | if ($this->debug) { |
|
181 | 2 | $backup = $event->getConfiguration(); |
|
182 | 2 | $this->writeWithAsterisk('backup: [' . $backup->getSource()->type . '] '); |
|
183 | } |
||
184 | 3 | } |
|
185 | |||
186 | /** |
||
187 | * Backup failed event. |
||
188 | * |
||
189 | * @param \phpbu\App\Event\Backup\Failed $event |
||
190 | */ |
||
191 | 1 | public function onBackupFailed(Event\Backup\Failed $event) |
|
192 | { |
||
193 | 1 | if ($this->debug) { |
|
194 | 1 | $this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL); |
|
195 | } |
||
196 | 1 | } |
|
197 | |||
198 | /** |
||
199 | * Backup end event. |
||
200 | * |
||
201 | * @param \phpbu\App\Event\Backup\End $event |
||
202 | */ |
||
203 | 2 | public function onBackupEnd(Event\Backup\End $event) |
|
204 | { |
||
205 | 2 | if ($this->debug) { |
|
206 | 1 | $this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL); |
|
207 | } |
||
208 | 2 | } |
|
209 | |||
210 | /** |
||
211 | * Check start event. |
||
212 | * |
||
213 | * @param \phpbu\App\Event\Check\Start $event |
||
214 | */ |
||
215 | 3 | public function onCheckStart(Event\Check\Start $event) |
|
216 | { |
||
217 | 3 | $this->numChecks++; |
|
218 | 3 | if ($this->debug) { |
|
219 | 2 | $check = $event->getConfiguration(); |
|
220 | 2 | $this->writeWithAsterisk('check: [' . $check->type . '] '); |
|
221 | 2 | $this->write('checking: [' . $check->value . '] '); |
|
222 | } |
||
223 | 3 | } |
|
224 | |||
225 | /** |
||
226 | * Check failed event. |
||
227 | * |
||
228 | * @param \phpbu\App\Event\Check\Failed $event |
||
229 | */ |
||
230 | 1 | public function onCheckFailed(Event\Check\Failed $event) |
|
231 | { |
||
232 | 1 | if ($this->debug) { |
|
233 | 1 | $this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL); |
|
234 | } |
||
235 | 1 | } |
|
236 | |||
237 | /** |
||
238 | * Check end event. |
||
239 | * |
||
240 | * @param \phpbu\App\Event\Check\End $event |
||
241 | */ |
||
242 | 2 | public function onCheckEnd(Event\Check\End $event) |
|
243 | { |
||
244 | 2 | if ($this->debug) { |
|
245 | 1 | $this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL); |
|
246 | } |
||
247 | 2 | } |
|
248 | |||
249 | /** |
||
250 | * Crypt start event. |
||
251 | * |
||
252 | * @param \phpbu\App\Event\Crypt\Start $event |
||
253 | */ |
||
254 | 4 | public function onCryptStart(Event\Crypt\Start $event) |
|
255 | { |
||
256 | 4 | $this->numCrypts++; |
|
257 | 4 | if ($this->debug) { |
|
258 | 3 | $crypt = $event->getConfiguration(); |
|
259 | 3 | $this->writeWithAsterisk('crypt: [' . $crypt->type . '] '); |
|
260 | } |
||
261 | 4 | } |
|
262 | |||
263 | /** |
||
264 | * Crypt skipped event. |
||
265 | * |
||
266 | * @param \phpbu\App\Event\Crypt\Skipped $event |
||
267 | */ |
||
268 | 1 | public function onCryptSkipped(Event\Crypt\Skipped $event) |
|
269 | { |
||
270 | 1 | if ($this->debug) { |
|
271 | 1 | $this->writeWithColor('fg-black, bg-yellow', 'skipped' . PHP_EOL); |
|
272 | } |
||
273 | 1 | } |
|
274 | |||
275 | /** |
||
276 | * Crypt failed event. |
||
277 | * |
||
278 | * @param \phpbu\App\Event\Crypt\Failed $event |
||
279 | */ |
||
280 | 1 | public function onCryptFailed(Event\Crypt\Failed $event) |
|
281 | { |
||
282 | 1 | if ($this->debug) { |
|
283 | 1 | $this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL); |
|
284 | } |
||
285 | 1 | } |
|
286 | |||
287 | /** |
||
288 | * Crypt end event. |
||
289 | * |
||
290 | * @param \phpbu\App\Event\Crypt\End $event |
||
291 | */ |
||
292 | 2 | public function onCryptEnd(Event\Crypt\End $event) |
|
293 | { |
||
294 | 2 | if ($this->debug) { |
|
295 | 1 | $this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL); |
|
296 | } |
||
297 | 2 | } |
|
298 | |||
299 | /** |
||
300 | * Sync start event. |
||
301 | * |
||
302 | * @param \phpbu\App\Event\Sync\Start $event |
||
303 | */ |
||
304 | 4 | public function onSyncStart(Event\Sync\Start $event) |
|
312 | |||
313 | /** |
||
314 | * Sync skipped event. |
||
315 | * |
||
316 | * @param \phpbu\App\Event\Sync\Skipped $event |
||
317 | */ |
||
318 | 1 | public function onSyncSkipped(Event\Sync\Skipped $event) |
|
324 | |||
325 | /** |
||
326 | * Sync failed event. |
||
327 | * |
||
328 | * @param \phpbu\App\Event\Sync\Failed $event |
||
329 | */ |
||
330 | 1 | public function onSyncFailed(Event\Sync\Failed $event) |
|
331 | { |
||
332 | 1 | if ($this->debug) { |
|
333 | 1 | $this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL); |
|
334 | } |
||
335 | 1 | } |
|
336 | |||
337 | /** |
||
338 | * Sync end event. |
||
339 | * |
||
340 | * @param \phpbu\App\Event\Sync\End $event |
||
341 | */ |
||
342 | 2 | public function onSyncEnd(Event\Sync\End $event) |
|
348 | |||
349 | /** |
||
350 | * Cleanup start event. |
||
351 | * |
||
352 | * @param \phpbu\App\Event\Cleanup\Start $event |
||
353 | */ |
||
354 | 4 | public function onCleanupStart(Event\Cleanup\Start $event) |
|
355 | { |
||
356 | 4 | $this->numCleanups++; |
|
357 | 4 | if ($this->debug) { |
|
358 | 3 | $cleanup = $event->getConfiguration(); |
|
359 | 3 | $this->writeWithAsterisk('cleanup: [' . $cleanup->type . '] '); |
|
360 | } |
||
361 | 4 | } |
|
362 | |||
363 | /** |
||
364 | * Cleanup skipped event. |
||
365 | * |
||
366 | * @param \phpbu\App\Event\Cleanup\Skipped $event |
||
367 | */ |
||
368 | 1 | public function onCleanupSkipped(Event\Cleanup\Skipped $event) |
|
369 | { |
||
370 | 1 | if ($this->debug) { |
|
371 | 1 | $this->writeWithColor('fg-black, bg-yellow', 'skipped' . PHP_EOL); |
|
372 | } |
||
373 | 1 | } |
|
374 | |||
375 | /** |
||
376 | * Cleanup failed event. |
||
377 | * |
||
378 | * @param \phpbu\App\Event\Cleanup\Failed $event |
||
379 | */ |
||
380 | 1 | public function onCleanupFailed(Event\Cleanup\Failed $event) |
|
381 | { |
||
382 | 1 | if ($this->debug) { |
|
383 | 1 | $this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL); |
|
384 | } |
||
385 | 1 | } |
|
386 | |||
387 | /** |
||
388 | * Cleanup end event. |
||
389 | * |
||
390 | * @param \phpbu\App\Event\Cleanup\End $event |
||
391 | */ |
||
392 | 2 | public function onCleanupEnd(Event\Cleanup\End $event) |
|
393 | { |
||
394 | 2 | if ($this->debug) { |
|
395 | 1 | $this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL); |
|
396 | } |
||
397 | 2 | } |
|
398 | |||
399 | /** |
||
400 | * Debugging. |
||
401 | * |
||
402 | * @param \phpbu\App\Event\Debug $event |
||
403 | */ |
||
404 | 1 | public function onDebug(Event\Debug $event) |
|
405 | { |
||
406 | 1 | if ($this->debug) { |
|
407 | 1 | $this->write($event->getMessage() . PHP_EOL); |
|
408 | } |
||
409 | 1 | } |
|
410 | |||
411 | /** |
||
412 | * phpbu end event. |
||
413 | * |
||
414 | * @param \phpbu\App\Event\App\End $event |
||
415 | */ |
||
416 | 1 | public function onPhpbuEnd(Event\App\End $event) |
|
421 | |||
422 | /** |
||
423 | * Prints a result summary. |
||
424 | * |
||
425 | * @param \phpbu\App\Result $result |
||
426 | */ |
||
427 | 5 | public function printResult(Result $result) |
|
428 | { |
||
429 | 5 | $this->printHeader(); |
|
430 | 5 | $this->printErrors($result); |
|
431 | |||
432 | 5 | if ($this->verbose) { |
|
433 | 3 | foreach ($result->getBackups() as $backup) { |
|
434 | 3 | $this->printBackupVerbose($backup); |
|
435 | } |
||
436 | } |
||
437 | 5 | $this->printFooter($result); |
|
438 | 5 | } |
|
439 | |||
440 | /** |
||
441 | * Prints the result header with memory usage info. |
||
442 | */ |
||
443 | 5 | protected function printHeader() |
|
447 | |||
448 | /** |
||
449 | * Print error information. |
||
450 | * |
||
451 | * @param \phpbu\App\Result $result |
||
452 | */ |
||
453 | 5 | protected function printErrors(Result $result) |
|
468 | |||
469 | /** |
||
470 | * Prints verbose backup information. |
||
471 | * |
||
472 | * @param \phpbu\App\Result\Backup $backup |
||
473 | */ |
||
474 | 3 | protected function printBackupVerbose(Result\Backup $backup) |
|
515 | |||
516 | /** |
||
517 | * Prints 'OK' or 'FAILURE' footer. |
||
518 | * |
||
519 | * @param Result $result |
||
520 | */ |
||
521 | 5 | protected function printFooter(Result $result) |
|
575 | |||
576 | /** |
||
577 | * Writes a buffer out with a color sequence if colors are enabled. |
||
578 | * |
||
579 | * @author Sebastian Bergmann <[email protected]> |
||
580 | * @param string $color |
||
581 | * @param string $buffer |
||
582 | */ |
||
583 | 18 | protected function writeWithColor($color, $buffer) |
|
590 | |||
591 | /** |
||
592 | * Writes a buffer with Ansible like asterisk decoration. |
||
593 | * |
||
594 | * @param string $buffer |
||
595 | */ |
||
596 | 13 | protected function writeWithAsterisk($buffer) |
|
600 | |||
601 | /** |
||
602 | * Writes a buffer. |
||
603 | * |
||
604 | * @param string $buffer |
||
605 | */ |
||
606 | 20 | public function write($buffer) |
|
613 | } |
||
614 |