1919
2020/**
2121 * Parses command line arguments.
22+ *
23+ * @implements \ArrayAccess<string, mixed>
2224 */
2325class Arguments implements \ArrayAccess {
26+ /** @var array<string, array<string, mixed>> */
2427 protected $ _flags = array ();
28+ /** @var array<string, array<string, mixed>> */
2529 protected $ _options = array ();
30+ /** @var bool */
2631 protected $ _strict = false ;
32+ /** @var array<int, string> */
2733 protected $ _input = array ();
34+ /** @var array<int, string> */
2835 protected $ _invalid = array ();
36+ /** @var array<string, mixed>|null */
2937 protected $ _parsed ;
38+ /** @var Lexer|null */
3039 protected $ _lexer ;
3140
3241 /**
@@ -36,7 +45,7 @@ class Arguments implements \ArrayAccess {
3645 *
3746 * `'help'` is `true` by default, `'strict'` is false by default.
3847 *
39- * @param array $options An array of options for this parser.
48+ * @param array<string, mixed> $options An array of options for this parser.
4049 */
4150 public function __construct ($ options = array ()) {
4251 $ options += array (
@@ -58,7 +67,7 @@ public function __construct($options = array()) {
5867 /**
5968 * Get the list of arguments found by the defined definitions.
6069 *
61- * @return array
70+ * @return array<string, mixed>
6271 */
6372 public function getArguments () {
6473 if (!isset ($ this ->_parsed )) {
@@ -67,6 +76,11 @@ public function getArguments() {
6776 return $ this ->_parsed ;
6877 }
6978
79+ /**
80+ * Get the help screen.
81+ *
82+ * @return HelpScreen
83+ */
7084 public function getHelpScreen () {
7185 return new HelpScreen ($ this );
7286 }
@@ -147,7 +161,7 @@ public function offsetUnset($offset) {
147161 * Adds a flag (boolean argument) to the argument list.
148162 *
149163 * @param mixed $flag A string representing the flag, or an array of strings.
150- * @param array $settings An array of settings for this flag.
164+ * @param array<string, mixed>|string $settings An array of settings for this flag.
151165 * @setting string description A description to be shown in --help.
152166 * @setting bool default The default value for this flag.
153167 * @setting bool stackable Whether the flag is repeatable to increase the value.
@@ -183,7 +197,7 @@ public function addFlag($flag, $settings = array()) {
183197 * primary flag character, and the values should be the settings array
184198 * used by {addFlag}.
185199 *
186- * @param array $flags An array of flags to add
200+ * @param array<string, array<string, mixed>|string> $flags An array of flags to add
187201 * @return $this
188202 */
189203 public function addFlags ($ flags ) {
@@ -203,7 +217,7 @@ public function addFlags($flags) {
203217 * Adds an option (string argument) to the argument list.
204218 *
205219 * @param mixed $option A string representing the option, or an array of strings.
206- * @param array $settings An array of settings for this option.
220+ * @param array<string, mixed>|string $settings An array of settings for this option.
207221 * @setting string description A description to be shown in --help.
208222 * @setting bool default The default value for this option.
209223 * @setting array aliases Other ways to trigger this option.
@@ -237,7 +251,7 @@ public function addOption($option, $settings = array()) {
237251 * primary option string, and the values should be the settings array
238252 * used by {addOption}.
239253 *
240- * @param array $options An array of options to add
254+ * @param array<string, array<string, mixed>|string> $options An array of options to add
241255 * @return $this
242256 */
243257 public function addOptions ($ options ) {
@@ -271,7 +285,7 @@ public function setStrict($strict) {
271285 /**
272286 * Get the list of invalid arguments the parser found.
273287 *
274- * @return array
288+ * @return array<int, string>
275289 */
276290 public function getInvalidArguments () {
277291 return $ this ->_invalid ;
@@ -282,7 +296,7 @@ public function getInvalidArguments() {
282296 *
283297 * @param mixed $flag Either a string representing the flag or an
284298 * cli\arguments\Argument object.
285- * @return array|null
299+ * @return array<string, mixed> |null
286300 */
287301 public function getFlag ($ flag ) {
288302 if ($ flag instanceOf Argument) {
@@ -308,10 +322,20 @@ public function getFlag($flag) {
308322 return null ;
309323 }
310324
325+ /**
326+ * Get all flags.
327+ *
328+ * @return array<string, array<string, mixed>>
329+ */
311330 public function getFlags () {
312331 return $ this ->_flags ;
313332 }
314333
334+ /**
335+ * Check if there are any flags defined.
336+ *
337+ * @return bool
338+ */
315339 public function hasFlags () {
316340 return !empty ($ this ->_flags );
317341 }
@@ -345,7 +369,7 @@ public function isStackable($flag) {
345369 *
346370 * @param mixed $option Either a string representing the option or an
347371 * cli\arguments\Argument object.
348- * @return array|null
372+ * @return array<string, mixed> |null
349373 */
350374 public function getOption ($ option ) {
351375 if ($ option instanceOf Argument) {
@@ -370,10 +394,20 @@ public function getOption($option) {
370394 return null ;
371395 }
372396
397+ /**
398+ * Get all options.
399+ *
400+ * @return array<string, array<string, mixed>>
401+ */
373402 public function getOptions () {
374403 return $ this ->_options ;
375404 }
376405
406+ /**
407+ * Check if there are any options defined.
408+ *
409+ * @return bool
410+ */
377411 public function hasOptions () {
378412 return !empty ($ this ->_options );
379413 }
@@ -424,6 +458,8 @@ public function parse() {
424458 * This applies the default values, if any, of all of the
425459 * flags and options, so that if there is a default value
426460 * it will be available.
461+ *
462+ * @return void
427463 */
428464 private function _applyDefaults () {
429465 foreach ($ this ->_flags as $ flag => $ settings ) {
@@ -438,10 +474,22 @@ private function _applyDefaults() {
438474 }
439475 }
440476
477+ /**
478+ * Warn about something.
479+ *
480+ * @param string $message
481+ * @return void
482+ */
441483 private function _warn ($ message ) {
442484 trigger_error ('[ ' . __CLASS__ .'] ' . $ message , E_USER_WARNING );
443485 }
444486
487+ /**
488+ * Parse a flag.
489+ *
490+ * @param Argument $argument
491+ * @return bool
492+ */
445493 private function _parseFlag ($ argument ) {
446494 if (!$ this ->isFlag ($ argument )) {
447495 return false ;
@@ -460,6 +508,12 @@ private function _parseFlag($argument) {
460508 return true ;
461509 }
462510
511+ /**
512+ * Parse an option.
513+ *
514+ * @param Argument $option
515+ * @return bool
516+ */
463517 private function _parseOption ($ option ) {
464518 if (!$ this ->isOption ($ option )) {
465519 return false ;
@@ -490,7 +544,6 @@ private function _parseOption($option) {
490544 $ value = $ this ->_lexer ->current ();
491545 array_push ( $ values , $ value ->raw );
492546
493- // @phpstan-ignore-next-line
494547 if ( ! $ this ->_lexer ->end () && ! $ this ->_lexer ->peek ->isValue ) {
495548 break ;
496549 }
0 commit comments