Complex classes like Console 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 Console, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Console |
||
| 16 | { |
||
| 17 | |||
| 18 | const black = "\e[0;30m"; |
||
| 19 | const b_black = "\e[30;1m"; |
||
| 20 | |||
| 21 | const red = "\e[0;31m"; |
||
| 22 | const b_red = "\e[1;31m"; |
||
| 23 | |||
| 24 | const green = "\e[0;32m"; |
||
| 25 | const b_green = "\e[1;32m"; |
||
| 26 | |||
| 27 | const yellow = "\e[0;33m"; |
||
| 28 | const b_yellow = "\e[1;33m"; |
||
| 29 | |||
| 30 | const blue = "\e[0;34m"; |
||
| 31 | const b_blue = "\e[1;34m"; |
||
| 32 | |||
| 33 | const magenta = "\e[0;35m"; |
||
| 34 | const b_magenta = "\e[1;35m"; |
||
| 35 | |||
| 36 | const cyan = "\e[0;36m"; |
||
| 37 | const b_cyan = "\e[1;36m"; |
||
| 38 | |||
| 39 | const white = "\e[0;37m"; |
||
| 40 | const b_white = "\e[1;37m"; |
||
| 41 | |||
| 42 | // define aliases for colors |
||
| 43 | const error = "\e[37;1m\e[41m"; |
||
| 44 | const success = self::b_green; |
||
| 45 | const normal = "\e[0m"; |
||
| 46 | const bold = self::b_white; |
||
| 47 | |||
| 48 | |||
| 49 | /** @var ConsoleOutputInterface */ |
||
| 50 | protected $consoleOutput; |
||
| 51 | |||
| 52 | /** @var boolean Color console enabled */ |
||
| 53 | protected $colorEnabled; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Console constructor. |
||
| 57 | * |
||
| 58 | * @param bool $colorEnabled |
||
| 59 | */ |
||
| 60 | public function __construct($colorEnabled) |
||
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * Initialize service with the console output. |
||
| 68 | * |
||
| 69 | * @param ConsoleOutputInterface $consoleOutput |
||
| 70 | */ |
||
| 71 | public function init(ConsoleOutputInterface $consoleOutput) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @inheritdoc |
||
| 78 | */ |
||
| 79 | public function writeln($string) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @inheritdoc |
||
| 86 | */ |
||
| 87 | public function write($string, $newline = false) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Outoyt brute text. |
||
| 121 | * |
||
| 122 | * @param string $msg |
||
| 123 | * @param boolean $newline |
||
| 124 | * |
||
| 125 | */ |
||
| 126 | protected function ansiOut($msg, $newline) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Strip styles from a string. |
||
| 138 | * |
||
| 139 | * @param $string |
||
| 140 | * |
||
| 141 | * @return mixed |
||
| 142 | */ |
||
| 143 | protected function stripStyles($string) |
||
| 147 | |||
| 148 | |||
| 149 | protected function doHslConvert($r, $g, $b) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Fix the color codes from MP standard to world standard |
||
| 208 | * |
||
| 209 | * @param string $r |
||
| 210 | * @param string $g |
||
| 211 | * @param string $b |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public function fixColors($r, $g, $b) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Convert from number to numeric string |
||
| 238 | * |
||
| 239 | * @param int $number |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function convert($number) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get symphony console. |
||
| 261 | * |
||
| 262 | * @return OutputInterface |
||
| 263 | */ |
||
| 264 | public function getConsoleOutput() |
||
| 268 | } |
||
| 269 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.