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:
| 1 | <?php |
||
| 26 | class ArrayInput extends Input |
||
| 27 | { |
||
| 28 | private $parameters; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Constructor. |
||
| 32 | * |
||
| 33 | * @param array $parameters An array of parameters |
||
| 34 | * @param InputDefinition $definition A InputDefinition instance |
||
| 35 | */ |
||
| 36 | public function __construct(array $parameters, InputDefinition $definition = null) |
||
| 37 | { |
||
| 38 | $this->parameters = $parameters; |
||
| 39 | |||
| 40 | parent::__construct($definition); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Returns the first argument from the raw parameters (not parsed). |
||
| 45 | * |
||
| 46 | * @return string The value of the first argument or null otherwise |
||
| 47 | */ |
||
| 48 | public function getFirstArgument() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Returns true if the raw parameters (not parsed) contain a value. |
||
| 61 | * |
||
| 62 | * This method is to be used to introspect the input parameters |
||
| 63 | * before they have been validated. It must be used carefully. |
||
| 64 | * |
||
| 65 | * @param string|array $values The values to look for in the raw parameters (can be an array) |
||
| 66 | * @param bool $onlyParams Only check real parameters, skip those following an end of options (--) signal |
||
| 67 | * |
||
| 68 | * @return bool true if the value is contained in the raw parameters |
||
| 69 | */ |
||
| 70 | public function hasParameterOption($values, $onlyParams = false) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Returns the value of a raw option (not parsed). |
||
| 93 | * |
||
| 94 | * This method is to be used to introspect the input parameters |
||
| 95 | * before they have been validated. It must be used carefully. |
||
| 96 | * |
||
| 97 | * @param string|array $values The value(s) to look for in the raw parameters (can be an array) |
||
| 98 | * @param mixed $default The default value to return if no result is found |
||
| 99 | * @param bool $onlyParams Only check real parameters, skip those following an end of options (--) signal |
||
| 100 | * |
||
| 101 | * @return mixed The option value |
||
| 102 | */ |
||
| 103 | public function getParameterOption($values, $default = false, $onlyParams = false) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns a stringified representation of the args passed to the command. |
||
| 126 | * |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | public function __toString() |
||
| 130 | { |
||
| 131 | $params = array(); |
||
| 132 | foreach ($this->parameters as $param => $val) { |
||
| 133 | if ($param && '-' === $param[0]) { |
||
| 134 | $params[] = $param.('' != $val ? '='.$this->escapeToken($val) : ''); |
||
| 135 | } else { |
||
| 136 | $params[] = $this->escapeToken($val); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | return implode(' ', $params); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Processes command line arguments. |
||
| 145 | */ |
||
| 146 | protected function parse() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Adds a short option value. |
||
| 164 | * |
||
| 165 | * @param string $shortcut The short option key |
||
| 166 | * @param mixed $value The value for the option |
||
| 167 | * |
||
| 168 | * @throws InvalidOptionException When option given doesn't exist |
||
| 169 | */ |
||
| 170 | View Code Duplication | private function addShortOption($shortcut, $value) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Adds a long option value. |
||
| 181 | * |
||
| 182 | * @param string $name The long option key |
||
| 183 | * @param mixed $value The value for the option |
||
| 184 | * |
||
| 185 | * @throws InvalidOptionException When option given doesn't exist |
||
| 186 | * @throws InvalidOptionException When a required value is missing |
||
| 187 | */ |
||
| 188 | private function addLongOption($name, $value) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Adds an argument value. |
||
| 209 | * |
||
| 210 | * @param string $name The argument name |
||
| 211 | * @param mixed $value The value for the argument |
||
| 212 | * |
||
| 213 | * @throws InvalidArgumentException When argument given doesn't exist |
||
| 214 | */ |
||
| 215 | View Code Duplication | private function addArgument($name, $value) |
|
| 223 | } |
||
| 224 |