Skip to content

Commit 31bf815

Browse files
Copilotswissspidy
andcommitted
Rename internal variables to prevent bleeding into eval context
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 1c5f7d6 commit 31bf815

2 files changed

Lines changed: 42 additions & 24 deletions

File tree

features/shell.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,21 @@ Feature: WordPress REPL
7777
"""
7878
history: -1: invalid option
7979
"""
80+
81+
Scenario: User can define variable named $line
82+
Given a WP install
83+
And a session file:
84+
"""
85+
$line = 'this should work';
86+
$line;
87+
"""
88+
89+
When I run `wp shell --basic < session`
90+
Then STDOUT should contain:
91+
"""
92+
=> string(16) "this should work"
93+
"""
94+
And STDOUT should contain:
95+
"""
96+
=> string(16) "this should work"
97+
"""

src/WP_CLI/Shell/REPL.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,38 @@ public function __construct( $prompt ) {
1919
public function start() {
2020
// @phpstan-ignore while.alwaysTrue
2121
while ( true ) {
22-
$line = $this->prompt();
22+
$__repl_input_line = $this->prompt();
2323

24-
if ( '' === $line ) {
24+
if ( '' === $__repl_input_line ) {
2525
continue;
2626
}
2727

28-
$line = rtrim( $line, ';' ) . ';';
28+
$__repl_input_line = rtrim( $__repl_input_line, ';' ) . ';';
2929

30-
if ( self::starts_with( self::non_expressions(), $line ) ) {
30+
if ( self::starts_with( self::non_expressions(), $__repl_input_line ) ) {
3131
ob_start();
3232
// phpcs:ignore Squiz.PHP.Eval.Discouraged -- This is meant to be a REPL, no way to avoid eval.
33-
eval( $line );
34-
$out = (string) ob_get_clean();
35-
if ( 0 < strlen( $out ) ) {
36-
$out = rtrim( $out, "\n" ) . "\n";
33+
eval( $__repl_input_line );
34+
$__repl_output = (string) ob_get_clean();
35+
if ( 0 < strlen( $__repl_output ) ) {
36+
$__repl_output = rtrim( $__repl_output, "\n" ) . "\n";
3737
}
38-
fwrite( STDOUT, $out );
38+
fwrite( STDOUT, $__repl_output );
3939
} else {
40-
if ( ! self::starts_with( 'return', $line ) ) {
41-
$line = 'return ' . $line;
40+
if ( ! self::starts_with( 'return', $__repl_input_line ) ) {
41+
$__repl_input_line = 'return ' . $__repl_input_line;
4242
}
4343

4444
// Write directly to STDOUT, to sidestep any output buffers created by plugins
4545
ob_start();
4646
// phpcs:ignore Squiz.PHP.Eval.Discouraged -- This is meant to be a REPL, no way to avoid eval.
47-
$evl = eval( $line );
48-
$out = (string) ob_get_clean();
49-
if ( 0 < strlen( $out ) ) {
50-
echo rtrim( $out, "\n" ) . "\n";
47+
$__repl_eval_result = eval( $__repl_input_line );
48+
$__repl_output = (string) ob_get_clean();
49+
if ( 0 < strlen( $__repl_output ) ) {
50+
echo rtrim( $__repl_output, "\n" ) . "\n";
5151
}
5252
echo '=> ';
53-
var_dump( $evl );
53+
var_dump( $__repl_eval_result );
5454
fwrite( STDOUT, (string) ob_get_clean() );
5555
}
5656
}
@@ -88,25 +88,25 @@ private function prompt() {
8888

8989
$fp = popen( self::create_prompt_cmd( $prompt, $this->history_file ), 'r' );
9090

91-
$line = $fp ? fgets( $fp ) : '';
91+
$__repl_input_line = $fp ? fgets( $fp ) : '';
9292

9393
if ( $fp ) {
9494
pclose( $fp );
9595
}
9696

97-
if ( ! $line ) {
97+
if ( ! $__repl_input_line ) {
9898
break;
9999
}
100100

101-
$line = rtrim( $line, "\n" );
101+
$__repl_input_line = rtrim( $__repl_input_line, "\n" );
102102

103-
if ( $line && '\\' === $line[ strlen( $line ) - 1 ] ) {
104-
$line = substr( $line, 0, -1 );
103+
if ( $__repl_input_line && '\\' === $__repl_input_line[ strlen( $__repl_input_line ) - 1 ] ) {
104+
$__repl_input_line = substr( $__repl_input_line, 0, -1 );
105105
} else {
106106
$done = true;
107107
}
108108

109-
$full_line .= $line;
109+
$full_line .= $__repl_input_line;
110110

111111
} while ( ! $done );
112112

@@ -150,7 +150,7 @@ private function set_history_file() {
150150
$this->history_file = \WP_CLI\Utils\get_temp_dir() . 'wp-cli-history-' . md5( $data );
151151
}
152152

153-
private static function starts_with( $tokens, $line ) {
154-
return preg_match( "/^($tokens)[\(\s]+/", $line );
153+
private static function starts_with( $tokens, $__repl_input_line ) {
154+
return preg_match( "/^($tokens)[\(\s]+/", $__repl_input_line );
155155
}
156156
}

0 commit comments

Comments
 (0)