1 | <?php |
||
33 | class DefaultArgsParser extends ArgvInput implements ArgsParser |
||
34 | { |
||
35 | /** |
||
36 | * Creates a new parser. |
||
37 | */ |
||
38 | 307 | public function __construct() |
|
39 | { |
||
40 | // Hide the parent arguments from the public signature |
||
41 | 307 | parent::__construct(); |
|
42 | 307 | } |
|
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | 303 | public function parseArgs(RawArgs $args, ArgsFormat $format, $lenient = false) |
|
48 | { |
||
49 | 303 | $this->setTokens($args->getTokens()); |
|
50 | |||
51 | 303 | $formatAdapter = new ArgsFormatInputDefinition($format); |
|
52 | |||
53 | try { |
||
54 | 303 | $this->bind($formatAdapter); |
|
55 | 13 | } catch (RuntimeException $e) { |
|
56 | 13 | if (!$lenient) { |
|
57 | 6 | throw new CannotParseArgsException($e->getMessage()); |
|
58 | } |
||
59 | } |
||
60 | |||
61 | // Prevent failing validation if not all command names are given |
||
62 | 297 | $this->insertMissingCommandNames($formatAdapter, $lenient); |
|
63 | |||
64 | try { |
||
65 | 296 | $this->validate(); |
|
66 | 6 | } catch (RuntimeException $e) { |
|
67 | 6 | if (!$lenient) { |
|
68 | 3 | throw new CannotParseArgsException($e->getMessage()); |
|
69 | } |
||
70 | } |
||
71 | |||
72 | 293 | return $this->createArgs($format, $args); |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Creates the arguments from the current class state. |
||
77 | * |
||
78 | * @param ArgsFormat $format The format. |
||
79 | * @param RawArgs $rawArgs The raw arguments. |
||
80 | * |
||
81 | * @return Args The created console arguments. |
||
82 | */ |
||
83 | 293 | private function createArgs(ArgsFormat $format, RawArgs $rawArgs) |
|
84 | { |
||
85 | 293 | $args = new Args($format, $rawArgs); |
|
86 | |||
87 | 293 | foreach ($this->arguments as $name => $value) { |
|
88 | // Filter command names |
||
89 | 290 | if ($format->hasArgument($name)) { |
|
90 | 290 | $args->setArgument($name, $value); |
|
91 | } |
||
92 | } |
||
93 | |||
94 | 293 | foreach ($this->options as $name => $value) { |
|
95 | // Filter command options |
||
96 | 235 | if ($format->hasOption($name)) { |
|
97 | 235 | $args->setOption($name, $value); |
|
98 | } |
||
99 | } |
||
100 | |||
101 | 293 | return $args; |
|
102 | } |
||
103 | |||
104 | 297 | private function insertMissingCommandNames(ArgsFormatInputDefinition $inputDefinition, $lenient = false) |
|
105 | { |
||
106 | // Start with the default values of the arguments. |
||
107 | 297 | $inputArguments = $inputDefinition->getArguments(); |
|
108 | 297 | $fixedValues = array(); |
|
109 | 297 | $commandNames = $inputDefinition->getCommandNamesByArgumentName(); |
|
110 | |||
111 | // Flatten the actual arguments, in case they contain a multi-valued |
||
112 | // argument. |
||
113 | 297 | $actualValues = $this->flatten($this->arguments); |
|
114 | |||
115 | // Reset all array pointers. |
||
116 | 297 | reset($commandNames); |
|
117 | 297 | reset($actualValues); |
|
118 | 297 | reset($inputArguments); |
|
119 | |||
120 | // Skip the command names. The resulting pointer is like this: |
||
121 | // |
||
122 | // actual: [ 0: remote, 1: origin, 2: foo/bar ] |
||
|
|||
123 | // ^ |
||
124 | |||
125 | 297 | $this->skipCommandNames($actualValues, $commandNames); |
|
126 | |||
127 | // Copy the command names into the fixed array. The result is: |
||
128 | // |
||
129 | // fixed: [ cmd1: remote, cmd2: add ] |
||
130 | |||
131 | 297 | $this->copyArgumentValues($commandNames, $inputArguments, $fixedValues, $lenient); |
|
132 | |||
133 | // Copy the remaining actual values. The result is: |
||
134 | // |
||
135 | // fixed: [ cmd1: remote, cmd2: add, name: origin, target: foo/bar ] |
||
136 | |||
137 | 297 | $this->copyArgumentValues($actualValues, $inputArguments, $fixedValues, $lenient); |
|
138 | |||
139 | // Overwrite all current arguments with the fixed values |
||
140 | 296 | foreach ($fixedValues as $name => $value) { |
|
141 | 293 | $this->arguments[$name] = $value; |
|
142 | } |
||
143 | 296 | } |
|
144 | |||
145 | 297 | private function flatten(array $arguments, array &$result = array()) |
|
146 | { |
||
147 | 297 | foreach ($arguments as $value) { |
|
148 | 238 | if (is_array($value)) { |
|
149 | 3 | $this->flatten($value, $result); |
|
150 | } else { |
||
151 | 238 | $result[] = $value; |
|
152 | } |
||
153 | } |
||
154 | |||
155 | 297 | return $result; |
|
156 | } |
||
157 | |||
158 | 297 | private function skipCommandNames(array &$arguments, array $commandNames) |
|
167 | |||
168 | 297 | private function copyArgumentValues(array &$actualValues, array &$inputArguments, array &$fixedValues, $lenient = false) |
|
169 | { |
||
170 | // The starting point are two arrays of arguments with the array |
||
171 | // pointers set: |
||
172 | |||
232 | } |
||
233 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.