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 | * @var integer |
||
52 | */ |
||
53 | protected $backupsFailed = 0; |
||
54 | |||
55 | /** |
||
56 | * @var integer |
||
57 | */ |
||
58 | protected $checksFailed = 0; |
||
59 | |||
60 | /** |
||
61 | * @var integer |
||
62 | */ |
||
63 | protected $cryptsSkipped = 0; |
||
64 | |||
65 | /** |
||
66 | * @var integer |
||
67 | */ |
||
68 | protected $cryptsFailed = 0; |
||
69 | |||
70 | /** |
||
71 | * @var integer |
||
72 | */ |
||
73 | protected $syncsSkipped = 0; |
||
74 | |||
75 | /** |
||
76 | * @var integer |
||
77 | */ |
||
78 | protected $syncsFailed = 0; |
||
79 | |||
80 | /** |
||
81 | * @var integer |
||
82 | */ |
||
83 | protected $cleanupsSkipped = 0; |
||
84 | |||
85 | /** |
||
86 | * @var integer |
||
87 | */ |
||
88 | protected $cleanupsFailed = 0; |
||
89 | |||
90 | /** |
||
91 | * Constructor |
||
92 | */ |
||
93 | 21 | public function __construct() |
|
94 | { |
||
95 | 21 | $this->eventDispatcher = new EventDispatcher(); |
|
96 | 21 | } |
|
97 | |||
98 | /** |
||
99 | * Registers a Listener/Subscriber. |
||
100 | * |
||
101 | * @param \phpbu\App\Listener |
||
102 | */ |
||
103 | 1 | public function addListener(Listener $subscriber) |
|
104 | { |
||
105 | 1 | $this->eventDispatcher->addSubscriber($subscriber); |
|
106 | 1 | } |
|
107 | |||
108 | /** |
||
109 | * No errors at all? |
||
110 | * |
||
111 | * @return boolean |
||
112 | */ |
||
113 | 7 | public function allOk() |
|
117 | |||
118 | /** |
||
119 | * Backup without errors, but some tasks where skipped or failed. |
||
120 | * |
||
121 | * @return boolean |
||
122 | */ |
||
123 | 5 | public function backupOkButSkipsOrFails() |
|
127 | |||
128 | /** |
||
129 | * Backup without errors? |
||
130 | * |
||
131 | * @return boolean |
||
132 | */ |
||
133 | 7 | public function wasSuccessful() |
|
137 | |||
138 | /** |
||
139 | * Nothing skipped? |
||
140 | * |
||
141 | * @return boolean |
||
142 | */ |
||
143 | 5 | public function noneSkipped() |
|
147 | |||
148 | /** |
||
149 | * Nothing failed? |
||
150 | * |
||
151 | * @return boolean |
||
152 | */ |
||
153 | 4 | public function noneFailed() |
|
157 | |||
158 | /** |
||
159 | * Add Exception to error list |
||
160 | * |
||
161 | * @param \Exception $e |
||
162 | */ |
||
163 | 3 | public function addError(\Exception $e) |
|
167 | |||
168 | /** |
||
169 | * Return current error count. |
||
170 | * |
||
171 | * @return integer |
||
172 | */ |
||
173 | 5 | public function errorCount() |
|
177 | |||
178 | /** |
||
179 | * Return list of errors. |
||
180 | * |
||
181 | * @return array<Exception> |
||
182 | */ |
||
183 | 5 | public function getErrors() |
|
184 | { |
||
185 | 5 | return $this->errors; |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * Return list of executed backups. |
||
190 | * |
||
191 | * @return array<\phpbu\App\Configuration\Backup> |
||
192 | */ |
||
193 | 5 | public function getBackups() |
|
197 | |||
198 | /** |
||
199 | * phpbu start event. |
||
200 | * |
||
201 | * @param \phpbu\App\Configuration $configuration |
||
202 | */ |
||
203 | 7 | public function phpbuStart(Configuration $configuration) |
|
204 | { |
||
205 | 7 | $event = new Event\App\Start($configuration); |
|
206 | 7 | $this->eventDispatcher->dispatch(Event\App\Start::NAME, $event); |
|
207 | 7 | } |
|
208 | |||
209 | /** |
||
210 | * phpbu end event. |
||
211 | */ |
||
212 | 7 | public function phpbuEnd() |
|
217 | |||
218 | /** |
||
219 | * Backup start event. |
||
220 | * |
||
221 | * @param \phpbu\App\Configuration\Backup $backup |
||
222 | */ |
||
223 | 7 | public function backupStart(Configuration\Backup $backup) |
|
224 | { |
||
225 | 7 | $this->backupActive = new Result\Backup($backup->getName()); |
|
226 | 7 | $this->backups[] = $this->backupActive; |
|
227 | |||
228 | 7 | $event = new Event\Backup\Start($backup); |
|
229 | 7 | $this->eventDispatcher->dispatch(Event\Backup\Start::NAME, $event); |
|
230 | 7 | } |
|
231 | |||
232 | /** |
||
233 | * Backup failed event. |
||
234 | * |
||
235 | * @param \phpbu\App\Configuration\Backup $backup |
||
236 | */ |
||
237 | 2 | public function backupFailed(Configuration\Backup $backup) |
|
238 | { |
||
239 | 2 | $this->backupsFailed++; |
|
240 | 2 | $this->backupActive->fail(); |
|
241 | |||
242 | 2 | $event = new Event\Backup\Failed($backup); |
|
243 | 2 | $this->eventDispatcher->dispatch(Event\Backup\Failed::NAME, $event); |
|
244 | 2 | } |
|
245 | |||
246 | /** |
||
247 | * Return amount of failed backups |
||
248 | * |
||
249 | * @return integer |
||
250 | */ |
||
251 | 3 | public function backupsFailedCount() |
|
252 | { |
||
253 | 3 | return $this->backupsFailed; |
|
254 | } |
||
255 | |||
256 | /** |
||
257 | * Backup end event. |
||
258 | * |
||
259 | * @param \phpbu\App\Configuration\Backup $backup |
||
260 | */ |
||
261 | 7 | public function backupEnd(Configuration\Backup $backup) |
|
266 | |||
267 | /** |
||
268 | * Check start event. |
||
269 | * |
||
270 | * @param \phpbu\App\Configuration\Backup\Check $check |
||
271 | */ |
||
272 | 4 | public function checkStart(Configuration\Backup\Check $check) |
|
279 | |||
280 | /** |
||
281 | * Check failed event. |
||
282 | * |
||
283 | * @param \phpbu\App\Configuration\Backup\Check $check |
||
284 | */ |
||
285 | 1 | public function checkFailed(Configuration\Backup\Check $check) |
|
299 | |||
300 | 2 | /** |
|
301 | * Return amount of failed checks. |
||
302 | 2 | * |
|
303 | * @return integer |
||
304 | */ |
||
305 | public function checksFailedCount() |
||
309 | |||
310 | 3 | /** |
|
311 | * Check end event. |
||
312 | 3 | * |
|
313 | 3 | * @param \phpbu\App\Configuration\Backup\Check $check |
|
314 | 3 | */ |
|
315 | public function checkEnd(Configuration\Backup\Check $check) |
||
320 | |||
321 | 2 | /** |
|
322 | * Crypt start event. |
||
323 | 2 | * |
|
324 | * @param \phpbu\App\Configuration\Backup\Crypt $crypt |
||
325 | 2 | */ |
|
326 | 2 | public function cryptStart(Configuration\Backup\Crypt $crypt) |
|
333 | |||
334 | 1 | /** |
|
335 | * Crypt skipped event. |
||
336 | 1 | * |
|
337 | 1 | * @param \phpbu\App\Configuration\Backup\Crypt $crypt |
|
338 | */ |
||
339 | 1 | public function cryptSkipped(Configuration\Backup\Crypt $crypt) |
|
347 | |||
348 | 1 | /** |
|
349 | * Return amount of skipped crypts. |
||
350 | 1 | * |
|
351 | * @return integer |
||
352 | */ |
||
353 | public function cryptsSkippedCount() |
||
357 | |||
358 | |||
359 | 1 | /** |
|
360 | * Crypt failed event. |
||
361 | 1 | * |
|
362 | 1 | * @param \phpbu\App\Configuration\Backup\Crypt $crypt |
|
363 | 1 | */ |
|
364 | public function cryptFailed(Configuration\Backup\Crypt $crypt) |
||
373 | |||
374 | 1 | /** |
|
375 | * Return amount of failed crypts. |
||
376 | 1 | * |
|
377 | * @return integer |
||
378 | */ |
||
379 | public function cryptsFailedCount() |
||
383 | |||
384 | 1 | /** |
|
385 | * Crypt end event. |
||
386 | 1 | * |
|
387 | 1 | * @param \phpbu\App\Configuration\Backup\Crypt $crypt |
|
388 | 1 | */ |
|
389 | public function cryptEnd(Configuration\Backup\Crypt $crypt) |
||
394 | |||
395 | 2 | /** |
|
396 | * Sync start event. |
||
397 | 2 | * |
|
398 | * @param \phpbu\App\Configuration\Backup\Sync $sync |
||
399 | 2 | */ |
|
400 | 2 | public function syncStart(Configuration\Backup\Sync $sync) |
|
407 | |||
408 | 1 | /** |
|
409 | * Sync skipped event. |
||
410 | 1 | * |
|
411 | 1 | * @param \phpbu\App\Configuration\Backup\Sync $sync |
|
412 | */ |
||
413 | 1 | public function syncSkipped(Configuration\Backup\Sync $sync) |
|
421 | |||
422 | 3 | /** |
|
423 | * Return amount of skipped syncs. |
||
424 | 3 | * |
|
425 | * @return integer |
||
426 | */ |
||
427 | public function syncsSkippedCount() |
||
431 | |||
432 | 1 | /** |
|
433 | * Sync failed event. |
||
434 | 1 | * |
|
435 | 1 | * @param \phpbu\App\Configuration\Backup\Sync $sync |
|
436 | */ |
||
437 | 1 | public function syncFailed(Configuration\Backup\Sync $sync) |
|
445 | |||
446 | 3 | /** |
|
447 | * Return amount of failed syncs. |
||
448 | 3 | * |
|
449 | * @return integer |
||
450 | */ |
||
451 | public function syncsFailedCount() |
||
455 | |||
456 | 1 | /** |
|
457 | * Sync end event. |
||
458 | 1 | * |
|
459 | 1 | * @param \phpbu\App\Configuration\Backup\Sync $sync |
|
460 | 1 | */ |
|
461 | public function syncEnd(Configuration\Backup\Sync $sync) |
||
466 | |||
467 | 2 | /** |
|
468 | * Cleanup start event. |
||
469 | 2 | * |
|
470 | * @param \phpbu\App\Configuration\Backup\Cleanup $cleanup |
||
471 | 2 | */ |
|
472 | 2 | public function cleanupStart(Configuration\Backup\Cleanup $cleanup) |
|
479 | |||
480 | 1 | /** |
|
481 | * Cleanup skipped event. |
||
482 | 1 | * |
|
483 | 1 | * @param \phpbu\App\Configuration\Backup\Cleanup $cleanup |
|
484 | */ |
||
485 | 1 | public function cleanupSkipped(Configuration\Backup\Cleanup $cleanup) |
|
493 | |||
494 | 3 | /** |
|
495 | * Return amount of skipped cleanups. |
||
496 | 3 | * |
|
497 | * @return integer |
||
498 | */ |
||
499 | public function cleanupsSkippedCount() |
||
503 | |||
504 | 1 | /** |
|
505 | * Cleanup failed event. |
||
506 | 1 | * |
|
507 | 1 | * @param \phpbu\App\Configuration\Backup\Cleanup $cleanup |
|
508 | */ |
||
509 | 1 | public function cleanupFailed(Configuration\Backup\Cleanup $cleanup) |
|
517 | |||
518 | 3 | /** |
|
519 | * Return amount of failed cleanups. |
||
520 | 3 | * |
|
521 | * @return integer |
||
522 | */ |
||
523 | public function cleanupsFailedCount() |
||
527 | |||
528 | 1 | /** |
|
529 | * Cleanup end event. |
||
530 | 1 | * |
|
531 | 1 | * @param \phpbu\App\Configuration\Backup\Cleanup $cleanup |
|
532 | 1 | */ |
|
533 | public function cleanupEnd(Configuration\Backup\Cleanup $cleanup) |
||
538 | |||
539 | 1 | /** |
|
540 | * Debug. |
||
541 | 1 | * |
|
542 | 1 | * @param string $msg |
|
543 | 1 | */ |
|
544 | public function debug($msg) |
||
549 | } |
||
550 |