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