Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Output 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 Output, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
67 | class Output extends OutputPlugin |
||
68 | { |
||
69 | /** |
||
70 | * a list of the plugins that are currently active |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $plugins = []; |
||
75 | |||
76 | /** |
||
77 | * a list of the log messages that we have been asked to output |
||
78 | * |
||
79 | * this is used for producing detailed error reports when something |
||
80 | * has gone badly wrong |
||
81 | * |
||
82 | * @var array |
||
83 | */ |
||
84 | protected $activityLog = []; |
||
85 | |||
86 | /** |
||
87 | * constructor |
||
88 | * |
||
89 | * ensures we have a default console that is connected to stdout |
||
90 | */ |
||
91 | public function __construct() |
||
99 | |||
100 | /** |
||
101 | * make a plugin the one that we use when writing to the user's |
||
102 | * console |
||
103 | * |
||
104 | * @param Console $plugin |
||
105 | * the plugin that we want |
||
106 | * |
||
107 | * @return void |
||
108 | */ |
||
109 | public function usePluginAsConsole(Console $plugin) |
||
113 | |||
114 | /** |
||
115 | * set the plugin for a named output slot |
||
116 | * |
||
117 | * @param OutputPlugin $plugin |
||
118 | * the plugin to use in the slot |
||
119 | * @param string $slotName |
||
120 | * the name of the slot to use for this plugin |
||
121 | */ |
||
122 | public function usePluginInSlot(OutputPlugin $plugin, $slotName) |
||
130 | |||
131 | /** |
||
132 | * get the array of all plugins |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | public function getPlugins() |
||
140 | |||
141 | /** |
||
142 | * return the active plugin in the 'console' slot |
||
143 | * |
||
144 | * @return Console|null |
||
145 | */ |
||
146 | public function getActiveConsolePlugin() |
||
151 | |||
152 | /** |
||
153 | * return the active plugin in the named slot |
||
154 | * |
||
155 | * @param string $slotName |
||
156 | * @return OutputPlugin|null |
||
157 | */ |
||
158 | public function getActivePluginInSlot($slotName) |
||
171 | |||
172 | /** |
||
173 | * disable 'silent' mode |
||
174 | * |
||
175 | * NOTE: it is up to each plugin in turn whether or not to support |
||
176 | * 'silent' mode at all |
||
177 | * |
||
178 | * @return void |
||
179 | */ |
||
180 | public function resetSilentMode() |
||
187 | |||
188 | /** |
||
189 | * switches 'silent' mode on |
||
190 | * |
||
191 | * in 'silent' mode, we do not write log activity to the output writer |
||
192 | * at all. HOWEVER, the plugin may still add the log activity to any |
||
193 | * internal cache it has (can be useful for error reports etc) |
||
194 | * |
||
195 | * @return void |
||
196 | */ |
||
197 | public function setSilentMode() |
||
204 | |||
205 | /** |
||
206 | * switches 'verbose' mode on or off |
||
207 | * |
||
208 | * in 'non-verbose' mode, each output plugin is free to supress some of |
||
209 | * the output, for the sake of asthetics |
||
210 | * |
||
211 | * @param boolean $isVerbose |
||
212 | * do we want verbose mode or not? |
||
213 | */ |
||
214 | public function setIsVerbose($isVerbose) |
||
221 | |||
222 | /** |
||
223 | * disables any colour output |
||
224 | * |
||
225 | * @return void |
||
226 | */ |
||
227 | public function disableColourSupport() |
||
234 | |||
235 | /** |
||
236 | * forces switching on colour support |
||
237 | * |
||
238 | * @return void |
||
239 | */ |
||
240 | public function enforceColourSupport() |
||
247 | |||
248 | /** |
||
249 | * asks each active plugin to switch on colour support if possible |
||
250 | * |
||
251 | * a plugin may still choose to not output colour. one example of this |
||
252 | * are consoles. they're happy to output colour if talking to a terminal, |
||
253 | * but choose not to output colour if they're only writing to log files |
||
254 | * or to a pipe into another UNIX process. |
||
255 | * |
||
256 | * @return void |
||
257 | */ |
||
258 | public function enableColourSupport() |
||
265 | |||
266 | /** |
||
267 | * called when storyplayer starts |
||
268 | * |
||
269 | * @param string $version |
||
270 | * @param string $url |
||
271 | * @param string $copyright |
||
272 | * @param string $license |
||
273 | * @return void |
||
274 | */ |
||
275 | public function startStoryplayer($version, $url, $copyright, $license) |
||
289 | |||
290 | /** |
||
291 | * called when Storyplayer exits |
||
292 | * |
||
293 | * @param float $duration |
||
294 | * how long did storyplayer take to run (in seconds)? |
||
295 | * @return void |
||
296 | */ |
||
297 | public function endStoryplayer($duration) |
||
304 | |||
305 | /** |
||
306 | * called when we start playing a new PhaseGroup |
||
307 | * |
||
308 | * @param string $activity |
||
309 | * what are we doing? (e.g. 'creating', 'running') |
||
310 | * @param string $name |
||
311 | * the name of the phase group |
||
312 | * @param array|null $details |
||
313 | * optional explanation of what this PhaseGroup is trying |
||
314 | * to achieve |
||
315 | * @return void |
||
316 | */ |
||
317 | public function startPhaseGroup($activity, $name, $details = null) |
||
329 | |||
330 | /** |
||
331 | * called when we have finished playing a PhaseGroup |
||
332 | * |
||
333 | * NOTE: we cannot use a type-hint for $result here. we may pass in |
||
334 | * a class that inherits from PhaseGroup_Result, and (annoyingly) |
||
335 | * this isn't allowed if we use a type-hint (grrrr) |
||
336 | * |
||
337 | * @param PhaseGroup_Result $result |
||
338 | * @return void |
||
339 | */ |
||
340 | public function endPhaseGroup($result) |
||
351 | |||
352 | /** |
||
353 | * called when a story starts a new phase |
||
354 | * |
||
355 | * $param Phase $phase |
||
356 | * the phase that we are executing |
||
357 | * @return void |
||
358 | */ |
||
359 | public function startPhase($phase) |
||
369 | |||
370 | /** |
||
371 | * called when a story ends a phase |
||
372 | * |
||
373 | * @param Phase $phase |
||
374 | * the phase that has finished |
||
375 | * @param Phase_Result $phaseResult |
||
376 | * the result of running $phase |
||
377 | * @return void |
||
378 | */ |
||
379 | public function endPhase($phase, $phaseResult) |
||
395 | |||
396 | /** |
||
397 | * called when a story logs an action |
||
398 | * |
||
399 | * @param string $msg |
||
400 | * the message to write to the logs / console |
||
401 | * @param array|null $codeLine |
||
402 | * information about the line of code that is executing |
||
403 | * @return void |
||
404 | */ |
||
405 | View Code Duplication | public function logPhaseActivity($msg, $codeLine = null) |
|
428 | |||
429 | /** |
||
430 | * called when a story logs the (possibly partial) output from |
||
431 | * running a subprocess |
||
432 | * |
||
433 | * @param string $msg the output to log |
||
434 | * @return void |
||
435 | */ |
||
436 | public function logPhaseSubprocessOutput($msg) |
||
456 | |||
457 | /** |
||
458 | * called when a story logs an error |
||
459 | * |
||
460 | * @param string $phaseName |
||
461 | * the name of the phase where the error occurred |
||
462 | * @param string $msg |
||
463 | * an error message to send to console|logfile |
||
464 | * @return void |
||
465 | */ |
||
466 | View Code Duplication | public function logPhaseError($phaseName, $msg) |
|
486 | |||
487 | /** |
||
488 | * called when a story is skipped |
||
489 | * |
||
490 | * @param string $phaseName |
||
491 | * the name of the phase where the error occurred |
||
492 | * @param string $msg |
||
493 | * an informational message to send to console|logfile |
||
494 | * @return void |
||
495 | */ |
||
496 | View Code Duplication | public function logPhaseSkipped($phaseName, $msg) |
|
516 | |||
517 | /** |
||
518 | * called when we want to record which line of code in a phase is |
||
519 | * currently executing |
||
520 | * |
||
521 | * @param array $codeLine |
||
522 | * details about the line of code that is executing |
||
523 | * @return void |
||
524 | */ |
||
525 | public function logPhaseCodeLine($codeLine) |
||
536 | |||
537 | /** |
||
538 | * called when the outer CLI shell encounters a fatal error |
||
539 | * |
||
540 | * @param string $msg |
||
541 | * the error message to show the user |
||
542 | * |
||
543 | * @return void |
||
544 | */ |
||
545 | public function logCliError($msg) |
||
556 | |||
557 | /** |
||
558 | * called when the outer CLI shell encounters a fatal error |
||
559 | * |
||
560 | * @param string $msg |
||
561 | * the error message to show the user |
||
562 | * @param \Exception $e |
||
563 | * the exception that caused the error |
||
564 | * @return void |
||
565 | */ |
||
566 | public function logCliErrorWithException($msg, $e) |
||
578 | |||
579 | /** |
||
580 | * called when the outer CLI shell needs to publish a warning |
||
581 | * |
||
582 | * @param string $msg |
||
583 | * the warning message to show the user |
||
584 | * |
||
585 | * @return void |
||
586 | */ |
||
587 | public function logCliWarning($msg) |
||
598 | |||
599 | /** |
||
600 | * called when the outer CLI shell needs to tell the user something |
||
601 | * |
||
602 | * @param string $msg |
||
603 | * the message to show the user |
||
604 | * |
||
605 | * @return void |
||
606 | */ |
||
607 | public function logCliInfo($msg) |
||
618 | |||
619 | /** |
||
620 | * an alternative to using PHP's built-in var_dump() |
||
621 | * |
||
622 | * @param string $name |
||
623 | * a human-readable name to describe $var |
||
624 | * |
||
625 | * @param mixed $var |
||
626 | * the variable to dump |
||
627 | * |
||
628 | * @return void |
||
629 | */ |
||
630 | public function logVardump($name, $var) |
||
642 | } |
||
643 |