Skip to content

Commit e8d1513

Browse files
committed
Part way through line length and psalm cleanup
backporting pull request for substr bug
1 parent 3ca1697 commit e8d1513

File tree

3 files changed

+99
-44
lines changed

3 files changed

+99
-44
lines changed

src/CLI.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ abstract class CLI {
1919
/** @var Colors */
2020
public $colors;
2121

22-
/** @var array PSR-3 compatible loglevels and their prefix, color, output channel */
22+
/** @var array PSR-3 compatible loglevels and their prefix, color,
23+
* output channel */
2324
protected $loglevel = [
2425
'debug' => [ '', Colors::C_LIGHTGRAY, STDOUT ],
2526
'info' => [ '', Colors::C_CYAN, STDOUT ],
@@ -246,21 +247,21 @@ public function critical( $message, array $context = [] ) :void {
246247
}
247248

248249
/**
249-
* Runtime errors that do not require immediate action but should typically
250-
* be logged and monitored.
250+
* Runtime errors that do not require immediate action but should
251+
* typically be logged and monitored.
251252
*
252253
* @param string $message
253254
* @param array $context
254255
*/
255-
public function error( string $message, array $context = [] ) :void {
256+
public function error( $message, array $context = [] ) :void {
256257
$this->log( 'error', $message, $context );
257258
}
258259

259260
/**
260261
* Exceptional occurrences that are not errors.
261262
*
262-
* Example: Use of deprecated APIs, poor use of an API, undesirable things
263-
* that are not necessarily wrong.
263+
* Example: Use of deprecated APIs, poor use of an API, undesirable
264+
* things that are not necessarily wrong.
264265
*
265266
* @param string $message
266267
* @param array $context
@@ -344,8 +345,11 @@ function interpolate( string $message, array $context = [] ) :string {
344345
$replace = [];
345346
foreach ( $context as $key => $val ) {
346347
// check that the value can be casted to string
347-
if ( !is_array( $val ) &&
348-
( !is_object( $val ) || method_exists( $val, '__toString' ) )
348+
if (
349+
!is_array( $val ) && (
350+
!is_object( $val ) ||
351+
method_exists( $val, '__toString' )
352+
)
349353
) {
350354
$replace['{' . $key . '}'] = $val;
351355
}

src/Options.php

Lines changed: 78 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
/**
66
* Class Options
77
*
8-
* Parses command line options passed to the CLI script. Allows CLI scripts to easily register all accepted options and
9-
* commands and even generates a help text from this setup.
8+
* Parses command line options passed to the CLI script. Allows CLI
9+
* scripts to easily register all accepted options and commands and
10+
* even generates a help text from this setup.
1011
*
1112
* @author Andreas Gohr <andi@splitbrain.org>
1213
* @license MIT
@@ -62,7 +63,7 @@ public function __construct( Colors $colors = null ) {
6263
*
6364
* @param string $help
6465
*/
65-
public function setHelp( $help ) {
66+
public function setHelp( $help ) :void {
6667
$this->setup['']['help'] = $help;
6768
}
6869

@@ -71,7 +72,7 @@ public function setHelp( $help ) {
7172
*
7273
* @param string $help
7374
*/
74-
public function setCommandHelp( $help ) {
75+
public function setCommandHelp( $help ) :void {
7576
$this->setup['']['commandhelp'] = $help;
7677
}
7778

@@ -80,12 +81,13 @@ public function setCommandHelp( $help ) {
8081
*
8182
* @param bool $compact
8283
*/
83-
public function setCompactHelp( $compact = true ) {
84+
public function setCompactHelp( $compact = true ) :void {
8485
$this->setup['']['compacthelp'] = $compact;
8586
}
8687

8788
/**
88-
* Register the names of arguments for help generation and number checking
89+
* Register the names of arguments for help generation and number
90+
* checking
8991
*
9092
* This has to be called in the order arguments are expected
9193
*
@@ -95,7 +97,12 @@ public function setCompactHelp( $compact = true ) {
9597
* @param string $command if theses apply to a sub command only
9698
* @throws Exception
9799
*/
98-
public function registerArgument( $arg, $help, $required = true, $command = '' ) {
100+
public function registerArgument(
101+
string $arg,
102+
string $help,
103+
bool $required = true,
104+
string $command = ''
105+
) :void {
99106
if ( !isset( $this->setup[$command] ) ) {
100107
throw new Exception( "Command $command not registered" );
101108
}
@@ -110,13 +117,14 @@ public function registerArgument( $arg, $help, $required = true, $command = '' )
110117
/**
111118
* This registers a sub command
112119
*
113-
* Sub commands have their own options and use their own function (not main()).
120+
* Sub commands have their own options and use their own function
121+
* (not main()).
114122
*
115123
* @param string $command
116124
* @param string $help
117125
* @throws Exception
118126
*/
119-
public function registerCommand( $command, $help ) {
127+
public function registerCommand( $command, $help ) :void {
120128
if ( isset( $this->setup[$command] ) ) {
121129
throw new Exception( "Command $command already registered" );
122130
}
@@ -134,11 +142,18 @@ public function registerCommand( $command, $help ) {
134142
* @param string $long multi character option (specified with --)
135143
* @param string $help help text for this option
136144
* @param string|null $short one character option (specified with -)
137-
* @param bool|string $needsarg does this option require an argument? give it a name here
145+
* @param bool|string $needsarg does this option require an argument?
146+
* give it a name here
138147
* @param string $command what command does this option apply to
139148
* @throws Exception
140149
*/
141-
public function registerOption( $long, $help, $short = null, $needsarg = false, $command = '' ) {
150+
public function registerOption(
151+
string $long,
152+
string $help,
153+
?string $short = null,
154+
$needsarg = false,
155+
string $command = ''
156+
) :void {
142157
if ( !isset( $this->setup[$command] ) ) {
143158
throw new Exception( "Command $command not registered" );
144159
}
@@ -163,11 +178,13 @@ public function registerOption( $long, $help, $short = null, $needsarg = false,
163178
*
164179
* Throws an exception if arguments are missing.
165180
*
166-
* This is run from CLI automatically and usually does not need to be called directly
181+
* This is run from CLI automatically and usually does not need to
182+
* be called directly
167183
*
168-
* @throws Exception
184+
* @returns bool true if all is ok
185+
* @throws UsageException
169186
*/
170-
public function checkArguments() {
187+
public function checkArguments() :bool {
171188
$argc = count( $this->args );
172189

173190
$req = 0;
@@ -179,46 +196,60 @@ public function checkArguments() {
179196
}
180197

181198
if ( $req > $argc ) {
182-
throw new Exception( "Not enough arguments", Exception::E_OPT_ARG_REQUIRED );
199+
throw new UsageException(
200+
"Not enough arguments", Exception::E_OPT_ARG_REQUIRED
201+
);
183202
}
203+
204+
return true;
184205
}
185206

186207
/**
187208
* Parses the given arguments for known options and command
188209
*
189-
* The given $args array should NOT contain the executed file as first item anymore! The $args
190-
* array is stripped from any options and possible command. All found otions can be accessed via the
191-
* getOpt() function
210+
* The given $args array should NOT contain the executed file as
211+
* first item anymore! The $args array is stripped from any
212+
* options and possible command. All found otions can be accessed
213+
* via the getOpt() function
192214
*
193-
* Note that command options will overwrite any global options with the same name
215+
* Note that command options will overwrite any global options
216+
* with the same name
194217
*
195-
* This is run from CLI automatically and usually does not need to be called directly
218+
* This is run from CLI automatically and usually does not need to
219+
* be called directly
196220
*
197221
* @throws Exception
198222
*/
199-
public function parseOptions() {
223+
public function parseOptions() :void {
200224
$non_opts = [];
201225

202226
$argc = count( $this->args );
203227
for ( $i = 0; $i < $argc; $i++ ) {
204228
$arg = $this->args[$i];
205229

206-
// The special element '--' means explicit end of options. Treat the rest of the arguments as non-options
230+
// The special element '--' means explicit end of
231+
// options. Treat the rest of the arguments as non-options
207232
// and end the loop.
208233
if ( $arg == '--' ) {
209-
$non_opts = array_merge( $non_opts, array_slice( $this->args, $i + 1 ) );
234+
$non_opts = array_merge(
235+
$non_opts, array_slice( $this->args, $i + 1 )
236+
);
210237
break;
211238
}
212239

213240
// '-' is stdin - a normal argument
214241
if ( $arg == '-' ) {
215-
$non_opts = array_merge( $non_opts, array_slice( $this->args, $i ) );
242+
$non_opts = array_merge(
243+
$non_opts, array_slice( $this->args, $i )
244+
);
216245
break;
217246
}
218247

219248
// first non-option
220249
if ( $arg{0} != '-' ) {
221-
$non_opts = array_merge( $non_opts, array_slice( $this->args, $i ) );
250+
$non_opts = array_merge(
251+
$non_opts, array_slice( $this->args, $i )
252+
);
222253
break;
223254
}
224255

@@ -229,12 +260,17 @@ public function parseOptions() {
229260
$val = array_shift( $arg );
230261

231262
if ( !isset( $this->setup[$this->command]['opts'][$opt] ) ) {
232-
throw new Exception( "No such option '$opt'", Exception::E_UNKNOWN_OPT );
263+
throw new UsageException(
264+
"No such option '$opt'", Exception::E_UNKNOWN_OPT
265+
);
233266
}
234267

235268
// argument required?
236269
if ( $this->setup[$this->command]['opts'][$opt]['needsarg'] ) {
237-
if ( is_null( $val ) && $i + 1 < $argc && !preg_match( '/^--?[\w]/', $this->args[$i + 1] ) ) {
270+
if (
271+
is_null( $val ) && $i + 1 < $argc &&
272+
!preg_match( '/^--?[\w]/', $this->args[$i + 1] )
273+
) {
238274
$val = $this->args[++$i];
239275
}
240276
if ( is_null( $val ) ) {
@@ -252,15 +288,21 @@ public function parseOptions() {
252288
// short option
253289
$opt = substr( $arg, 1 );
254290
if ( !isset( $this->setup[$this->command]['short'][$opt] ) ) {
255-
throw new Exception( "No such option $arg", Exception::E_UNKNOWN_OPT );
291+
throw new UsageException(
292+
"No such option $arg", Exception::E_UNKNOWN_OPT
293+
);
256294
} else {
257-
$opt = $this->setup[$this->command]['short'][$opt]; // store it under long name
295+
// store it under long name
296+
$opt = $this->setup[$this->command]['short'][$opt];
258297
}
259298

260299
// argument required?
261300
if ( $this->setup[$this->command]['opts'][$opt]['needsarg'] ) {
262301
$val = null;
263-
if ( $i + 1 < $argc && !preg_match( '/^--?[\w]/', $this->args[$i + 1] ) ) {
302+
if (
303+
$i + 1 < $argc &&
304+
!preg_match( '/^--?[\w]/', $this->args[$i + 1] )
305+
) {
264306
$val = $this->args[++$i];
265307
}
266308
if ( is_null( $val ) ) {
@@ -276,8 +318,12 @@ public function parseOptions() {
276318
// parsing is now done, update args array
277319
$this->args = $non_opts;
278320

279-
// if not done yet, check if first argument is a command and reexecute argument parsing if it is
280-
if ( !$this->command && $this->args && isset( $this->setup[$this->args[0]] ) ) {
321+
// if not done yet, check if first argument is a command and
322+
// reexecute argument parsing if it is
323+
if (
324+
!$this->command && $this->args &&
325+
isset( $this->setup[$this->args[0]] )
326+
) {
281327
// it is a command!
282328
$this->command = array_shift( $this->args );
283329
$this->parseOptions(); // second pass

src/TableFormatter.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function getBorder() {
5454
*
5555
* @param string $border
5656
*/
57-
public function setBorder( $border ) {
57+
public function setBorder( $border ) :void {
5858
$this->border = $border;
5959
}
6060

@@ -74,7 +74,7 @@ public function getMaxWidth() {
7474
*
7575
* @param int $max
7676
*/
77-
public function setMaxWidth( $max ) {
77+
public function setMaxWidth( $max ) :void {
7878
$this->max = $max;
7979
}
8080

@@ -241,7 +241,7 @@ protected function pad( $string, $len ) {
241241
* @param $string
242242
* @return int
243243
*/
244-
protected function strlen( $string ) {
244+
protected function strlen( $string ) :int {
245245
// don't count color codes
246246
$string = preg_replace( "/\33\\[\\d+(;\\d+)?m/", '', $string );
247247

@@ -262,7 +262,12 @@ protected function substr( $string, $start = 0, $length = null ) {
262262
if ( function_exists( 'mb_substr' ) ) {
263263
return mb_substr( $string, $start, $length );
264264
} else {
265-
return substr( $string, $start, $length );
265+
// substr() treats the third parameter different than mb_substr()
266+
if ( $length ) {
267+
return substr( $string, $start, $length );
268+
} else {
269+
return substr( $string, $start );
270+
}
266271
}
267272
}
268273

0 commit comments

Comments
 (0)