1 | <?php |
||
13 | class Args implements \JsonSerializable, \ArrayAccess { |
||
14 | protected $command; |
||
15 | protected $opts; |
||
16 | protected $args; |
||
17 | protected $meta; |
||
18 | |||
19 | /** |
||
20 | * Initialize the {@link Args} instance. |
||
21 | * |
||
22 | * @param string $command The name of the command. |
||
23 | * @param array $opts An array of command line options. |
||
24 | * @param array $args A numeric array of command line args. |
||
25 | */ |
||
26 | 43 | public function __construct($command = '', $opts = [], $args = []) { |
|
32 | |||
33 | /** |
||
34 | * Add an argument to the args array. |
||
35 | * |
||
36 | * @param string $value The argument to add. |
||
37 | * @param string|null $index The index to add the arg at. |
||
38 | * @return $this |
||
39 | */ |
||
40 | 4 | public function addArg($value, $index = null) { |
|
41 | 4 | if ($index !== null) { |
|
42 | 3 | $this->args[$index] = $value; |
|
43 | } else { |
||
44 | 1 | $this->args[] = $value; |
|
45 | } |
||
46 | 4 | return $this; |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * Get an argument at a given index. |
||
51 | * |
||
52 | * Arguments can be accessed by name or index. |
||
53 | * |
||
54 | * @param string|int $index |
||
55 | * @param mixed $default The default value to return if the argument is not found. |
||
56 | * @return mixed Returns the argument at {@link $index} or {@link $default}. |
||
57 | */ |
||
58 | 3 | public function getArg($index, $default = null) { |
|
59 | 3 | if (array_key_exists($index, $this->args)) { |
|
60 | 3 | return $this->args[$index]; |
|
61 | 2 | } elseif (is_int($index)) { |
|
62 | 2 | $values = array_values($this->args); |
|
63 | 2 | if (array_key_exists($index, $values)) { |
|
64 | 2 | return $values[$index]; |
|
65 | } |
||
66 | } |
||
67 | 1 | return $default; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Set an argument in the args array. |
||
72 | * |
||
73 | * @param string|int $index The index to set at. |
||
74 | * @param mixed $value The value of the arg. |
||
75 | * @return $this |
||
76 | */ |
||
77 | 1 | public function setArg($index, $value) { |
|
81 | |||
82 | /** |
||
83 | * Get the args array. |
||
84 | * |
||
85 | * @return array Returns the args array. |
||
86 | */ |
||
87 | 35 | public function getArgs() { |
|
90 | |||
91 | /** |
||
92 | * Set the args array. |
||
93 | * |
||
94 | * @param array $args The new args array. |
||
95 | * @return $this |
||
96 | */ |
||
97 | 35 | public function setArgs(array $args) { |
|
101 | |||
102 | /** |
||
103 | * Get the name of the command associated with the args. |
||
104 | * |
||
105 | * @return string Returns the name of the command. |
||
106 | */ |
||
107 | 40 | public function getCommand() { |
|
110 | |||
111 | /** |
||
112 | * Set the name of the command associated with the args. |
||
113 | * |
||
114 | * @param string $command The new command. |
||
115 | * @return $this |
||
116 | */ |
||
117 | 4 | public function setCommand($command) { |
|
121 | |||
122 | /** |
||
123 | * Get a meta value. |
||
124 | * |
||
125 | * @param string $name The name of the meta value. |
||
126 | * @param mixed $default The default value to return if {@link $name} is not found. |
||
127 | * @return mixed Returns the meta value or {@link $default} if it doesn't exist. |
||
128 | */ |
||
129 | 7 | public function getMeta($name, $default = null) { |
|
132 | |||
133 | /** |
||
134 | * Set a meta value. |
||
135 | * |
||
136 | * @param string $name The name of the meta value. |
||
137 | * @param mixed $value The new meta value. |
||
138 | * @return $this |
||
139 | */ |
||
140 | 41 | public function setMeta($name, $value) { |
|
144 | |||
145 | /** |
||
146 | * Gets the entire options array. |
||
147 | * |
||
148 | * @return array Returns the current options array. |
||
149 | */ |
||
150 | 34 | public function getOpts() { |
|
153 | |||
154 | /** |
||
155 | * Sets the entire options array. |
||
156 | * |
||
157 | * @param array $value Pass an array to set a new options array. |
||
158 | * @return $this |
||
159 | */ |
||
160 | 1 | public function setOpts(array $value) { |
|
164 | |||
165 | /** |
||
166 | * Get the value of a passed option. |
||
167 | * |
||
168 | * @param string $option The name of the option to get. |
||
169 | * @param mixed $default The default value if the option does not exist. |
||
170 | * @return mixed Returns the option or {@link $default} if it does not exist. |
||
171 | */ |
||
172 | 38 | public function getOpt($option, $default = null) { |
|
175 | |||
176 | /** |
||
177 | * Set an option. |
||
178 | * |
||
179 | * @param string $option The name of the option. |
||
180 | * @param mixed $value The value of the option. |
||
181 | * @return $this |
||
182 | */ |
||
183 | 39 | public function setOpt($option, $value) { |
|
187 | |||
188 | /** |
||
189 | * Return the json serializable data for the args. |
||
190 | * |
||
191 | * @return array Returns an array of data that can be used to serialize the args to json. |
||
192 | */ |
||
193 | 1 | public function jsonSerialize() { |
|
194 | return [ |
||
195 | 1 | 'command' => $this->command, |
|
196 | 1 | 'opts' => $this->opts, |
|
197 | 1 | 'args' => $this->args, |
|
198 | 1 | 'meta' => $this->meta |
|
199 | ]; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Whether a offset exists. |
||
204 | * |
||
205 | * @param mixed $offset An offset to check for. |
||
206 | * @return boolean true on success or false on failure. |
||
207 | * The return value will be casted to boolean if non-boolean was returned. |
||
208 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
209 | */ |
||
210 | 1 | public function offsetExists($offset) { |
|
213 | |||
214 | /** |
||
215 | * Offset to retrieve. |
||
216 | * |
||
217 | * @param mixed $offset The offset to retrieve. |
||
218 | * @return mixed Can return all value types. |
||
219 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
220 | */ |
||
221 | 2 | public function offsetGet($offset) { |
|
224 | |||
225 | /** |
||
226 | * Offset to set. |
||
227 | * |
||
228 | * @param mixed $offset The offset to assign the value to. |
||
229 | * @param mixed $value The value to set. |
||
230 | * @return void |
||
231 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
232 | */ |
||
233 | 1 | public function offsetSet($offset, $value) { |
|
236 | |||
237 | /** |
||
238 | * Offset to unset. |
||
239 | * |
||
240 | * @param mixed $offset The offset to unset. |
||
241 | * @return void |
||
242 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
243 | */ |
||
244 | 1 | public function offsetUnset($offset) { |
|
247 | } |
||
248 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.