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 | * @api |
||
53 | */ |
||
54 | public function __construct(array $argv = null, InputDefinition $definition = null) |
||
67 | |||
68 | protected function setTokens(array $tokens) |
||
72 | |||
73 | /** |
||
74 | * Processes command line arguments. |
||
75 | */ |
||
76 | protected function parse() |
||
94 | |||
95 | /** |
||
96 | * Parses a short option. |
||
97 | * |
||
98 | * @param string $token The current token. |
||
99 | */ |
||
100 | private function parseShortOption($token) |
||
115 | |||
116 | /** |
||
117 | * Parses a short option set. |
||
118 | * |
||
119 | * @param string $name The current token |
||
120 | * |
||
121 | * @throws \RuntimeException When option given doesn't exist |
||
122 | */ |
||
123 | private function parseShortOptionSet($name) |
||
141 | |||
142 | /** |
||
143 | * Parses a long option. |
||
144 | * |
||
145 | * @param string $token The current token |
||
146 | */ |
||
147 | private function parseLongOption($token) |
||
157 | |||
158 | /** |
||
159 | * Parses an argument. |
||
160 | * |
||
161 | * @param string $token The current token |
||
162 | * |
||
163 | * @throws \RuntimeException When too many arguments are given |
||
164 | */ |
||
165 | private function parseArgument($token) |
||
184 | |||
185 | /** |
||
186 | * Adds a short option value. |
||
187 | * |
||
188 | * @param string $shortcut The short option key |
||
189 | * @param mixed $value The value for the option |
||
190 | * |
||
191 | * @throws \RuntimeException When option given doesn't exist |
||
192 | */ |
||
193 | View Code Duplication | private function addShortOption($shortcut, $value) |
|
201 | |||
202 | /** |
||
203 | * Adds a long option value. |
||
204 | * |
||
205 | * @param string $name The long option key |
||
206 | * @param mixed $value The value for the option |
||
207 | * |
||
208 | * @throws \RuntimeException When option given doesn't exist |
||
209 | */ |
||
210 | private function addLongOption($name, $value) |
||
256 | |||
257 | /** |
||
258 | * Returns the first argument from the raw parameters (not parsed). |
||
259 | * |
||
260 | * @return string The value of the first argument or null otherwise |
||
261 | */ |
||
262 | public function getFirstArgument() |
||
272 | |||
273 | /** |
||
274 | * Returns true if the raw parameters (not parsed) contain a value. |
||
275 | * |
||
276 | * This method is to be used to introspect the input parameters |
||
277 | * before they have been validated. It must be used carefully. |
||
278 | * |
||
279 | * @param string|array $values The value(s) to look for in the raw parameters (can be an array) |
||
280 | * |
||
281 | * @return bool true if the value is contained in the raw parameters |
||
282 | */ |
||
283 | public function hasParameterOption($values) |
||
297 | |||
298 | /** |
||
299 | * Returns the value of a raw option (not parsed). |
||
300 | * |
||
301 | * This method is to be used to introspect the input parameters |
||
302 | * before they have been validated. It must be used carefully. |
||
303 | * |
||
304 | * @param string|array $values The value(s) to look for in the raw parameters (can be an array) |
||
305 | * @param mixed $default The default value to return if no result is found |
||
306 | * |
||
307 | * @return mixed The option value |
||
308 | */ |
||
309 | public function getParameterOption($values, $default = false) |
||
330 | |||
331 | /** |
||
332 | * Returns a stringified representation of the args passed to the command. |
||
333 | * |
||
334 | * @return string |
||
335 | */ |
||
336 | public function __toString() |
||
337 | { |
||
338 | $self = $this; |
||
339 | $tokens = array_map(function ($token) use ($self) { |
||
340 | if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) { |
||
341 | return $match[1].$self->escapeToken($match[2]); |
||
342 | } |
||
343 | |||
344 | if ($token && $token[0] !== '-') { |
||
345 | return $self->escapeToken($token); |
||
346 | } |
||
347 | |||
348 | return $token; |
||
349 | }, $this->tokens); |
||
350 | |||
351 | return implode(' ', $tokens); |
||
352 | } |
||
353 | } |
||
354 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: