Skip to content

Commit 4fd98bf

Browse files
committed
Level 6
1 parent 7327bea commit 4fd98bf

24 files changed

Lines changed: 365 additions & 66 deletions

lib/cli/Arguments.php

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,23 @@
1919

2020
/**
2121
* Parses command line arguments.
22+
*
23+
* @implements \ArrayAccess<string, mixed>
2224
*/
2325
class 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
}

lib/cli/Colors.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Reference: http://graphcomp.com/info/specs/ansi_col.html#colors
1919
*/
2020
class Colors {
21+
/** @var array<string, array<string, int>> */
2122
static protected $_colors = array(
2223
'color' => array(
2324
'black' => 30,
@@ -48,14 +49,28 @@ class Colors {
4849
'white' => 47
4950
)
5051
);
52+
/** @var bool|null */
5153
static protected $_enabled = null;
5254

55+
/** @var array<string, array<string, string>> */
5356
static protected $_string_cache = array();
5457

58+
/**
59+
* Enable colorized output.
60+
*
61+
* @param bool $force Force enable.
62+
* @return void
63+
*/
5564
static public function enable($force = true) {
5665
self::$_enabled = $force === true ? true : null;
5766
}
5867

68+
/**
69+
* Disable colorized output.
70+
*
71+
* @param bool $force Force disable.
72+
* @return void
73+
*/
5974
static public function disable($force = true) {
6075
self::$_enabled = $force === true ? false : null;
6176
}
@@ -64,6 +79,9 @@ static public function disable($force = true) {
6479
* Check if we should colorize output based on local flags and shell type.
6580
*
6681
* Only check the shell type if `Colors::$_enabled` is null and `$colored` is null.
82+
*
83+
* @param bool|null $colored Force enable or disable the colorized output.
84+
* @return bool
6785
*/
6886
static public function shouldColorize($colored = null) {
6987
return self::$_enabled === true ||
@@ -75,8 +93,8 @@ static public function shouldColorize($colored = null) {
7593
/**
7694
* Set the color.
7795
*
78-
* @param string $color The name of the color or style to set.
79-
* @return string
96+
* @param string|array<string, string|int> $color The name of the color or style to set, or an array of options.
97+
* @return string
8098
*/
8199
static public function color($color) {
82100
if (!is_array($color)) {
@@ -171,6 +189,7 @@ static public function decolorize( $string, $keep = 0 ) {
171189
* @param string $passed The original string before colorization.
172190
* @param string $colorized The string after running through self::colorize.
173191
* @param string $deprecated Optional. Not used. Default null.
192+
* @return void
174193
*/
175194
static public function cacheString( $passed, $colorized, $deprecated = null ) {
176195
self::$_string_cache[md5($passed)] = array(
@@ -225,7 +244,7 @@ static public function pad( $string, $length, $pre_colorized = false, $encoding
225244
/**
226245
* Get the color mapping array.
227246
*
228-
* @return array Array of color tokens mapped to colors and styles.
247+
* @return array<string, array<string, string|int>> Array of color tokens mapped to colors and styles.
229248
*/
230249
static public function getColors() {
231250
return array(
@@ -268,14 +287,16 @@ static public function getColors() {
268287
/**
269288
* Get the cached string values.
270289
*
271-
* @return array The cached string values.
290+
* @return array<string, array<string, string>> The cached string values.
272291
*/
273292
static public function getStringCache() {
274293
return self::$_string_cache;
275294
}
276295

277296
/**
278297
* Clear the string cache.
298+
*
299+
* @return void
279300
*/
280301
static public function clearStringCache() {
281302
self::$_string_cache = array();
@@ -305,7 +326,7 @@ static public function getResetCode() {
305326
* @param string $string The string to wrap (with ANSI codes).
306327
* @param int $width The maximum display width per line.
307328
* @param string|bool $encoding Optional. The encoding of the string. Default false.
308-
* @return array Array of wrapped string segments.
329+
* @return array<int, string> Array of wrapped string segments.
309330
*/
310331
static public function wrapPreColorized( $string, $width, $encoding = false ) {
311332
$wrapped = array();

lib/cli/Memoize.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@
1313
namespace cli;
1414

1515
abstract class Memoize {
16+
/** @var array<string, mixed> */
1617
protected $_memoCache = array();
1718

19+
/**
20+
* Magic getter to retrieve memoized properties.
21+
*
22+
* @param string $name Property name.
23+
* @return mixed
24+
*/
1825
public function __get($name) {
1926
if (isset($this->_memoCache[$name])) {
2027
return $this->_memoCache[$name];
@@ -34,6 +41,12 @@ public function __get($name) {
3441
return $this->_memoCache[$name];
3542
}
3643

44+
/**
45+
* Unmemoize a property or all properties.
46+
*
47+
* @param string|bool $name Property name to unmemoize, or true to unmemoize all.
48+
* @return void
49+
*/
3750
protected function _unmemo($name) {
3851
if ($name === true) {
3952
$this->_memoCache = array();

0 commit comments

Comments
 (0)