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 ArgvInput 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 ArgvInput, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class ArgvInput extends Input |
||
| 42 | { |
||
| 43 | private $tokens; |
||
| 44 | private $parsed; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Constructor. |
||
| 48 | * |
||
| 49 | * @param array $argv An array of parameters from the CLI (in the argv format) |
||
| 50 | * @param InputDefinition $definition A InputDefinition instance |
||
| 51 | */ |
||
| 52 | public function __construct(array $argv = null, InputDefinition $definition = null) |
||
| 53 | { |
||
| 54 | if (null === $argv) { |
||
| 55 | $argv = $_SERVER['argv']; |
||
| 56 | } |
||
| 57 | |||
| 58 | // strip the application name |
||
| 59 | array_shift($argv); |
||
| 60 | |||
| 61 | $this->tokens = $argv; |
||
| 62 | |||
| 63 | parent::__construct($definition); |
||
| 64 | } |
||
| 65 | |||
| 66 | protected function setTokens(array $tokens) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Processes command line arguments. |
||
| 73 | */ |
||
| 74 | protected function parse() |
||
| 75 | { |
||
| 76 | $parseOptions = true; |
||
| 77 | $this->parsed = $this->tokens; |
||
| 78 | while (null !== $token = array_shift($this->parsed)) { |
||
| 79 | if ($parseOptions && '' == $token) { |
||
| 80 | $this->parseArgument($token); |
||
| 81 | } elseif ($parseOptions && '--' == $token) { |
||
| 82 | $parseOptions = false; |
||
| 83 | } elseif ($parseOptions && 0 === strpos($token, '--')) { |
||
| 84 | $this->parseLongOption($token); |
||
| 85 | } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) { |
||
| 86 | $this->parseShortOption($token); |
||
| 87 | } else { |
||
| 88 | $this->parseArgument($token); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Parses a short option. |
||
| 95 | * |
||
| 96 | * @param string $token The current token. |
||
| 97 | */ |
||
| 98 | private function parseShortOption($token) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Parses a short option set. |
||
| 116 | * |
||
| 117 | * @param string $name The current token |
||
| 118 | * |
||
| 119 | * @throws RuntimeException When option given doesn't exist |
||
| 120 | */ |
||
| 121 | private function parseShortOptionSet($name) |
||
| 122 | { |
||
| 123 | $len = strlen($name); |
||
| 124 | for ($i = 0; $i < $len; ++$i) { |
||
| 125 | if (!$this->definition->hasShortcut($name[$i])) { |
||
| 126 | throw new RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i])); |
||
| 127 | } |
||
| 128 | |||
| 129 | $option = $this->definition->getOptionForShortcut($name[$i]); |
||
| 130 | if ($option->acceptValue()) { |
||
| 131 | $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1)); |
||
| 132 | |||
| 133 | break; |
||
| 134 | } else { |
||
| 135 | $this->addLongOption($option->getName(), null); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Parses a long option. |
||
| 142 | * |
||
| 143 | * @param string $token The current token |
||
| 144 | */ |
||
| 145 | View Code Duplication | private function parseLongOption($token) |
|
| 146 | { |
||
| 147 | $name = substr($token, 2); |
||
| 148 | |||
| 149 | if (false !== $pos = strpos($name, '=')) { |
||
| 150 | $this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1)); |
||
| 151 | } else { |
||
| 152 | $this->addLongOption($name, null); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Parses an argument. |
||
| 158 | * |
||
| 159 | * @param string $token The current token |
||
| 160 | * |
||
| 161 | * @throws RuntimeException When too many arguments are given |
||
| 162 | */ |
||
| 163 | private function parseArgument($token) |
||
| 164 | { |
||
| 165 | $c = count($this->arguments); |
||
| 166 | |||
| 167 | // if input is expecting another argument, add it |
||
| 168 | if ($this->definition->hasArgument($c)) { |
||
| 169 | $arg = $this->definition->getArgument($c); |
||
| 170 | $this->arguments[$arg->getName()] = $arg->isArray() ? array($token) : $token; |
||
| 171 | |||
| 172 | // if last argument isArray(), append token to last argument |
||
| 173 | } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) { |
||
| 174 | $arg = $this->definition->getArgument($c - 1); |
||
| 175 | $this->arguments[$arg->getName()][] = $token; |
||
| 176 | |||
| 177 | // unexpected argument |
||
| 178 | } else { |
||
| 179 | throw new RuntimeException('Too many arguments.'); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Adds a short option value. |
||
| 185 | * |
||
| 186 | * @param string $shortcut The short option key |
||
| 187 | * @param mixed $value The value for the option |
||
| 188 | * |
||
| 189 | * @throws RuntimeException When option given doesn't exist |
||
| 190 | */ |
||
| 191 | View Code Duplication | private function addShortOption($shortcut, $value) |
|
| 192 | { |
||
| 193 | if (!$this->definition->hasShortcut($shortcut)) { |
||
| 194 | throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut)); |
||
| 195 | } |
||
| 196 | |||
| 197 | $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Adds a long option value. |
||
| 202 | * |
||
| 203 | * @param string $name The long option key |
||
| 204 | * @param mixed $value The value for the option |
||
| 205 | * |
||
| 206 | * @throws RuntimeException When option given doesn't exist |
||
| 207 | */ |
||
| 208 | private function addLongOption($name, $value) |
||
| 209 | { |
||
| 210 | if (!$this->definition->hasOption($name)) { |
||
| 211 | throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name)); |
||
| 212 | } |
||
| 213 | |||
| 214 | $option = $this->definition->getOption($name); |
||
| 215 | |||
| 216 | // Convert empty values to null |
||
| 217 | if (!isset($value[0])) { |
||
| 218 | $value = null; |
||
| 219 | } |
||
| 220 | |||
| 221 | if (null !== $value && !$option->acceptValue()) { |
||
| 222 | throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); |
||
| 223 | } |
||
| 224 | |||
| 225 | if (null === $value && $option->acceptValue() && count($this->parsed)) { |
||
| 226 | // if option accepts an optional or mandatory argument |
||
| 227 | // let's see if there is one provided |
||
| 228 | $next = array_shift($this->parsed); |
||
| 229 | if (isset($next[0]) && '-' !== $next[0]) { |
||
| 230 | $value = $next; |
||
| 231 | } elseif (empty($next)) { |
||
| 232 | $value = ''; |
||
| 233 | } else { |
||
| 234 | array_unshift($this->parsed, $next); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | if (null === $value) { |
||
| 239 | if ($option->isValueRequired()) { |
||
| 240 | throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name)); |
||
| 241 | } |
||
| 242 | |||
| 243 | if (!$option->isArray()) { |
||
| 244 | $value = $option->isValueOptional() ? $option->getDefault() : true; |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | if ($option->isArray()) { |
||
| 249 | $this->options[$name][] = $value; |
||
| 250 | } else { |
||
| 251 | $this->options[$name] = $value; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Returns the first argument from the raw parameters (not parsed). |
||
| 257 | * |
||
| 258 | * @return string The value of the first argument or null otherwise |
||
| 259 | */ |
||
| 260 | public function getFirstArgument() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Returns true if the raw parameters (not parsed) contain a value. |
||
| 273 | * |
||
| 274 | * This method is to be used to introspect the input parameters |
||
| 275 | * before they have been validated. It must be used carefully. |
||
| 276 | * |
||
| 277 | * @param string|array $values The value(s) to look for in the raw parameters (can be an array) |
||
| 278 | * @param bool $onlyParams Only check real parameters, skip those following an end of options (--) signal |
||
| 279 | * |
||
| 280 | * @return bool true if the value is contained in the raw parameters |
||
| 281 | */ |
||
| 282 | public function hasParameterOption($values, $onlyParams = false) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Returns the value of a raw option (not parsed). |
||
| 302 | * |
||
| 303 | * This method is to be used to introspect the input parameters |
||
| 304 | * before they have been validated. It must be used carefully. |
||
| 305 | * |
||
| 306 | * @param string|array $values The value(s) to look for in the raw parameters (can be an array) |
||
| 307 | * @param mixed $default The default value to return if no result is found |
||
| 308 | * @param bool $onlyParams Only check real parameters, skip those following an end of options (--) signal |
||
| 309 | * |
||
| 310 | * @return mixed The option value |
||
| 311 | */ |
||
| 312 | public function getParameterOption($values, $default = false, $onlyParams = false) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Returns a stringified representation of the args passed to the command. |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function __toString() |
||
| 343 | { |
||
| 344 | $tokens = array_map(function ($token) { |
||
| 345 | if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) { |
||
| 346 | return $match[1].$this->escapeToken($match[2]); |
||
| 347 | } |
||
| 348 | |||
| 349 | if ($token && $token[0] !== '-') { |
||
| 350 | return $this->escapeToken($token); |
||
| 351 | } |
||
| 352 | |||
| 353 | return $token; |
||
| 354 | }, $this->tokens); |
||
| 355 | |||
| 356 | return implode(' ', $tokens); |
||
| 357 | } |
||
| 358 | } |
||
| 359 |