Skip to content

Commit 4a04ffb

Browse files
authored
Add initial PHPStan configuration (#201)
1 parent c3d2513 commit 4a04ffb

26 files changed

+911
-366
lines changed

lib/cli/Arguments.php

Lines changed: 157 additions & 34 deletions
Large diffs are not rendered by default.

lib/cli/Colors.php

Lines changed: 31 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();
@@ -319,6 +340,10 @@ static public function wrapPreColorized( $string, $width, $encoding = false ) {
319340
// Split the string into parts: ANSI codes and text
320341
$parts = preg_split( $ansi_pattern, $string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
321342

343+
if ( false === $parts ) {
344+
$parts = array( $string );
345+
}
346+
322347
foreach ( $parts as $part ) {
323348
// Check if this part is an ANSI code
324349
if ( preg_match( $ansi_pattern, $part ) ) {
@@ -340,6 +365,7 @@ static public function wrapPreColorized( $string, $width, $encoding = false ) {
340365

341366
while ( $offset < $text_length ) {
342367
$char = \cli\safe_substr( $part, $offset, 1, false, $encoding );
368+
assert( is_string( $char ) );
343369
$char_width = \cli\strwidth( $char, $encoding );
344370

345371
// Check if adding this character would exceed the width

lib/cli/Memoize.php

Lines changed: 14 additions & 2 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];
@@ -29,11 +36,16 @@ public function __get($name) {
2936
return ($this->_memoCache[$name] = null);
3037
}
3138

32-
$method = array($this, $name);
33-
($this->_memoCache[$name] = call_user_func($method));
39+
($this->_memoCache[$name] = $this->$name());
3440
return $this->_memoCache[$name];
3541
}
3642

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

lib/cli/Notify.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,23 @@
2424
* of characters to indicate progress is being made.
2525
*/
2626
abstract class Notify {
27+
/** @var int */
2728
protected $_current = 0;
29+
/** @var bool */
2830
protected $_first = true;
31+
/** @var int */
2932
protected $_interval;
33+
/** @var string */
3034
protected $_message;
35+
/** @var int|null */
3136
protected $_start;
37+
/** @var float|null */
3238
protected $_timer;
39+
/** @var float|int|null */
3340
protected $_tick;
41+
/** @var int */
3442
protected $_iteration = 0;
43+
/** @var float|int */
3544
protected $_speed = 0;
3645

3746
/**
@@ -52,11 +61,14 @@ public function __construct($msg, $interval = 100) {
5261
* @abstract
5362
* @param boolean $finish
5463
* @see cli\Notify::tick()
64+
* @return void
5565
*/
5666
abstract public function display($finish = false);
5767

5868
/**
5969
* Reset the notifier state so the same instance can be used in multiple loops.
70+
*
71+
* @return void
6072
*/
6173
public function reset() {
6274
$this->_current = 0;
@@ -92,7 +104,7 @@ public function elapsed() {
92104
* Calculates the speed (number of ticks per second) at which the Notifier
93105
* is being updated.
94106
*
95-
* @return int The number of ticks performed in 1 second.
107+
* @return float|int The number of ticks performed in 1 second.
96108
*/
97109
public function speed() {
98110
if (!$this->_start) {
@@ -120,14 +132,15 @@ public function speed() {
120132
* @return string The formatted time span.
121133
*/
122134
public function formatTime($time) {
123-
return floor($time / 60) . ':' . str_pad($time % 60, 2, 0, STR_PAD_LEFT);
135+
return sprintf('%02d:%02d', (int)floor($time / 60), $time % 60);
124136
}
125137

126138
/**
127139
* Finish our Notification display. Should be called after the Notifier is
128140
* no longer needed.
129141
*
130142
* @see cli\Notify::display()
143+
* @return void
131144
*/
132145
public function finish() {
133146
Streams::out("\r");
@@ -140,6 +153,7 @@ public function finish() {
140153
* the ticker is incremented by 1.
141154
*
142155
* @param int $increment The amount to increment by.
156+
* @return void
143157
*/
144158
public function increment($increment = 1) {
145159
$this->_current += $increment;
@@ -174,6 +188,7 @@ public function shouldUpdate() {
174188
* @see cli\Notify::increment()
175189
* @see cli\Notify::shouldUpdate()
176190
* @see cli\Notify::display()
191+
* @return void
177192
*/
178193
public function tick($increment = 1) {
179194
$this->increment($increment);

lib/cli/Progress.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* @see cli\Notify
2121
*/
2222
abstract class Progress extends \cli\Notify {
23+
/** @var int */
2324
protected $_total = 0;
2425

2526
/**
@@ -40,6 +41,7 @@ public function __construct($msg, $total, $interval = 100) {
4041
*
4142
* @param int $total The total number of times this indicator should be `tick`ed.
4243
* @throws \InvalidArgumentException Thrown if the `$total` is less than 0.
44+
* @return void
4345
*/
4446
public function setTotal($total) {
4547
$this->_total = (int)$total;
@@ -51,6 +53,9 @@ public function setTotal($total) {
5153

5254
/**
5355
* Reset the progress state so the same instance can be used in multiple loops.
56+
*
57+
* @param int|null $total Optional new total.
58+
* @return void
5459
*/
5560
public function reset($total = null) {
5661
parent::reset();
@@ -85,8 +90,8 @@ public function total() {
8590
* Calculates the estimated total time for the tick count to reach the
8691
* total ticks given.
8792
*
88-
* @return int The estimated total number of seconds for all ticks to be
89-
* completed. This is not the estimated time left, but total.
93+
* @return int|float The estimated total number of seconds for all ticks to be
94+
* completed. This is not the estimated time left, but total.
9095
* @see cli\Notify::speed()
9196
* @see cli\Notify::elapsed()
9297
*/
@@ -103,6 +108,8 @@ public function estimated() {
103108
/**
104109
* Forces the current tick count to the total ticks given at instantiation
105110
* time before passing on to `cli\Notify::finish()`.
111+
*
112+
* @return void
106113
*/
107114
public function finish() {
108115
$this->_current = $this->_total;
@@ -114,6 +121,7 @@ public function finish() {
114121
* the ticker is incremented by 1.
115122
*
116123
* @param int $increment The amount to increment by.
124+
* @return void
117125
*/
118126
public function increment($increment = 1) {
119127
$this->_current = min($this->_total, $this->_current + $increment);

lib/cli/Shell.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static public function columns() {
5050
}
5151
} else {
5252
$size = exec( '/usr/bin/env stty size 2>/dev/null' );
53-
if ( '' !== $size && preg_match( '/[0-9]+ ([0-9]+)/', $size, $matches ) ) {
53+
if ( $size && preg_match( '/[0-9]+ ([0-9]+)/', $size, $matches ) ) {
5454
$columns = (int) $matches[1];
5555
}
5656
if ( ! $columns ) {
@@ -99,7 +99,9 @@ static public function isPiped() {
9999

100100
/**
101101
* Uses `stty` to hide input/output completely.
102+
*
102103
* @param boolean $hidden Will hide/show the next data. Defaults to true.
104+
* @return void
103105
*/
104106
static public function hide($hidden = true) {
105107
system( 'stty ' . ( $hidden? '-echo' : 'echo' ) );

0 commit comments

Comments
 (0)