Skip to content

Commit 779bdd1

Browse files
authored
Merge pull request #5198 from jrfnl/feature/update-phpcs
Implement CS checking based on the `WP_CLI_CS` ruleset
2 parents 589b918 + 7e12cae commit 779bdd1

19 files changed

Lines changed: 228 additions & 173 deletions

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ PHAR_BUILD_VERSION
55
/packages
66
/vendor
77
/*.phar
8-
/phpunit.xml.dist
9-
/codesniffer
10-
/PHP_Codesniffer-VariableAnalysis
118
.*.swp
129
*.log
10+
phpunit.xml
11+
phpcs.xml
12+
.phpcs.xml

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"wp-cli/entity-command": "^1.2 || ^2",
2323
"wp-cli/extension-command": "^1.1 || ^2",
2424
"wp-cli/package-command": "^1 || ^2",
25-
"wp-cli/wp-cli-tests": "^2.0.8"
25+
"wp-cli/wp-cli-tests": "^2.1"
2626
},
2727
"suggest": {
2828
"ext-readline": "Include for a better --prompt implementation",

composer.lock

Lines changed: 62 additions & 98 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

php/WP_CLI/Bootstrap/BootstrapState.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
* Represents the state that is passed from one bootstrap step to the next.
99
*
1010
* @package WP_CLI\Bootstrap
11+
*
12+
* Maintain BC: Changing the method names in this class breaks autoload interactions between Phar
13+
* & framework/commands you use outside of Phar (like when running the Phar WP inside of a command folder).
14+
* @phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
1115
*/
1216
class BootstrapState {
1317

@@ -34,7 +38,6 @@ class BootstrapState {
3438
*
3539
* @return mixed
3640
*/
37-
// @codingStandardsIgnoreLine
3841
public function getValue( $key, $fallback = null ) {
3942
return array_key_exists( $key, $this->state )
4043
? $this->state[ $key ]
@@ -49,7 +52,6 @@ public function getValue( $key, $fallback = null ) {
4952
*
5053
* @return void
5154
*/
52-
// @codingStandardsIgnoreLine
5355
public function setValue( $key, $value ) {
5456
$this->state[ $key ] = $value;
5557
}

php/WP_CLI/Dispatcher/Subcommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,14 @@ private function validate_args( $args, $assoc_args, $extra_args ) {
331331
if ( isset( $spec_args['options'] ) ) {
332332
if ( ! empty( $spec['repeating'] ) ) {
333333
do {
334+
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict -- This is a loose comparison by design.
334335
if ( isset( $args[ $i ] ) && ! in_array( $args[ $i ], $spec_args['options'] ) ) {
335336
\WP_CLI::error( 'Invalid value specified for positional arg.' );
336337
}
337338
$i++;
338339
} while ( isset( $args[ $i ] ) );
339340
} else {
341+
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict -- This is a loose comparison by design.
340342
if ( isset( $args[ $i ] ) && ! in_array( $args[ $i ], $spec_args['options'] ) ) {
341343
\WP_CLI::error( 'Invalid value specified for positional arg.' );
342344
}
@@ -351,6 +353,7 @@ private function validate_args( $args, $assoc_args, $extra_args ) {
351353
}
352354
}
353355
if ( isset( $assoc_args[ $spec['name'] ] ) && isset( $spec_args['options'] ) ) {
356+
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict -- This is a loose comparison by design.
354357
if ( ! in_array( $assoc_args[ $spec['name'] ], $spec_args['options'] ) ) {
355358
$errors['fatal'][ $spec['name'] ] = "Invalid value specified for '{$spec['name']}'";
356359
}

php/WP_CLI/FileCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ protected function ensure_dir_exists( $dir ) {
320320
return false;
321321
}
322322

323-
if ( ! @mkdir( $dir, 0777, true ) ) { // @codingStandardsIgnoreLine
323+
if ( ! @mkdir( $dir, 0777, true ) ) {
324324
$error = error_get_last();
325325
\WP_CLI::warning( sprintf( "Failed to create directory '%s': %s.", $dir, $error['message'] ) );
326326
return false;

php/WP_CLI/Formatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private function format( $items, $ascii_pre_colorized = false ) {
157157

158158
if ( 'json' === $this->args['format'] ) {
159159
if ( defined( 'JSON_PARTIAL_OUTPUT_ON_ERROR' ) ) {
160-
// @codingStandardsIgnoreLine
160+
// phpcs:ignore PHPCompatibility.Constants.NewConstants.json_partial_output_on_errorFound
161161
echo json_encode( $out, JSON_PARTIAL_OUTPUT_ON_ERROR );
162162
} else {
163163
echo json_encode( $out );

php/WP_CLI/Runner.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private static function extract_subdir_path( $index_path ) {
181181
$wp_path_src = $matches[1] . $matches[2];
182182
$wp_path_src = Utils\replace_path_consts( $wp_path_src, $index_path );
183183

184-
$wp_path = eval( "return $wp_path_src;" ); // phpcs:ignore Squiz.PHP.Eval.Discouraged -- @codingStandardsIgnoreLine
184+
$wp_path = eval( "return $wp_path_src;" ); // phpcs:ignore Squiz.PHP.Eval.Discouraged
185185

186186
if ( ! Utils\is_path_absolute( $wp_path ) ) {
187187
$wp_path = dirname( $index_path ) . "/$wp_path";
@@ -239,6 +239,7 @@ private function find_wp_root() {
239239
*/
240240
private static function set_wp_root( $path ) {
241241
if ( ! defined( 'ABSPATH' ) ) {
242+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- Declaring a WP native constant.
242243
define( 'ABSPATH', Utils\normalize_path( Utils\trailingslashit( $path ) ) );
243244
} elseif ( ! is_null( $path ) ) {
244245
WP_CLI::error_multi_line(
@@ -856,15 +857,13 @@ private function check_wp_version() {
856857

857858
$minimum_version = '3.7';
858859

859-
// @codingStandardsIgnoreStart
860860
if ( version_compare( $wp_version, $minimum_version, '<' ) ) {
861861
WP_CLI::error(
862862
"WP-CLI needs WordPress $minimum_version or later to work properly. " .
863863
"The version currently installed is $wp_version.\n" .
864864
'Try running `wp core download --force`.'
865865
);
866866
}
867-
// @codingStandardsIgnoreEnd
868867
}
869868

870869
public function init_config() {
@@ -1082,6 +1081,8 @@ public function start() {
10821081
);
10831082
}
10841083

1084+
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- Declaring WP native constants.
1085+
10851086
if ( $this->cmd_starts_with( array( 'core', 'is-installed' ) )
10861087
|| $this->cmd_starts_with( array( 'core', 'update-db' ) ) ) {
10871088
define( 'WP_INSTALLING', true );
@@ -1119,6 +1120,7 @@ public function start() {
11191120
if ( $this->cmd_starts_with( array( 'cron', 'event', 'run' ) ) ) {
11201121
define( 'DOING_CRON', true );
11211122
}
1123+
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound
11221124

11231125
$this->load_wordpress();
11241126

@@ -1159,14 +1161,16 @@ public function load_wordpress() {
11591161
// Load wp-config.php code, in the global scope
11601162
$wp_cli_original_defined_vars = get_defined_vars();
11611163

1162-
eval( $this->get_wp_config_code() ); // phpcs:ignore Squiz.PHP.Eval.Discouraged -- @codingStandardsIgnoreLine
1164+
eval( $this->get_wp_config_code() ); // phpcs:ignore Squiz.PHP.Eval.Discouraged
11631165

11641166
foreach ( get_defined_vars() as $key => $var ) {
11651167
if ( array_key_exists( $key, $wp_cli_original_defined_vars ) || 'wp_cli_original_defined_vars' === $key ) {
11661168
continue;
11671169
}
1168-
// @codingStandardsIgnoreLine
1170+
1171+
// phpcs:ignore PHPCompatibility.Variables.ForbiddenGlobalVariableVariable.NonBareVariableFound
11691172
global ${$key};
1173+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
11701174
${$key} = $var;
11711175
}
11721176

@@ -1203,6 +1207,7 @@ public function load_wordpress() {
12031207
}
12041208

12051209
// Fix memory limit. See http://core.trac.wordpress.org/ticket/14889
1210+
// phpcs:ignore WordPress.PHP.IniSet.memory_limit_Blacklisted -- This is perfectly fine for CLI usage.
12061211
ini_set( 'memory_limit', -1 );
12071212

12081213
// Load all the admin APIs, for convenience
@@ -1233,6 +1238,7 @@ private static function fake_current_site_blog( $url_parts ) {
12331238
$url_parts['path'] = '/';
12341239
}
12351240

1241+
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Intentional override.
12361242
$current_site = (object) array(
12371243
'id' => 1,
12381244
'blog_id' => 1,
@@ -1242,6 +1248,10 @@ private static function fake_current_site_blog( $url_parts ) {
12421248
'site_name' => 'Fake Site',
12431249
);
12441250

1251+
// Bug in WPCS {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/pull/1693}
1252+
// which will be fixed in WPCS 2.1.1/2.2.0. Once the bug fix is in, this comment can be removed.
1253+
// However, it then need to be replaced with the GlobalVariableOverride ignore as used above.
1254+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
12451255
$current_blog = (object) array(
12461256
'blog_id' => 1,
12471257
'site_id' => 1,
@@ -1753,6 +1763,6 @@ private function enable_error_reporting() {
17531763
// Don't enable E_DEPRECATED as old versions of WP use PHP 4 style constructors and the mysql extension.
17541764
error_reporting( E_ALL & ~E_DEPRECATED );
17551765
}
1756-
ini_set( 'display_errors', 'stderr' );
1766+
ini_set( 'display_errors', 'stderr' ); // phpcs:ignore WordPress.PHP.IniSet.display_errors_Blacklisted
17571767
}
17581768
}

php/WP_CLI/UpgraderSkin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function feedback( $string ) {
4444
}
4545

4646
if ( strpos( $string, '%' ) !== false ) {
47+
// Only looking at the arguments from the second one onwards, so this is "safe".
48+
// phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.Changed
4749
$args = func_get_args();
4850
$args = array_splice( $args, 1 );
4951
if ( ! empty( $args ) ) {

php/class-wp-cli.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,9 @@ public static function add_wp_hook( $tag, $function_to_add, $priority = 10, $acc
327327
if ( function_exists( 'add_filter' ) ) {
328328
add_filter( $tag, $function_to_add, $priority, $accepted_args );
329329
} else {
330-
$idx = self::wp_hook_build_unique_id( $tag, $function_to_add, $priority );
330+
$idx = self::wp_hook_build_unique_id( $tag, $function_to_add, $priority );
331+
332+
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- This is intentional & the purpose of this function.
331333
$wp_filter[ $tag ][ $priority ][ $idx ] = array(
332334
'function' => $function_to_add,
333335
'accepted_args' => $accepted_args,
@@ -1358,7 +1360,7 @@ public static function out( $str ) {
13581360
}
13591361

13601362
// back-compat
1361-
// @codingStandardsIgnoreLine
1363+
// phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid -- Deprecated method.
13621364
public static function addCommand( $name, $class ) {
13631365
trigger_error(
13641366
sprintf(

0 commit comments

Comments
 (0)