Skip to content

Commit ca8ddaa

Browse files
committed
Level 8
1 parent 2668466 commit ca8ddaa

File tree

6 files changed

+40
-23
lines changed

6 files changed

+40
-23
lines changed

lib/cli/Arguments.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function getArguments() {
7373
if (!isset($this->_parsed)) {
7474
$this->parse();
7575
}
76-
return $this->_parsed;
76+
return $this->_parsed ?? [];
7777
}
7878

7979
/**
@@ -110,7 +110,7 @@ public function offsetExists($offset) {
110110
$offset = $offset->key;
111111
}
112112

113-
return array_key_exists($offset, $this->_parsed);
113+
return array_key_exists($offset, $this->_parsed ?? []);
114114
}
115115

116116
/**
@@ -442,15 +442,20 @@ public function parse() {
442442

443443
$this->_applyDefaults();
444444

445-
foreach ($this->_lexer as $argument) {
446-
if ($this->_parseFlag($argument)) {
447-
continue;
448-
}
449-
if ($this->_parseOption($argument)) {
450-
continue;
451-
}
445+
if ($this->_lexer) {
446+
foreach ($this->_lexer as $argument) {
447+
if (null === $argument) {
448+
continue;
449+
}
450+
if ($this->_parseFlag($argument)) {
451+
continue;
452+
}
453+
if ($this->_parseOption($argument)) {
454+
continue;
455+
}
452456

453-
array_push($this->_invalid, $argument->raw());
457+
array_push($this->_invalid, $argument->raw());
458+
}
454459
}
455460

456461
if ($this->_strict && !empty($this->_invalid)) {
@@ -523,6 +528,8 @@ private function _parseOption($option) {
523528
return false;
524529
}
525530

531+
assert(null !== $this->_lexer);
532+
526533
// Peak ahead to make sure we get a value.
527534
if ($this->_lexer->end() || !$this->_lexer->peek->isValue) {
528535
$optionSettings = $this->getOption($option->key);
@@ -546,6 +553,9 @@ private function _parseOption($option) {
546553
// Loop until we find a flag in peak-ahead
547554
while ( $this->_lexer->valid() ) {
548555
$value = $this->_lexer->current();
556+
if ( null === $value ) {
557+
break;
558+
}
549559
array_push( $values, $value->raw );
550560

551561
if ( ! $this->_lexer->end() && ! $this->_lexer->peek->isValue ) {

lib/cli/Streams.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public static function prompt( $question, $default = false, $marker = ': ', $hid
204204
*
205205
* @param string $question The question to ask the user.
206206
* @param string $choice A string of characters allowed as a response. Case is ignored.
207-
* @param string $default The default choice. NULL if a default is not allowed.
207+
* @param string|null $default The default choice. NULL if a default is not allowed.
208208
* @return string The users choice.
209209
* @see cli\prompt()
210210
*/
@@ -214,12 +214,16 @@ public static function choose( $question, $choice = 'yn', $default = 'n' ) {
214214
}
215215

216216
// Make every choice character lowercase except the default
217-
$choice = str_ireplace( $default, strtoupper( $default ), strtolower( $choice ) );
217+
if ( null !== $default ) {
218+
$choice = str_ireplace( $default, strtoupper( $default ), strtolower( $choice ) );
219+
} else {
220+
$choice = strtolower( $choice );
221+
}
218222
// Separate each choice with a forward-slash
219223
$choices = trim( join( '/', str_split( $choice ) ), '/' );
220224

221225
while( true ) {
222-
$line = self::prompt( sprintf( '%s? [%s]', $question, $choices ), $default, '' );
226+
$line = self::prompt( sprintf( '%s? [%s]', $question, $choices ), $default ?? false, '' );
223227

224228
if( stripos( $choice, $line ) !== false ) {
225229
return strtolower( $line );

lib/cli/arguments/Argument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public function exploded() {
140140
array_push($exploded, $this->_argument[$i - 1]);
141141
}
142142

143-
$this->_argument = array_pop($exploded);
143+
$this->_argument = (string) array_pop($exploded);
144144
$this->_raw = '-' . $this->_argument;
145145
return $exploded;
146146
}

lib/cli/arguments/Lexer.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* @property-read Argument $peek
1919
*
20-
* @implements \Iterator<int, Argument>
20+
* @implements \Iterator<int, Argument|null>
2121
*/
2222
class Lexer extends Memoize implements \Iterator {
2323
/** @var Argument|null */
@@ -42,7 +42,7 @@ public function __construct(array $items) {
4242
/**
4343
* The current token.
4444
*
45-
* @return Argument
45+
* @return Argument|null
4646
*/
4747
#[\ReturnTypeWillChange]
4848
public function current() {
@@ -125,7 +125,8 @@ public function end() {
125125
* @return void
126126
*/
127127
private function _shift() {
128-
$this->_item = new Argument(array_shift($this->_items));
128+
$shifted = array_shift($this->_items);
129+
$this->_item = null !== $shifted ? new Argument($shifted) : null;
129130
$this->_index += 1;
130131
$this->_explode();
131132
$this->_unmemo('peek');
@@ -135,7 +136,7 @@ private function _shift() {
135136
* @return void
136137
*/
137138
private function _explode() {
138-
if (!$this->_item->canExplode) {
139+
if (null === $this->_item || !$this->_item->canExplode) {
139140
return;
140141
}
141142

lib/cli/table/Tabular.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ public function row( array $row ) {
4242

4343
$rows[] = $row;
4444

45-
foreach ( $split_lines as $i => $line ) {
46-
if ( ! isset( $rows[ $i + 1 ] ) ) {
47-
$rows[ $i + 1 ] = array_fill_keys( array_keys( $row ), '' );
45+
if ( null !== $col ) {
46+
foreach ( $split_lines as $i => $line ) {
47+
if ( ! isset( $rows[ $i + 1 ] ) ) {
48+
$rows[ $i + 1 ] = array_fill_keys( array_keys( $row ), '' );
49+
}
50+
$rows[ $i + 1 ][ $col ] = $line;
4851
}
49-
$rows[ $i + 1 ][ $col ] = $line;
5052
}
5153

5254
foreach ( $rows as $r ) {

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 7
2+
level: 8
33
paths:
44
- lib
55
scanDirectories:

0 commit comments

Comments
 (0)