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 ConsoleIO 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 ConsoleIO, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class ConsoleIO implements IOInterface |
||
| 17 | { |
||
| 18 | protected $input; |
||
| 19 | protected $output; |
||
| 20 | protected $helperSet; |
||
| 21 | protected $authorizations = array(); |
||
| 22 | protected $lastMessage; |
||
| 23 | protected $lastMessageNewline = true; |
||
| 24 | protected $indention = 0; |
||
| 25 | protected $prefix; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Constructor. |
||
| 29 | * |
||
| 30 | * @param InputInterface $input The input instance |
||
| 31 | * @param OutputInterface $output The output instance |
||
| 32 | * @param HelperSet $helperSet The helperSet instance |
||
| 33 | */ |
||
| 34 | public function __construct(InputInterface $input, OutputInterface $output, HelperSet $helperSet) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritDoc} |
||
| 43 | */ |
||
| 44 | public function isInteractive() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * {@inheritDoc} |
||
| 51 | */ |
||
| 52 | public function isDecorated() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritDoc} |
||
| 59 | */ |
||
| 60 | public function isVerbose() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Set verbose option |
||
| 67 | * |
||
| 68 | * @param bool $option |
||
| 69 | */ |
||
| 70 | public function setVerbose($option) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param string|array $messages |
||
| 77 | * @return string|array |
||
| 78 | */ |
||
| 79 | protected function applyIndention($messages) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param string|array $messages |
||
| 94 | * @return string|array |
||
| 95 | */ |
||
| 96 | protected function applyPrefix($messages) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritDoc} |
||
| 115 | */ |
||
| 116 | public function write($messages, $newline = true) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritDoc} |
||
| 126 | */ |
||
| 127 | protected function _write($messages, $newline = true) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * {@inheritDoc} |
||
| 136 | */ |
||
| 137 | public function overwrite($messages, $newline = true, $size = null) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritDoc} |
||
| 177 | */ |
||
| 178 | View Code Duplication | public function select($question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid') |
|
| 186 | |||
| 187 | /** |
||
| 188 | * {@inheritDoc} |
||
| 189 | */ |
||
| 190 | View Code Duplication | public function ask($question, $default = null) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritDoc} |
||
| 201 | */ |
||
| 202 | View Code Duplication | public function askConfirmation($question, $default = true) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritDoc} |
||
| 213 | */ |
||
| 214 | View Code Duplication | public function askAndValidate($question, $validator, $attempts = false, $default = null) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * {@inheritDoc} |
||
| 225 | */ |
||
| 226 | public function askAndHideAnswer($question) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * (non-PHPdoc) |
||
| 277 | * @see Webcreate\Conveyor\IO.IOInterface::setIndention() |
||
| 278 | */ |
||
| 279 | public function setIndention($indent) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * (non-PHPdoc) |
||
| 286 | * @see Webcreate\Conveyor\IO.IOInterface::increaseIndention() |
||
| 287 | */ |
||
| 288 | public function increaseIndention($indent) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * (non-PHPdoc) |
||
| 295 | * @see Webcreate\Conveyor\IO.IOInterface::decreaseIndention() |
||
| 296 | */ |
||
| 297 | public function decreaseIndention($indent) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * (non-PHPdoc) |
||
| 308 | * @see Webcreate\Conveyor\IO.IOInterface::getIndention() |
||
| 309 | */ |
||
| 310 | public function getIndention() |
||
| 314 | |||
| 315 | public function renderException($e) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param string $prefix |
||
| 330 | * @return mixed |
||
| 331 | */ |
||
| 332 | public function setPrefix($prefix) |
||
| 336 | } |
||
| 337 |