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 CLI 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 CLI, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class CLI { |
||
14 | use Module, Events; |
||
15 | |||
16 | protected static $file = null; |
||
17 | protected static $arguments = []; |
||
18 | protected static $options = []; |
||
19 | protected static $commands = []; |
||
20 | protected static $help = null; |
||
21 | protected static $error = null; |
||
22 | |||
23 | protected static $shell_colors = [ |
||
24 | 'BLACK' =>"\033[0;30m", 'DARKGRAY' =>"\033[1;30m", |
||
25 | 'BLUE' =>"\033[0;34m", 'LIGHTBLUE' =>"\033[1;34m", |
||
26 | 'GREEN' =>"\033[0;32m", 'LIGHTGREEN' =>"\033[1;32m", |
||
27 | 'CYAN' =>"\033[0;36m", 'LIGHTCYAN' =>"\033[1;36m", |
||
28 | 'RED' =>"\033[0;31m", 'LIGHTRED' =>"\033[1;31m", |
||
29 | 'PURPLE' =>"\033[0;35m", 'LIGHTPURPLE' =>"\033[1;35m", |
||
30 | 'BROWN' =>"\033[0;33m", 'YELLOW' =>"\033[1;33m", |
||
31 | 'LIGHTGRAY' =>"\033[0;37m", 'WHITE' =>"\033[1;37m", |
||
32 | 'NORMAL' =>"\033[0;37m", 'B' =>"\033[1m", |
||
33 | 'ERROR' =>"\033[1;31m", 'INFO' =>"\033[0;36m", |
||
34 | 'I' =>"\033[0;30;104m", 'IB' =>"\033[1;30;104m", |
||
35 | 'U' =>"\033[4m", 'D' =>"\033[2m", |
||
36 | ]; |
||
37 | protected static $color_stack = ['NORMAL']; |
||
38 | |||
39 | |||
40 | /** |
||
41 | * Bind a callback to a command route |
||
42 | * @param string $command The command route, use ":" before a parameter for extraction. |
||
43 | * @param callable $callback The callback to be binded to the route. |
||
44 | */ |
||
45 | public static function on($command,callable $callback,$description=''){ |
||
49 | |||
50 | /** |
||
51 | * Bind a callback to the "help" route. |
||
52 | * @param callable $callback The callback to be binded to the route. If omitted triggers the callback. |
||
53 | */ |
||
54 | public static function help(callable $callback = null){ |
||
59 | |||
60 | /** |
||
61 | * Bind a callback when an error occurs. |
||
62 | * @param callable $callback The callback to be binded to the route. If omitted triggers the callback. |
||
63 | */ |
||
64 | public static function error(callable $callback = null){ |
||
69 | |||
70 | /** |
||
71 | * Returns the script name. |
||
72 | * @return string |
||
73 | */ |
||
74 | public static function name(){ |
||
77 | |||
78 | /** |
||
79 | * Triggers an error and exit |
||
80 | * @param string $message |
||
81 | */ |
||
82 | protected static function triggerError($message){ |
||
86 | |||
87 | /** |
||
88 | * Get a passed option |
||
89 | * @param string $key The name of the option paramenter |
||
90 | * @param mixed $default The default value if parameter is omitted. (if a callable it will be evaluated) |
||
91 | * @return mixed |
||
92 | */ |
||
93 | public static function input($key=null,$default=null){ |
||
96 | |||
97 | /** |
||
98 | * Returns an explanation for the supported commands |
||
99 | * |
||
100 | * @method commands |
||
101 | * |
||
102 | * @return array The commands and their description. |
||
103 | */ |
||
104 | public static function commands(){ |
||
115 | |||
116 | /** |
||
117 | * Dispatch the router |
||
118 | * @param string[] $args The arguments array. |
||
119 | * @return boolean True if route was correctly dispatched. |
||
120 | */ |
||
121 | public static function run($args=null){ |
||
163 | |||
164 | /** |
||
165 | * Prints a message to the console with color formatting. |
||
166 | * @param string $message The html-like encoded message |
||
167 | * @return void |
||
168 | */ |
||
169 | public static function write($message){ |
||
187 | |||
188 | /** |
||
189 | * Like CLI::write, but appends a newline at the end. |
||
190 | * @param string $message The html-like encoded message |
||
191 | * @return void |
||
192 | */ |
||
193 | public static function writeln($message){ |
||
196 | |||
197 | /** |
||
198 | * Set output ANSI color |
||
199 | * @param string $color The color name constant. |
||
200 | * @return void |
||
201 | */ |
||
202 | public static function color($color){ |
||
205 | |||
206 | /** |
||
207 | * Edit a temporary block of text with $EDITOR (or nano as fallback) |
||
208 | * @param string $text The initial text of the document. |
||
209 | * @param string $filename The (fake) filename passed to the editor (for syntax highlighting hint). |
||
210 | * @return string The edited contents |
||
211 | */ |
||
212 | public static function edit($text,$filename=''){ |
||
221 | |||
222 | |||
223 | } |
||
224 | |||
241 |