1 | <?php |
||
18 | class SignatureParser |
||
19 | { |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $name; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private $arguments = []; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $options = []; |
||
34 | |||
35 | /** |
||
36 | * SignatureParser constructor. |
||
37 | * @param string $signature |
||
38 | * @throws \InvalidArgumentException |
||
39 | */ |
||
40 | public function __construct(string $signature) |
||
44 | |||
45 | /** |
||
46 | * @return string |
||
47 | */ |
||
48 | public function getName(): string |
||
52 | |||
53 | /** |
||
54 | * @return array |
||
55 | */ |
||
56 | public function getArguments(): array |
||
60 | |||
61 | /** |
||
62 | * @return array |
||
63 | */ |
||
64 | public function getOptions(): array |
||
68 | |||
69 | /** |
||
70 | * Parse the given console command definition into an array. |
||
71 | * |
||
72 | * @param string $expression |
||
73 | * @return array |
||
74 | * @throws \InvalidArgumentException |
||
75 | */ |
||
76 | protected function parse($expression): array |
||
86 | |||
87 | /** |
||
88 | * Extract the name of the command from the expression. |
||
89 | * |
||
90 | * @param string $expression |
||
91 | * @return string |
||
92 | * @throws \InvalidArgumentException |
||
93 | */ |
||
94 | protected function name($expression): string |
||
106 | |||
107 | /** |
||
108 | * Extract all of the parameters from the tokens. |
||
109 | * |
||
110 | * @param array $tokens |
||
111 | * @return array |
||
112 | * @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
||
113 | */ |
||
114 | protected function parameters(array $tokens): array |
||
128 | |||
129 | /** |
||
130 | * Parse an option expression. |
||
131 | * |
||
132 | * @param string $token |
||
133 | * @return \Symfony\Component\Console\Input\InputOption |
||
134 | * @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
||
135 | */ |
||
136 | protected function parseOption($token): ?InputOption |
||
165 | |||
166 | /** |
||
167 | * Parse the token into its token and description segments. |
||
168 | * |
||
169 | * @param string $token |
||
170 | * @return array |
||
171 | */ |
||
172 | protected function extractDescription($token): array |
||
178 | |||
179 | /** |
||
180 | * @param string $haystack |
||
181 | * @param string $needle |
||
182 | * @return bool |
||
183 | */ |
||
184 | private function endsWith(string $haystack, string $needle): bool |
||
188 | |||
189 | /** |
||
190 | * Parse an argument expression. |
||
191 | * |
||
192 | * @param string $token |
||
193 | * @return \Symfony\Component\Console\Input\InputArgument |
||
194 | * @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
||
195 | */ |
||
196 | protected function parseArgument($token): ?InputArgument |
||
220 | } |
||
221 |
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.