Complex classes like Result 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 Result, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Result |
||
21 | { |
||
22 | /** |
||
23 | * Symfony EventDispatcher. |
||
24 | * |
||
25 | * @var \Symfony\Component\EventDispatcher\EventDispatcher |
||
26 | */ |
||
27 | protected $eventDispatcher; |
||
28 | |||
29 | /** |
||
30 | * List of executed Backups. |
||
31 | * |
||
32 | * @var array<\phpbu\App\Result\Backup> |
||
33 | */ |
||
34 | protected $backups = []; |
||
35 | |||
36 | /** |
||
37 | * Currently running backup. |
||
38 | * |
||
39 | * @var \phpbu\App\Result\Backup |
||
40 | */ |
||
41 | protected $backupActive; |
||
42 | |||
43 | /** |
||
44 | * List of errors. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $errors = []; |
||
49 | |||
50 | /** |
||
51 | * Start timestamp |
||
52 | * |
||
53 | * @var float |
||
54 | */ |
||
55 | protected $start; |
||
56 | |||
57 | /** |
||
58 | * @var integer |
||
59 | */ |
||
60 | protected $backupsFailed = 0; |
||
61 | |||
62 | /** |
||
63 | * @var integer |
||
64 | */ |
||
65 | protected $checksFailed = 0; |
||
66 | |||
67 | /** |
||
68 | * @var integer |
||
69 | */ |
||
70 | protected $cryptsSkipped = 0; |
||
71 | |||
72 | /** |
||
73 | * @var integer |
||
74 | */ |
||
75 | protected $cryptsFailed = 0; |
||
76 | |||
77 | /** |
||
78 | * @var integer |
||
79 | */ |
||
80 | protected $syncsSkipped = 0; |
||
81 | |||
82 | /** |
||
83 | * @var integer |
||
84 | */ |
||
85 | protected $syncsFailed = 0; |
||
86 | |||
87 | /** |
||
88 | * @var integer |
||
89 | */ |
||
90 | protected $cleanupsSkipped = 0; |
||
91 | |||
92 | /** |
||
93 | * @var integer |
||
94 | */ |
||
95 | protected $cleanupsFailed = 0; |
||
96 | |||
97 | /** |
||
98 | * Constructor |
||
99 | */ |
||
100 | 22 | public function __construct() |
|
105 | |||
106 | /** |
||
107 | * Registers a Listener/Subscriber. |
||
108 | * |
||
109 | * @param \phpbu\App\Listener |
||
110 | */ |
||
111 | 2 | public function addListener(Listener $subscriber) |
|
115 | |||
116 | /** |
||
117 | * No errors at all? |
||
118 | * |
||
119 | * @return boolean |
||
120 | */ |
||
121 | 7 | public function allOk() |
|
125 | |||
126 | /** |
||
127 | * Backup without errors, but some tasks where skipped or failed. |
||
128 | * |
||
129 | * @return boolean |
||
130 | */ |
||
131 | 5 | public function backupOkButSkipsOrFails() |
|
135 | |||
136 | /** |
||
137 | * Backup without errors? |
||
138 | * |
||
139 | * @return boolean |
||
140 | */ |
||
141 | 7 | public function wasSuccessful() |
|
145 | |||
146 | /** |
||
147 | * Nothing skipped? |
||
148 | * |
||
149 | * @return boolean |
||
150 | */ |
||
151 | 5 | public function noneSkipped() |
|
155 | |||
156 | /** |
||
157 | * Nothing failed? |
||
158 | * |
||
159 | * @return boolean |
||
160 | */ |
||
161 | 4 | public function noneFailed() |
|
165 | |||
166 | /** |
||
167 | * Add Exception to error list |
||
168 | * |
||
169 | * @param \Exception $e |
||
170 | */ |
||
171 | 8 | public function addError(\Exception $e) |
|
175 | |||
176 | /** |
||
177 | * Return current error count. |
||
178 | * |
||
179 | * @return integer |
||
180 | */ |
||
181 | 5 | public function errorCount() |
|
185 | |||
186 | /** |
||
187 | * Return list of errors. |
||
188 | * |
||
189 | * @return array<Exception> |
||
190 | */ |
||
191 | 5 | public function getErrors() |
|
195 | |||
196 | /** |
||
197 | * Return list of executed backups. |
||
198 | * |
||
199 | * @return array<\phpbu\App\Configuration\Backup> |
||
200 | */ |
||
201 | 5 | public function getBackups() |
|
205 | |||
206 | /** |
||
207 | * phpbu start event. |
||
208 | * |
||
209 | * @param \phpbu\App\Configuration $configuration |
||
210 | */ |
||
211 | 20 | public function phpbuStart(Configuration $configuration) |
|
216 | |||
217 | /** |
||
218 | * phpbu end event. |
||
219 | */ |
||
220 | 20 | public function phpbuEnd() |
|
225 | |||
226 | /** |
||
227 | * Return phpbu start micro time. |
||
228 | * |
||
229 | * @return float |
||
230 | */ |
||
231 | 1 | public function started() : float |
|
232 | { |
||
233 | 1 | return $this->start; |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * Backup start event. |
||
238 | * |
||
239 | * @param \phpbu\App\Configuration\Backup $backup |
||
240 | */ |
||
241 | 20 | public function backupStart(Configuration\Backup $backup) |
|
249 | |||
250 | /** |
||
251 | * Backup failed event. |
||
252 | * |
||
253 | * @param \phpbu\App\Configuration\Backup $backup |
||
254 | */ |
||
255 | 2 | public function backupFailed(Configuration\Backup $backup) |
|
263 | |||
264 | /** |
||
265 | * Return amount of failed backups |
||
266 | * |
||
267 | * @return int |
||
268 | */ |
||
269 | 3 | public function backupsFailedCount() : int |
|
273 | |||
274 | /** |
||
275 | * Backup end event. |
||
276 | * |
||
277 | * @param \phpbu\App\Configuration\Backup $backup |
||
278 | */ |
||
279 | 19 | public function backupEnd(Configuration\Backup $backup) |
|
284 | |||
285 | /** |
||
286 | * Check start event. |
||
287 | * |
||
288 | * @param \phpbu\App\Configuration\Backup\Check $check |
||
289 | */ |
||
290 | 16 | public function checkStart(Configuration\Backup\Check $check) |
|
297 | |||
298 | /** |
||
299 | * Check failed event. |
||
300 | * |
||
301 | * @param \phpbu\App\Configuration\Backup\Check $check |
||
302 | */ |
||
303 | 3 | public function checkFailed(Configuration\Backup\Check $check) |
|
317 | |||
318 | /** |
||
319 | * Return amount of failed checks. |
||
320 | * |
||
321 | * @return int |
||
322 | */ |
||
323 | 2 | public function checksFailedCount() : int |
|
327 | |||
328 | /** |
||
329 | * Check end event. |
||
330 | * |
||
331 | * @param \phpbu\App\Configuration\Backup\Check $check |
||
332 | */ |
||
333 | 13 | public function checkEnd(Configuration\Backup\Check $check) |
|
338 | |||
339 | /** |
||
340 | * Crypt start event. |
||
341 | * |
||
342 | * @param \phpbu\App\Configuration\Backup\Crypt $crypt |
||
343 | */ |
||
344 | 14 | public function cryptStart(Configuration\Backup\Crypt $crypt) |
|
351 | |||
352 | /** |
||
353 | * Crypt skipped event. |
||
354 | * |
||
355 | * @param \phpbu\App\Configuration\Backup\Crypt $crypt |
||
356 | */ |
||
357 | 3 | public function cryptSkipped(Configuration\Backup\Crypt $crypt) |
|
365 | |||
366 | /** |
||
367 | * Return amount of skipped crypts. |
||
368 | * |
||
369 | * @return int |
||
370 | */ |
||
371 | 1 | public function cryptsSkippedCount() : int |
|
375 | |||
376 | /** |
||
377 | * Crypt failed event. |
||
378 | * |
||
379 | * @param \phpbu\App\Configuration\Backup\Crypt $crypt |
||
380 | */ |
||
381 | 2 | public function cryptFailed(Configuration\Backup\Crypt $crypt) |
|
390 | |||
391 | /** |
||
392 | * Return amount of failed crypts. |
||
393 | * |
||
394 | * @return int |
||
395 | */ |
||
396 | 1 | public function cryptsFailedCount() : int |
|
400 | |||
401 | /** |
||
402 | * Crypt end event. |
||
403 | * |
||
404 | * @param \phpbu\App\Configuration\Backup\Crypt $crypt |
||
405 | */ |
||
406 | 10 | public function cryptEnd(Configuration\Backup\Crypt $crypt) |
|
411 | |||
412 | /** |
||
413 | * Sync start event. |
||
414 | * |
||
415 | * @param \phpbu\App\Configuration\Backup\Sync $sync |
||
416 | */ |
||
417 | 9 | public function syncStart(Configuration\Backup\Sync $sync) |
|
424 | |||
425 | /** |
||
426 | * Sync skipped event. |
||
427 | * |
||
428 | * @param \phpbu\App\Configuration\Backup\Sync $sync |
||
429 | */ |
||
430 | 4 | public function syncSkipped(Configuration\Backup\Sync $sync) |
|
438 | |||
439 | /** |
||
440 | * Return amount of skipped syncs. |
||
441 | * |
||
442 | * @return int |
||
443 | */ |
||
444 | 3 | public function syncsSkippedCount() : int |
|
448 | |||
449 | /** |
||
450 | * Sync failed event. |
||
451 | * |
||
452 | * @param \phpbu\App\Configuration\Backup\Sync $sync |
||
453 | */ |
||
454 | 2 | public function syncFailed(Configuration\Backup\Sync $sync) |
|
462 | |||
463 | /** |
||
464 | * Return amount of failed syncs. |
||
465 | * |
||
466 | * @return int |
||
467 | */ |
||
468 | 3 | public function syncsFailedCount() : int |
|
472 | |||
473 | /** |
||
474 | * Sync end event. |
||
475 | * |
||
476 | * @param \phpbu\App\Configuration\Backup\Sync $sync |
||
477 | */ |
||
478 | 4 | public function syncEnd(Configuration\Backup\Sync $sync) |
|
483 | |||
484 | /** |
||
485 | * Cleanup start event. |
||
486 | * |
||
487 | * @param \phpbu\App\Configuration\Backup\Cleanup $cleanup |
||
488 | */ |
||
489 | 14 | public function cleanupStart(Configuration\Backup\Cleanup $cleanup) |
|
496 | |||
497 | /** |
||
498 | * Cleanup skipped event. |
||
499 | * |
||
500 | * @param \phpbu\App\Configuration\Backup\Cleanup $cleanup |
||
501 | */ |
||
502 | 5 | public function cleanupSkipped(Configuration\Backup\Cleanup $cleanup) |
|
510 | |||
511 | /** |
||
512 | * Return amount of skipped cleanups. |
||
513 | * |
||
514 | * @return int |
||
515 | */ |
||
516 | 3 | public function cleanupsSkippedCount() : int |
|
520 | |||
521 | /** |
||
522 | * Cleanup failed event. |
||
523 | * |
||
524 | * @param \phpbu\App\Configuration\Backup\Cleanup $cleanup |
||
525 | */ |
||
526 | 2 | public function cleanupFailed(Configuration\Backup\Cleanup $cleanup) |
|
534 | |||
535 | /** |
||
536 | * Return amount of failed cleanups. |
||
537 | * |
||
538 | * @return int |
||
539 | */ |
||
540 | 3 | public function cleanupsFailedCount() : int |
|
544 | |||
545 | /** |
||
546 | * Cleanup end event. |
||
547 | * |
||
548 | * @param \phpbu\App\Configuration\Backup\Cleanup $cleanup |
||
549 | */ |
||
550 | 8 | public function cleanupEnd(Configuration\Backup\Cleanup $cleanup) |
|
555 | |||
556 | /** |
||
557 | * Debug. |
||
558 | * |
||
559 | * @param string $msg |
||
560 | */ |
||
561 | 7 | public function debug($msg) |
|
566 | } |
||
567 |