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() |
||
| 92 | { |
||
| 93 | // we need a default output for the console |
||
| 94 | $console = new DefaultConsole(); |
||
| 95 | $console->addOutputToStdout(); |
||
| 96 | |||
| 97 | $this->usePluginAsConsole($console); |
||
| 98 | } |
||
| 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) |
||
| 123 | { |
||
| 124 | // enforce our inputs |
||
| 125 | Contract::RequiresValue($slotName, is_string($slotName)); |
||
| 126 | |||
| 127 | // put the plugin in the required slot |
||
| 128 | $this->plugins[$slotName] = $plugin; |
||
| 129 | } |
||
| 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 int |
||
| 296 | */ |
||
| 297 | public function endStoryplayer($duration) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * called when we start playing a new PhaseGroup |
||
| 311 | * |
||
| 312 | * @param string $activity |
||
| 313 | * what are we doing? (e.g. 'creating', 'running') |
||
| 314 | * @param string $name |
||
| 315 | * the name of the phase group |
||
| 316 | * @param array|null $details |
||
| 317 | * optional explanation of what this PhaseGroup is trying |
||
| 318 | * to achieve |
||
| 319 | * @return void |
||
| 320 | */ |
||
| 321 | public function startPhaseGroup($activity, $name, $details = null) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * called when we have finished playing a PhaseGroup |
||
| 336 | * |
||
| 337 | * NOTE: we cannot use a type-hint for $result here. we may pass in |
||
| 338 | * a class that inherits from PhaseGroup_Result, and (annoyingly) |
||
| 339 | * this isn't allowed if we use a type-hint (grrrr) |
||
| 340 | * |
||
| 341 | * @param PhaseGroup_Result $result |
||
| 342 | * @return void |
||
| 343 | */ |
||
| 344 | public function endPhaseGroup($result) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * called when a story starts a new phase |
||
| 358 | * |
||
| 359 | * $param Phase $phase |
||
| 360 | * the phase that we are executing |
||
| 361 | * @return void |
||
| 362 | */ |
||
| 363 | public function startPhase($phase) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * called when a story ends a phase |
||
| 376 | * |
||
| 377 | * @param Phase $phase |
||
| 378 | * the phase that has finished |
||
| 379 | * @param Phase_Result $phaseResult |
||
| 380 | * the result of running $phase |
||
| 381 | * @return void |
||
| 382 | */ |
||
| 383 | public function endPhase($phase, $phaseResult) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * called when a story logs an action |
||
| 402 | * |
||
| 403 | * @param string $msg |
||
| 404 | * the message to write to the logs / console |
||
| 405 | * @param array|null $codeLine |
||
| 406 | * information about the line of code that is executing |
||
| 407 | * @return void |
||
| 408 | */ |
||
| 409 | View Code Duplication | public function logPhaseActivity($msg, $codeLine = null) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * called when a story logs the (possibly partial) output from |
||
| 435 | * running a subprocess |
||
| 436 | * |
||
| 437 | * @param string $msg the output to log |
||
| 438 | * @return void |
||
| 439 | */ |
||
| 440 | public function logPhaseSubprocessOutput($msg) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * called when a story logs an error |
||
| 463 | * |
||
| 464 | * @param string $phaseName |
||
| 465 | * the name of the phase where the error occurred |
||
| 466 | * @param string $msg |
||
| 467 | * an error message to send to console|logfile |
||
| 468 | * @return void |
||
| 469 | */ |
||
| 470 | View Code Duplication | public function logPhaseError($phaseName, $msg) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * called when a story is skipped |
||
| 493 | * |
||
| 494 | * @param string $phaseName |
||
| 495 | * the name of the phase where the error occurred |
||
| 496 | * @param string $msg |
||
| 497 | * an informational message to send to console|logfile |
||
| 498 | * @return void |
||
| 499 | */ |
||
| 500 | View Code Duplication | public function logPhaseSkipped($phaseName, $msg) |
|
| 520 | |||
| 521 | /** |
||
| 522 | * called when we want to record which line of code in a phase is |
||
| 523 | * currently executing |
||
| 524 | * |
||
| 525 | * @param array $codeLine |
||
| 526 | * details about the line of code that is executing |
||
| 527 | * @return void |
||
| 528 | */ |
||
| 529 | public function logPhaseCodeLine($codeLine) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * called when the outer CLI shell encounters a fatal error |
||
| 543 | * |
||
| 544 | * @param string $msg |
||
| 545 | * the error message to show the user |
||
| 546 | * |
||
| 547 | * @return void |
||
| 548 | */ |
||
| 549 | public function logCliError($msg) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * called when the outer CLI shell encounters a fatal error |
||
| 563 | * |
||
| 564 | * @param string $msg |
||
| 565 | * the error message to show the user |
||
| 566 | * @param \Exception $e |
||
| 567 | * the exception that caused the error |
||
| 568 | * @return void |
||
| 569 | */ |
||
| 570 | public function logCliErrorWithException($msg, $e) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * called when the outer CLI shell needs to publish a warning |
||
| 585 | * |
||
| 586 | * @param string $msg |
||
| 587 | * the warning message to show the user |
||
| 588 | * |
||
| 589 | * @return void |
||
| 590 | */ |
||
| 591 | public function logCliWarning($msg) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * called when the outer CLI shell needs to tell the user something |
||
| 605 | * |
||
| 606 | * @param string $msg |
||
| 607 | * the message to show the user |
||
| 608 | * |
||
| 609 | * @return void |
||
| 610 | */ |
||
| 611 | public function logCliInfo($msg) |
||
| 622 | |||
| 623 | /** |
||
| 624 | * an alternative to using PHP's built-in var_dump() |
||
| 625 | * |
||
| 626 | * @param string $name |
||
| 627 | * a human-readable name to describe $var |
||
| 628 | * |
||
| 629 | * @param mixed $var |
||
| 630 | * the variable to dump |
||
| 631 | * |
||
| 632 | * @return void |
||
| 633 | */ |
||
| 634 | public function logVardump($name, $var) |
||
| 646 | } |
||
| 647 |