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 DialogProvider 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 DialogProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class DialogProvider extends AbstractProvider |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var QuestionHelper |
||
| 22 | */ |
||
| 23 | private $dialog; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var ProgressBar |
||
| 27 | */ |
||
| 28 | private $progress; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Registers services on the given app. |
||
| 32 | * |
||
| 33 | * @param Application $app An Application instance |
||
| 34 | */ |
||
| 35 | public function register(Application $app) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param string $message |
||
| 46 | * @param string|null $argumentname |
||
| 47 | * @param null $default |
||
| 48 | * @return string |
||
| 49 | * @throws \RuntimeException |
||
| 50 | */ |
||
| 51 | public function askFor($message, $argumentname = null, $default = null) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param $message |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | public function askHiddenResponse($message) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $question The question text |
||
| 95 | * @param bool $default The default action |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | public function askConfirmation($question, $default = true) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $message |
||
| 106 | */ |
||
| 107 | public function logStep($message) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $message |
||
| 115 | */ |
||
| 116 | public function logTask($message) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $message |
||
| 130 | */ |
||
| 131 | public function logCommand($message) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param string $message |
||
| 140 | */ |
||
| 141 | public function logCommandTime($startTime) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param string $message |
||
| 154 | */ |
||
| 155 | public function logOutput($message, $error=false, $silent=false) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $message |
||
| 172 | * @param string[] $extra |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public function logQuery($message, $extra=null) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $message |
||
| 197 | */ |
||
| 198 | View Code Duplication | public function logConfig($message) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $message |
||
| 211 | */ |
||
| 212 | View Code Duplication | public function logNotice($message) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @param $message |
||
| 225 | * @param bool|true $report |
||
| 226 | * @throws SkylabException |
||
| 227 | */ |
||
| 228 | public function logError($message, $report=true) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param \Exception $ex |
||
| 240 | * @param array $extra |
||
| 241 | */ |
||
| 242 | public function logException(\Exception $ex, $tags=array(), $extra = array()) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $message |
||
| 263 | */ |
||
| 264 | public function logWarning($message) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param $rows |
||
| 271 | */ |
||
| 272 | public function renderTable($headers, $rows) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $txt |
||
| 281 | */ |
||
| 282 | public static function logo(OutputInterface $output, $verbosity, $txt) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param OutputInterface $output |
||
| 292 | * @param $verbosity |
||
| 293 | * @param $startTime |
||
| 294 | */ |
||
| 295 | public static function logStatistics(OutputInterface $output, $verbosity, $startTime) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * |
||
| 304 | */ |
||
| 305 | public function clearLine() |
||
| 312 | } |
||
| 313 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: