Skip to content

Commit 56f0ff1

Browse files
authored
Merge pull request #40 from wp-cli/add-wp-cli-cs-checking
Implement CS checking based on the `WP_CLI_CS` ruleset
2 parents ed63925 + 38d04f4 commit 56f0ff1

File tree

7 files changed

+129
-39
lines changed

7 files changed

+129
-39
lines changed

.distignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ features/
1111
utils/
1212
*.zip
1313
*.tar.gz
14+
phpcs.xml.dist
15+
phpunit.xml.dist

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ vendor/
66
*.tar.gz
77
composer.lock
88
*.log
9+
phpunit.xml
10+
phpcs.xml
11+
.phpcs.xml

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"wp-cli/wp-cli": "^2"
1616
},
1717
"require-dev": {
18-
"wp-cli/wp-cli-tests": "^2.0.7"
18+
"wp-cli/wp-cli-tests": "^2.1"
1919
},
2020
"config": {
2121
"process-timeout": 7200,

phpcs.xml.dist

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="WP-CLI-shell">
3+
<description>Custom ruleset for WP-CLI-shell-command</description>
4+
5+
<!--
6+
#############################################################################
7+
COMMAND LINE ARGUMENTS
8+
For help understanding this file: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
9+
For help using PHPCS: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage
10+
#############################################################################
11+
-->
12+
13+
<!-- What to scan. -->
14+
<file>.</file>
15+
16+
<!-- Show progress. -->
17+
<arg value="p"/>
18+
19+
<!-- Strip the filepaths down to the relevant bit. -->
20+
<arg name="basepath" value="./"/>
21+
22+
<!-- Check up to 8 files simultanously. -->
23+
<arg name="parallel" value="8"/>
24+
25+
<!--
26+
#############################################################################
27+
USE THE WP_CLI_CS RULESET
28+
#############################################################################
29+
-->
30+
31+
<rule ref="WP_CLI_CS"/>
32+
33+
<!--
34+
#############################################################################
35+
PROJECT SPECIFIC CONFIGURATION FOR SNIFFS
36+
#############################################################################
37+
-->
38+
39+
<!-- For help understanding the `testVersion` configuration setting:
40+
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
41+
<config name="testVersion" value="5.4-"/>
42+
43+
<!-- Verify that everything in the global namespace is either namespaced or prefixed.
44+
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
45+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
46+
<properties>
47+
<property name="prefixes" type="array" extend="true">
48+
<element value="WP_CLI\Shell"/><!-- Namespaces. -->
49+
<element value="wpcli_shell"/><!-- Global variables and such. -->
50+
</property>
51+
</properties>
52+
</rule>
53+
54+
<!--
55+
#############################################################################
56+
SELECTIVE EXCLUSIONS
57+
#############################################################################
58+
-->
59+
60+
<!-- Exclude existing classes from the prefix rule as it would break BC to prefix them now. -->
61+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
62+
<exclude-pattern>*/src/Shell_Command\.php$</exclude-pattern>
63+
</rule>
64+
65+
</ruleset>

shell-command.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
return;
55
}
66

7-
$autoload = dirname( __FILE__ ) . '/vendor/autoload.php';
8-
if ( file_exists( $autoload ) ) {
9-
require_once $autoload;
7+
$wpcli_shell_autoloader = dirname( __FILE__ ) . '/vendor/autoload.php';
8+
if ( file_exists( $wpcli_shell_autoloader ) ) {
9+
require_once $wpcli_shell_autoloader;
1010
}
1111

1212
WP_CLI::add_command( 'shell', 'Shell_Command' );

src/Shell_Command.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3-
class Shell_Command extends \WP_CLI_Command {
3+
use WP_CLI\Utils;
4+
5+
class Shell_Command extends WP_CLI_Command {
46

57
/**
68
* Opens an interactive PHP console for running and testing PHP code.
@@ -26,13 +28,13 @@ class Shell_Command extends \WP_CLI_Command {
2628
*/
2729
public function __invoke( $_, $assoc_args ) {
2830
$implementations = array(
29-
'\\Psy\\Shell',
30-
'\\Boris\\Boris',
31-
'\\WP_CLI\\REPL',
31+
'Psy\\Shell',
32+
'Boris\\Boris',
33+
'WP_CLI\\Shell\\REPL',
3234
);
3335

34-
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'basic' ) ) {
35-
$class = '\\WP_CLI\\REPL';
36+
if ( Utils\get_flag_value( $assoc_args, 'basic' ) ) {
37+
$class = 'WP_CLI\\Shell\\REPL';
3638
} else {
3739
foreach ( $implementations as $candidate ) {
3840
if ( class_exists( $candidate ) ) {
@@ -42,10 +44,10 @@ public function __invoke( $_, $assoc_args ) {
4244
}
4345
}
4446

45-
if ( '\\Psy\\Shell' == $class ) {
46-
\Psy\Shell::debug();
47+
if ( 'Psy\\Shell' === $class ) {
48+
Psy\Shell::debug();
4749
} else {
48-
$repl = new $class( "wp> " );
50+
$repl = new $class( 'wp> ' );
4951
$repl->start();
5052
}
5153
}

src/WP_CLI/REPL.php renamed to src/WP_CLI/Shell/REPL.php

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace WP_CLI;
3+
namespace WP_CLI\Shell;
44

55
use 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

Comments
 (0)