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.
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 |
||
20 | class CLI extends Configurable |
||
21 | { |
||
22 | /** |
||
23 | * Maximum line length. |
||
24 | */ |
||
25 | const MAX_LINE_LENGTH = 78; |
||
26 | |||
27 | /** |
||
28 | * Parsed cli arguments. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $cliArguments = []; |
||
33 | |||
34 | /** |
||
35 | * Array of default options. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $defaultOptions = [ |
||
40 | 'silent' => false, |
||
41 | 'append' => false, |
||
42 | 'no_color' => false, |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * Array of valid options. |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $validOptions = [ |
||
51 | // option name => array(description, array of flags) |
||
52 | 'help' => ['Print help (this message) and exit.', ['h']], |
||
53 | 'version' => ['Print version number and exit.', ['v']], |
||
54 | 'silent' => ['Suppress output of error messages.', ['s']], |
||
55 | 'setup_file' => ['Setup file for the parser. Allows to setup custom variables, plugins...', []], |
||
56 | 'no_color' => ['Disable colorized output.', []], |
||
57 | 'compress' => ['Compress output by removing the whitespace.', ['x']], |
||
58 | 'append' => ['Append the generated CSS to the target file?', ['a']], |
||
59 | 'no_ie_compat' => ['Disable IE compatibility checks.', []], |
||
60 | 'source_map' => ['Outputs an inline sourcemap to the generated CSS (or output to filename.map).', []], |
||
61 | 'source_map_url' => ['The complete url and filename put in the less file.', []], |
||
62 | 'source_map_base_path' => ['Sets sourcemap base path, defaults to current working directory.', []], |
||
63 | 'strict-math' => ['Strict math. Requires brackets.', ['sm']], |
||
64 | 'strict-units' => [ |
||
65 | 'Allows mixed units, e.g. 1px+1em or 1px*1px which have units that cannot be represented.', |
||
66 | ['su'], |
||
67 | ], |
||
68 | 'root-path' => [ |
||
69 | 'Sets rootpath for url rewriting in relative imports and urls. Works with or without the relative-urls option.', |
||
70 | ['rp'], |
||
71 | ], |
||
72 | 'relative-urls' => ['Re-writes relative urls to the base less file.', ['ru']], |
||
73 | 'url-args' => ['Adds params into url tokens (e.g. 42, cb=42 or a=1&b=2)', []], |
||
74 | 'dump_line_numbers' => [ |
||
75 | 'Outputs filename and line numbers. TYPE can be either \'comments\', which will output the debug info within comments, \'mediaquery\' that will output the information within a fake media query which is compatible with the SASS format, and \'all\' which will do both.', |
||
76 | [], |
||
77 | ], |
||
78 | ]; |
||
79 | |||
80 | /** |
||
81 | * Array of valid flags. |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $validFlags = []; |
||
86 | |||
87 | /** |
||
88 | * Valid flag. |
||
89 | * |
||
90 | * @var bool |
||
91 | */ |
||
92 | protected $isValid = false; |
||
93 | |||
94 | /** |
||
95 | * Current script name. |
||
96 | * |
||
97 | * @var string |
||
98 | */ |
||
99 | protected $scriptName; |
||
100 | |||
101 | /** |
||
102 | * Current directory. |
||
103 | * |
||
104 | * @var string |
||
105 | */ |
||
106 | protected $currentDir; |
||
107 | |||
108 | /** |
||
109 | * Stdin aliases. |
||
110 | * |
||
111 | * @var array |
||
112 | */ |
||
113 | private $stdAliases = [ |
||
114 | '−', |
||
115 | '–', |
||
116 | '-', |
||
117 | ]; |
||
118 | |||
119 | /** |
||
120 | * Constructor. |
||
121 | * |
||
122 | * @param array $cliArguments Array of ILess\CLI arguments ($argv array) |
||
123 | * @param string $currentDir Current directory |
||
124 | */ |
||
125 | public function __construct(array $cliArguments, $currentDir = null) |
||
132 | |||
133 | /** |
||
134 | * Setups the ILess\CLI handler. |
||
135 | * |
||
136 | * @throws InvalidArgumentException If there is an error in the arguments |
||
137 | */ |
||
138 | protected function setup() |
||
167 | |||
168 | /** |
||
169 | * Converts option names from dash to underscore. Also converts |
||
170 | * less.js command options to ILess valid options. |
||
171 | * |
||
172 | * @param array $options |
||
173 | * |
||
174 | * @return array |
||
175 | */ |
||
176 | protected function convertOptions(array $options) |
||
195 | |||
196 | /** |
||
197 | * Is valid? |
||
198 | * |
||
199 | * @return bool |
||
200 | */ |
||
201 | public function isValid() |
||
205 | |||
206 | /** |
||
207 | * Is flag set? |
||
208 | * |
||
209 | * @param string $flag The flag to check |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | public function hasFlag($flag) |
||
217 | |||
218 | /** |
||
219 | * Returns the script name. |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | public function getScriptName() |
||
227 | |||
228 | /** |
||
229 | * Returns the current directory. |
||
230 | * |
||
231 | * @return string |
||
232 | */ |
||
233 | public function getCurrentDir() |
||
237 | |||
238 | /** |
||
239 | * Runs the task based on the arguments. |
||
240 | * |
||
241 | * @return int 0 on success, error code on failure |
||
242 | */ |
||
243 | public function run() |
||
326 | |||
327 | /** |
||
328 | * Loads setup file. |
||
329 | */ |
||
330 | private static function loadSetupFile() |
||
338 | |||
339 | /** |
||
340 | * Prepares options for the parser. |
||
341 | * |
||
342 | * @return array |
||
343 | */ |
||
344 | protected function prepareOptionsForTheParser() |
||
401 | |||
402 | /** |
||
403 | * Saves the generated CSS to a given file. |
||
404 | * |
||
405 | * @param string $targetFile The target file to write to |
||
406 | * @param string $css The css |
||
407 | * @param bool $append Append the CSS? |
||
408 | * |
||
409 | * @return bool|int The number of bytes that were written to the file, or false on failure. |
||
410 | * |
||
411 | * @throws Exception If the file could not be saved |
||
412 | */ |
||
413 | protected function saveCss($targetFile, $css, $append = false) |
||
419 | |||
420 | /** |
||
421 | * Returns the ILess\CLI usage. |
||
422 | * |
||
423 | * @return string |
||
424 | */ |
||
425 | public function getUsage() |
||
473 | |||
474 | /** |
||
475 | * Returns the signature. |
||
476 | * |
||
477 | * @return string |
||
478 | * |
||
479 | * @link http://patorjk.com/software/taag/#p=display&f=Cyberlarge&t=iless |
||
480 | */ |
||
481 | protected function getSignature() |
||
489 | |||
490 | /** |
||
491 | * Renders an exception. |
||
492 | * |
||
493 | * @param Exception $e |
||
494 | */ |
||
495 | protected function renderException(Exception $e) |
||
515 | |||
516 | /** |
||
517 | * Converts the string to plain text. |
||
518 | * |
||
519 | * @return string $string The string |
||
520 | */ |
||
521 | protected function toText($string) |
||
525 | |||
526 | /** |
||
527 | * Does the console support colors? |
||
528 | * |
||
529 | * @return bool |
||
530 | */ |
||
531 | protected function detectColors() |
||
536 | |||
537 | /** |
||
538 | * Is silence requested? |
||
539 | * |
||
540 | * @return bool |
||
541 | */ |
||
542 | protected function isSilent() |
||
546 | |||
547 | /** |
||
548 | * Parses the $argv array to a more useful array. |
||
549 | * |
||
550 | * @param array $args The $argv array |
||
551 | * |
||
552 | * @return array |
||
553 | * |
||
554 | * @link http://php.net/manual/en/features.commandline.php#83843 |
||
555 | */ |
||
556 | protected function parseArguments($args) |
||
590 | |||
591 | /** |
||
592 | * Converts the value. Parses strings like "false" to boolean false, |
||
593 | * "true" to boolean true. |
||
594 | * |
||
595 | * @param string $value |
||
596 | * |
||
597 | * @return mixed |
||
598 | */ |
||
599 | protected function convertValue($value) |
||
617 | } |
||
618 |