1 | <?php declare(strict_types=1); |
||
19 | class Parser |
||
20 | { |
||
21 | /** @var array */ |
||
22 | private $boolParamSet = [ |
||
23 | 'y' => true, |
||
24 | 'n' => false, |
||
25 | 'yes' => true, |
||
26 | 'no' => false, |
||
27 | 'true' => true, |
||
28 | 'false' => false, |
||
29 | '1' => true, |
||
30 | '0' => false, |
||
31 | 'on' => true, |
||
32 | 'off' => false, |
||
33 | ]; |
||
34 | |||
35 | /** @var array */ |
||
36 | private $parsedCommands = []; |
||
37 | |||
38 | /** |
||
39 | * Check if parsed parameters has param. |
||
40 | * |
||
41 | * @param string $key The parameter's "key" |
||
42 | * @return bool |
||
43 | */ |
||
44 | 33 | public function has($key): bool |
|
45 | { |
||
46 | 33 | return isset($this->parsedCommands[$key]); |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * Get value from parsed parameters. |
||
51 | * |
||
52 | * @param string $key The parameter's "key" |
||
53 | * @param mixed $default A default value in case the key is not set |
||
54 | * @return mixed |
||
55 | */ |
||
56 | 21 | public function get($key, $default = null) |
|
57 | { |
||
58 | 21 | if (!$this->has($key)) { |
|
59 | 14 | return $default; |
|
60 | } |
||
61 | |||
62 | 7 | return $this->parsedCommands[$key]; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * Get boolean from parsed parameters. |
||
67 | * |
||
68 | * @param string $key The parameter's "key" |
||
69 | * @param bool $default A default value in case the key is not set |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | 12 | public function getBoolean(string $key, bool $default = false): bool |
|
85 | |||
86 | /** |
||
87 | * Parse console input. |
||
88 | * |
||
89 | * @param array $argv Arguments to parse. Defaults to empty array |
||
90 | * @return array |
||
91 | */ |
||
92 | 48 | public function parse(array $argv = []): array |
|
103 | |||
104 | /** |
||
105 | * Gets array of arguments passed from the input. |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | 1 | protected function getArgvFromServer(): array |
|
113 | |||
114 | /** |
||
115 | * Return either received parameter or default |
||
116 | * |
||
117 | * @param string $value The parameter passed |
||
118 | * @param bool $default A default value if the parameter is not set |
||
119 | * |
||
120 | * @return bool |
||
121 | */ |
||
122 | 10 | protected function getCoalescingDefault(string $value, bool $default): bool |
|
126 | |||
127 | /** |
||
128 | * @param string $arg The argument passed |
||
129 | * @param int $eqPos The position of where the equals sign is located |
||
130 | * @return array |
||
131 | */ |
||
132 | 10 | protected function getParamWithEqual(string $arg, int $eqPos): array |
|
139 | |||
140 | /** |
||
141 | * Handle received parameters |
||
142 | * |
||
143 | * @param array $argv The array with the arguments passed in the CLI |
||
144 | * @return array |
||
145 | */ |
||
146 | 48 | protected function handleArguments(array $argv): array |
|
188 | |||
189 | /** |
||
190 | * Parse command `foo=bar` |
||
191 | * |
||
192 | * @param string $command |
||
193 | * @return bool |
||
194 | */ |
||
195 | 42 | protected function parseAndMergeCommandWithEqualSign(string $command): bool |
|
207 | |||
208 | /** |
||
209 | * Delete dashes from param |
||
210 | * |
||
211 | * @param string $argument |
||
212 | * @return string |
||
213 | */ |
||
214 | 27 | protected function stripSlashes(string $argument): string |
|
224 | |||
225 | /** |
||
226 | * |
||
227 | * @return array |
||
228 | */ |
||
229 | 8 | public function getParsedCommands(): array |
|
233 | |||
234 | } |
||
235 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.