| Conditions | 8 |
| Paths | 14 |
| Total Lines | 60 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 85 | public static function parse(App $app, array $argv): Cmd { |
||
| 86 | |||
| 87 | $cmd = $app->setup(); |
||
| 88 | |||
| 89 | $appspecs = $cmd->getOptionCollection(); |
||
| 90 | |||
| 91 | $parser = new ContinuousOptionParser($appspecs); |
||
| 92 | |||
| 93 | try { |
||
| 94 | $cmd->optionResult = $parser->parse($argv); |
||
| 95 | |||
| 96 | } catch (Exception $e) { |
||
| 97 | |||
| 98 | $cmd->optionParseException = $e; |
||
| 99 | |||
| 100 | return $cmd; |
||
| 101 | } |
||
| 102 | |||
| 103 | while (!$parser->isEnd()) { |
||
| 104 | |||
| 105 | $currentArgument = $parser->getCurrentArgument(); |
||
| 106 | |||
| 107 | $subcommand = self::getSubcommand($currentArgument, $cmd); |
||
| 108 | |||
| 109 | if ($subcommand !== null) { |
||
| 110 | |||
| 111 | $cmd = $subcommand; |
||
| 112 | |||
| 113 | try { |
||
| 114 | self::parseSubcommand($parser, $cmd); |
||
| 115 | } catch (Exception $e) { |
||
| 116 | $cmd->optionParseException = $e; |
||
| 117 | return $cmd; |
||
| 118 | } |
||
| 119 | |||
| 120 | } else { |
||
| 121 | $cmd->arguments[] = $parser->advance(); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | return $cmd; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $argument |
||
| 130 | * @param Cmd $cmd |
||
| 131 | * @return Cmd|null |
||
| 132 | */ |
||
| 133 | private static function getSubcommand(string $argument, Cmd $cmd): ?Cmd { |
||
| 134 | |||
| 135 | $subcommand = null; |
||
| 136 | if ($cmd->hasSubCmd($argument)) { |
||
| 137 | $subcommand = $cmd->getSubCmd($argument); |
||
| 138 | } |
||
| 139 | |||
| 140 | return $subcommand; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param ContinuousOptionParser $parser |
||
| 145 | * @param Cmd $cmd |
||
| 165 | } |