diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 00000000..229d3401 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,15 @@ +parameters: + level: 9 + paths: + - src + - shell-command.php + scanDirectories: + - vendor/wp-cli/wp-cli/php + scanFiles: + - vendor/php-stubs/wordpress-stubs/wordpress-stubs.php + treatPhpDocTypesAsCertain: false + ignoreErrors: + - identifier: class.notFound + - identifier: missingType.property + - identifier: missingType.parameter + - identifier: missingType.return diff --git a/src/Shell_Command.php b/src/Shell_Command.php index 66c03fa4..edd2c611 100644 --- a/src/Shell_Command.php +++ b/src/Shell_Command.php @@ -27,15 +27,15 @@ class Shell_Command extends WP_CLI_Command { * => string(6) "WP-CLI" */ public function __invoke( $_, $assoc_args ) { + $class = WP_CLI\Shell\REPL::class; + $implementations = array( - 'Psy\\Shell', - 'Boris\\Boris', - 'WP_CLI\\Shell\\REPL', + \Psy\Shell::class, + \Boris\Boris::class, + WP_CLI\Shell\REPL::class, ); - if ( Utils\get_flag_value( $assoc_args, 'basic' ) ) { - $class = 'WP_CLI\\Shell\\REPL'; - } else { + if ( ! Utils\get_flag_value( $assoc_args, 'basic' ) ) { foreach ( $implementations as $candidate ) { if ( class_exists( $candidate ) ) { $class = $candidate; @@ -44,10 +44,17 @@ public function __invoke( $_, $assoc_args ) { } } - if ( 'Psy\\Shell' === $class ) { + /** + * @var class-string $class + */ + + if ( \Psy\Shell::class === $class ) { $shell = new Psy\Shell(); $shell->run(); } else { + /** + * @var class-string $class + */ $repl = new $class( 'wp> ' ); $repl->start(); } diff --git a/src/WP_CLI/Shell/REPL.php b/src/WP_CLI/Shell/REPL.php index cc9ee064..15697bf0 100644 --- a/src/WP_CLI/Shell/REPL.php +++ b/src/WP_CLI/Shell/REPL.php @@ -17,6 +17,7 @@ public function __construct( $prompt ) { } public function start() { + // @phpstan-ignore while.alwaysTrue while ( true ) { $line = $this->prompt(); @@ -30,7 +31,7 @@ public function start() { ob_start(); // phpcs:ignore Squiz.PHP.Eval.Discouraged -- This is meant to be a REPL, no way to avoid eval. eval( $line ); - $out = ob_get_clean(); + $out = (string) ob_get_clean(); if ( 0 < strlen( $out ) ) { $out = rtrim( $out, "\n" ) . "\n"; } @@ -44,13 +45,13 @@ public function start() { ob_start(); // phpcs:ignore Squiz.PHP.Eval.Discouraged -- This is meant to be a REPL, no way to avoid eval. $evl = eval( $line ); - $out = ob_get_clean(); + $out = (string) ob_get_clean(); if ( 0 < strlen( $out ) ) { echo rtrim( $out, "\n" ) . "\n"; } echo '=> '; var_dump( $evl ); - fwrite( STDOUT, ob_get_clean() ); + fwrite( STDOUT, (string) ob_get_clean() ); } } } @@ -82,13 +83,16 @@ private function prompt() { $done = false; do { + // @phpstan-ignore booleanNot.alwaysTrue $prompt = ( ! $done && false !== $full_line ) ? '--> ' : $this->prompt; $fp = popen( self::create_prompt_cmd( $prompt, $this->history_file ), 'r' ); - $line = fgets( $fp ); + $line = $fp ? fgets( $fp ) : ''; - pclose( $fp ); + if ( $fp ) { + pclose( $fp ); + } if ( ! $line ) { break;