11<?php
22
3- namespace WP_CLI ;
3+ namespace WP_CLI \ Shell ;
44
55use WP_CLI ;
66
@@ -20,82 +20,100 @@ public function start() {
2020 while ( true ) {
2121 $ line = $ this ->prompt ();
2222
23- if ( '' === $ line ) continue ;
23+ if ( '' === $ line ) {
24+ continue ;
25+ }
2426
2527 $ line = rtrim ( $ line , '; ' ) . '; ' ;
2628
2729 if ( self ::starts_with ( self ::non_expressions (), $ line ) ) {
2830 ob_start ();
31+ // phpcs:ignore Squiz.PHP.Eval.Discouraged -- This is meant to be a REPL, no way to avoid eval.
2932 eval ( $ line );
3033 $ out = ob_get_clean ();
31- if ( 0 < strlen ( $ out ) ) {
34+ if ( 0 < strlen ( $ out ) ) {
3235 $ out = rtrim ( $ out , "\n" ) . "\n" ;
3336 }
3437 fwrite ( STDOUT , $ out );
3538 } else {
36- if ( !self ::starts_with ( 'return ' , $ line ) )
39+ if ( ! self ::starts_with ( 'return ' , $ line ) ) {
3740 $ line = 'return ' . $ line ;
41+ }
3842
3943 // Write directly to STDOUT, to sidestep any output buffers created by plugins
4044 ob_start ();
41- $ evl = eval ( $ line );
45+ // phpcs:ignore Squiz.PHP.Eval.Discouraged -- This is meant to be a REPL, no way to avoid eval.
46+ $ evl = eval ( $ line );
4247 $ out = ob_get_clean ();
43- if ( 0 < strlen ( $ out ) ) {
48+ if ( 0 < strlen ( $ out ) ) {
4449 echo rtrim ( $ out , "\n" ) . "\n" ;
4550 }
46- echo " => " ;
51+ echo ' => ' ;
4752 var_dump ( $ evl );
4853 fwrite ( STDOUT , ob_get_clean () );
4954 }
5055 }
5156 }
5257
5358 private static function non_expressions () {
54- return implode ( '| ' , array (
55- 'echo ' , 'global ' , 'unset ' , 'function ' ,
56- 'while ' , 'for ' , 'foreach ' , 'if ' , 'switch ' ,
57- 'include ' , 'include\_once ' , 'require ' , 'require\_once '
58- ) );
59+ return implode (
60+ '| ' ,
61+ array (
62+ 'echo ' ,
63+ 'global ' ,
64+ 'unset ' ,
65+ 'function ' ,
66+ 'while ' ,
67+ 'for ' ,
68+ 'foreach ' ,
69+ 'if ' ,
70+ 'switch ' ,
71+ 'include ' ,
72+ 'include\_once ' ,
73+ 'require ' ,
74+ 'require\_once ' ,
75+ )
76+ );
5977 }
6078
6179 private function prompt () {
6280 $ full_line = false ;
6381
6482 $ done = false ;
6583 do {
66- $ prompt = ( !$ done && $ full_line !== false ) ? '--> ' : $ this ->prompt ;
84+ $ prompt = ( ! $ done && false !== $ full_line ) ? '--> ' : $ this ->prompt ;
6785
6886 $ fp = popen ( self ::create_prompt_cmd ( $ prompt , $ this ->history_file ), 'r ' );
6987
7088 $ line = fgets ( $ fp );
7189
7290 pclose ( $ fp );
7391
74- if ( !$ line ) {
92+ if ( ! $ line ) {
7593 break ;
7694 }
7795
7896 $ line = rtrim ( $ line , "\n" );
7997
80- if ( $ line && '\\' == $ line [ strlen ( $ line ) - 1 ] ) {
98+ if ( $ line && '\\' === $ line [ strlen ( $ line ) - 1 ] ) {
8199 $ line = substr ( $ line , 0 , -1 );
82100 } else {
83101 $ done = true ;
84102 }
85103
86104 $ full_line .= $ line ;
87105
88- } while ( !$ done );
106+ } while ( ! $ done );
89107
90- if ( $ full_line === false ) {
108+ if ( false === $ full_line ) {
91109 return 'exit ' ;
92110 }
93111
94112 return $ full_line ;
95113 }
96114
97115 private static function create_prompt_cmd ( $ prompt , $ history_path ) {
98- $ prompt = escapeshellarg ( $ prompt );
116+ $ prompt = escapeshellarg ( $ prompt );
99117 $ history_path = escapeshellarg ( $ history_path );
100118 if ( getenv ( 'WP_CLI_CUSTOM_SHELL ' ) ) {
101119 $ shell_binary = getenv ( 'WP_CLI_CUSTOM_SHELL ' );
@@ -109,14 +127,14 @@ private static function create_prompt_cmd( $prompt, $history_path ) {
109127
110128 $ shell_binary = escapeshellarg ( $ shell_binary );
111129
112- $ cmd = " set -f; "
113- . "history -r $ history_path; "
114- . " LINE= \"\ "; "
115- . "read -re -p $ prompt LINE; "
116- . " [ $? -eq 0 ] || exit; "
117- . " history -s \"\ $LINE \ "; "
118- . "history -w $ history_path; "
119- . " echo \ $LINE; " ;
130+ $ cmd = ' set -f; '
131+ . "history -r { $ history_path} ; "
132+ . ' LINE=" "; '
133+ . "read -re -p { $ prompt} LINE; "
134+ . ' [ $? -eq 0 ] || exit; '
135+ . ' history -s " $LINE"; '
136+ . "history -w { $ history_path} ; "
137+ . ' echo $LINE; ' ;
120138
121139 return "{$ shell_binary } -c " . escapeshellarg ( $ cmd );
122140 }
0 commit comments