Complex classes like Input 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 Input, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Input |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * @var Definition |
||
24 | */ |
||
25 | protected $definition; |
||
26 | |||
27 | /** |
||
28 | * @var Option[] |
||
29 | */ |
||
30 | protected $options = []; |
||
31 | |||
32 | /** |
||
33 | * @var Argument[] |
||
34 | */ |
||
35 | protected $arguments = []; |
||
36 | |||
37 | protected $interactive = true; |
||
38 | |||
39 | private $tokens; |
||
40 | private $parsed; |
||
41 | |||
42 | public function __construct($argv = null) |
||
54 | |||
55 | protected function setTokens(array $tokens) |
||
59 | |||
60 | /** |
||
61 | * 绑定实例 |
||
62 | * @param Definition $definition A InputDefinition instance |
||
63 | */ |
||
64 | public function bind(Definition $definition): void |
||
72 | |||
73 | /** |
||
74 | * 解析参数 |
||
75 | */ |
||
76 | protected function parse(): void |
||
94 | |||
95 | /** |
||
96 | * 解析短选项 |
||
97 | * @param string $token 当前的指令. |
||
98 | */ |
||
99 | private function parseShortOption(string $token): void |
||
115 | |||
116 | /** |
||
117 | * 解析短选项 |
||
118 | * @param string $name 当前指令 |
||
119 | * @throws \RuntimeException |
||
120 | */ |
||
121 | private function parseShortOptionSet(string $name): void |
||
139 | |||
140 | /** |
||
141 | * 解析完整选项 |
||
142 | * @param string $token 当前指令 |
||
143 | */ |
||
144 | private function parseLongOption(string $token): void |
||
154 | |||
155 | /** |
||
156 | * 解析参数 |
||
157 | * @param string $token 当前指令 |
||
158 | * @throws \RuntimeException |
||
159 | */ |
||
160 | private function parseArgument(string $token): void |
||
177 | |||
178 | /** |
||
179 | * 添加一个短选项的值 |
||
180 | * @param string $shortcut 短名称 |
||
181 | * @param mixed $value 值 |
||
182 | * @throws \RuntimeException |
||
183 | */ |
||
184 | private function addShortOption(string $shortcut, $value): void |
||
192 | |||
193 | /** |
||
194 | * 添加一个完整选项的值 |
||
195 | * @param string $name 选项名 |
||
196 | * @param mixed $value 值 |
||
197 | * @throws \RuntimeException |
||
198 | */ |
||
199 | private function addLongOption(string $name, $value): void |
||
242 | |||
243 | /** |
||
244 | * 获取第一个参数 |
||
245 | * @return string|null |
||
246 | */ |
||
247 | public function getFirstArgument() |
||
258 | |||
259 | /** |
||
260 | * 检查原始参数是否包含某个值 |
||
261 | * @param string|array $values 需要检查的值 |
||
262 | * @return bool |
||
263 | */ |
||
264 | public function hasParameterOption($values): bool |
||
278 | |||
279 | /** |
||
280 | * 获取原始选项的值 |
||
281 | * @param string|array $values 需要检查的值 |
||
282 | * @param mixed $default 默认值 |
||
283 | * @return mixed The option value |
||
284 | */ |
||
285 | public function getParameterOption($values, $default = false) |
||
306 | |||
307 | /** |
||
308 | * 验证输入 |
||
309 | * @throws \RuntimeException |
||
310 | */ |
||
311 | public function validate() |
||
317 | |||
318 | /** |
||
319 | * 检查输入是否是交互的 |
||
320 | * @return bool |
||
321 | */ |
||
322 | public function isInteractive(): bool |
||
326 | |||
327 | /** |
||
328 | * 设置输入的交互 |
||
329 | * @param bool |
||
330 | */ |
||
331 | public function setInteractive(bool $interactive): void |
||
335 | |||
336 | /** |
||
337 | * 获取所有的参数 |
||
338 | * @return Argument[] |
||
339 | */ |
||
340 | public function getArguments(): array |
||
344 | |||
345 | /** |
||
346 | * 根据名称获取参数 |
||
347 | * @param string $name 参数名 |
||
348 | * @return mixed |
||
349 | * @throws \InvalidArgumentException |
||
350 | */ |
||
351 | public function getArgument(string $name) |
||
360 | |||
361 | /** |
||
362 | * 设置参数的值 |
||
363 | * @param string $name 参数名 |
||
364 | * @param string $value 值 |
||
365 | * @throws \InvalidArgumentException |
||
366 | */ |
||
367 | public function setArgument(string $name, $value) |
||
375 | |||
376 | /** |
||
377 | * 检查是否存在某个参数 |
||
378 | * @param string|int $name 参数名或位置 |
||
379 | * @return bool |
||
380 | */ |
||
381 | public function hasArgument($name): bool |
||
385 | |||
386 | /** |
||
387 | * 获取所有的选项 |
||
388 | * @return Option[] |
||
389 | */ |
||
390 | public function getOptions(): array |
||
394 | |||
395 | /** |
||
396 | * 获取选项值 |
||
397 | * @param string $name 选项名称 |
||
398 | * @return mixed |
||
399 | * @throws \InvalidArgumentException |
||
400 | */ |
||
401 | public function getOption(string $name) |
||
409 | |||
410 | /** |
||
411 | * 设置选项值 |
||
412 | * @param string $name 选项名 |
||
413 | * @param string|bool $value 值 |
||
414 | * @throws \InvalidArgumentException |
||
415 | */ |
||
416 | public function setOption(string $name, $value): void |
||
424 | |||
425 | /** |
||
426 | * 是否有某个选项 |
||
427 | * @param string $name 选项名 |
||
428 | * @return bool |
||
429 | */ |
||
430 | public function hasOption(string $name): bool |
||
434 | |||
435 | /** |
||
436 | * 转义指令 |
||
437 | * @param string $token |
||
438 | * @return string |
||
439 | */ |
||
440 | public function escapeToken(string $token): string |
||
444 | |||
445 | /** |
||
446 | * 返回传递给命令的参数的字符串 |
||
447 | * @return string |
||
448 | */ |
||
449 | public function __toString() |
||
465 | } |
||
466 |