Skip to content

Commit

Permalink
Update to WPCS v3 (#181)
Browse files Browse the repository at this point in the history
* Update `wp-cli/wp-cli-tests`

* Add `phpcbf` composer script

* Auto-fix all newly reported issues

* Rename ignored sniff

* Rename parameter

* Ignore unused parameter
  • Loading branch information
swissspidy authored Aug 31, 2023
1 parent bdef7f2 commit ad8446b
Show file tree
Hide file tree
Showing 21 changed files with 24 additions and 53 deletions.
10 changes: 5 additions & 5 deletions command.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
}

spl_autoload_register(
function( $class ) {
$class = ltrim( $class, '\\' );
if ( 0 !== stripos( $class, 'runcommand\\Doctor\\' ) ) {
function ( $class_name ) {
$class_name = ltrim( $class_name, '\\' );
if ( 0 !== stripos( $class_name, 'runcommand\\Doctor\\' ) ) {
return;
}

$parts = explode( '\\', $class );
$parts = explode( '\\', $class_name );
array_shift( $parts ); // Don't need "runcommand\Doctor"
array_shift( $parts );
$last = array_pop( $parts ); // File should be 'class-[...].php'
$last = 'class-' . $last . '.php';
$parts[] = $last;
$file = dirname( __FILE__ ) . '/inc/' . str_replace( '_', '-', strtolower( implode( '/', $parts ) ) );
$file = __DIR__ . '/inc/' . str_replace( '_', '-', strtolower( implode( '/', $parts ) ) );
if ( file_exists( $file ) ) {
require $file;
}
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"wp-cli/wp-cli": "^2.1"
},
"require-dev": {
"wp-cli/wp-cli-tests": "^3.2"
"wp-cli/wp-cli-tests": "^4"
},
"config": {
"platform": {
Expand Down Expand Up @@ -66,6 +66,7 @@
"behat-rerun": "rerun-behat-tests",
"lint": "run-linter-tests",
"phpcs": "run-phpcs-tests",
"phpcbf": "run-phpcbf-cleanup",
"phpunit": "run-php-unit-tests",
"prepare-tests": "install-package-tests",
"test": [
Expand Down
2 changes: 0 additions & 2 deletions inc/checks/class-autoload-options-size.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ public function run() {
$this->set_status( 'success' );
$this->set_message( "Autoloaded options size ({$human_total}) is less than threshold ({$human_threshold})." );
}

}

private static function format_bytes( $size, $precision = 2 ) {
$base = log( $size, 1024 );
$suffixes = array( '', 'kb', 'mb', 'g', 't' );
return round( pow( 1024, $base - floor( $base ) ), $precision ) . $suffixes[ floor( $base ) ];
}

}
1 change: 0 additions & 1 deletion inc/checks/class-check.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,4 @@ public function get_results() {
'message' => $this->_message,
);
}

}
12 changes: 4 additions & 8 deletions inc/checks/class-constant-definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,12 @@ public function run() {
$this->set_status( 'error' );
$this->set_message( "Constant '{$this->constant}' is defined '{$human_actual}' but expected to not be falsy." );
}
} else {
if ( ! $this->falsy ) {
} elseif ( ! $this->falsy ) {
$this->set_status( 'success' );
$this->set_message( "Constant '{$this->constant}' is not defined falsy." );
} else {
$this->set_status( 'error' );
$this->set_message( "Constant '{$this->constant}' is defined '{$human_actual}' but expected to be falsy." );
}
} else {
$this->set_status( 'error' );
$this->set_message( "Constant '{$this->constant}' is defined '{$human_actual}' but expected to be falsy." );
}
}
return;
Expand Down Expand Up @@ -118,7 +116,6 @@ public function run() {
$human_expected = self::human_value( $this->value );
$this->set_message( "Constant '{$this->constant}' is defined '{$human_actual}' but expected to be '{$human_expected}'." );
}

}

private static function human_value( $value ) {
Expand All @@ -129,5 +126,4 @@ private static function human_value( $value ) {
}
return $value;
}

}
2 changes: 0 additions & 2 deletions inc/checks/class-core-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,5 @@ public function run() {
$this->set_status( 'success' );
$this->set_message( 'WordPress is at the latest version.' );
}

}

}
1 change: 0 additions & 1 deletion inc/checks/class-core-verify-checksums.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ public function run() {
$this->set_message( "WordPress doesn't verify against its checksums." );
}
}

}
1 change: 0 additions & 1 deletion inc/checks/class-cron-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ public function run() {
$this->set_message( 'Total number of cron jobs is within normal operating expectations.' );
}
}

}
3 changes: 1 addition & 2 deletions inc/checks/class-cron-duplicates.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function run() {
if ( ! isset( $job_counts[ $job['hook'] ] ) ) {
$job_counts[ $job['hook'] ] = 0;
}
$job_counts[ $job['hook'] ]++;
++$job_counts[ $job['hook'] ];
if ( $job_counts[ $job['hook'] ] >= $this->threshold_count ) {
$excess_duplicates = true;
}
Expand All @@ -37,5 +37,4 @@ public function run() {
$this->set_message( 'All cron job counts are within normal operating expectations.' );
}
}

}
1 change: 0 additions & 1 deletion inc/checks/class-cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ protected static function get_crons() {
self::$crons = ! empty( $ret ) ? json_decode( $ret, true ) : array();
return self::$crons;
}

}
2 changes: 0 additions & 2 deletions inc/checks/class-file-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function run() {
$this->set_message( "All '{$this->extension}' files passed assertion that symlink is '{$symlink}'." );
}
}

}

public function check_file( SplFileInfo $file ) {
Expand All @@ -42,5 +41,4 @@ public function check_file( SplFileInfo $file ) {
}
}
}

}
1 change: 0 additions & 1 deletion inc/checks/class-file.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,4 @@ public function get_options() {
'path' => $this->path,
);
}

}
6 changes: 2 additions & 4 deletions inc/checks/class-option-value.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public function run() {
$actual_value = get_option( $this->option );

if ( isset( $this->value ) ) {
if ( $actual_value == $this->value ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- Keep existing behavior.
if ( $actual_value == $this->value ) { // phpcs:ignore Universal.Operators.StrictComparisons -- Keep existing behavior.
$status = 'success';
$message = "Option '{$this->option}' is '{$this->value}' as expected.";
} else {
$status = 'error';
$message = "Option '{$this->option}' is '{$actual_value}' but expected to be '{$this->value}'.";
}
} elseif ( isset( $this->value_is_not ) ) {
if ( $actual_value == $this->value_is_not ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- Keep existing behavior.
if ( $actual_value == $this->value_is_not ) { // phpcs:ignore Universal.Operators.StrictComparisons -- Keep existing behavior.
$status = 'error';
$message = "Option '{$this->option}' is '{$actual_value}' and expected not to be.";
} else {
Expand Down Expand Up @@ -77,7 +77,5 @@ public function run() {
# code...
break;
}

}

}
4 changes: 1 addition & 3 deletions inc/checks/class-plugin-active-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function run() {
$active = 0;
foreach ( self::get_plugins() as $plugin ) {
if ( 'active' === $plugin['status'] || 'active-network' === $plugin['status'] ) {
$active++;
++$active;
}
}

Expand All @@ -34,7 +34,5 @@ public function run() {
$this->set_status( 'success' );
$this->set_message( "Number of active plugins ({$active}) is less than threshold ({$threshold})." );
}

}

}
6 changes: 2 additions & 4 deletions inc/checks/class-plugin-deactivated.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public function run() {
$inactive = 0;
foreach ( self::get_plugins() as $plugin ) {
if ( 'active' === $plugin['status'] || 'active-network' === $plugin['status'] ) {
$active++;
++$active;
} elseif ( 'inactive' === $plugin['status'] ) {
$inactive++;
++$inactive;
}
}

Expand All @@ -37,7 +37,5 @@ public function run() {
$this->set_status( 'success' );
$this->set_message( "Less than {$threshold} percent of plugins are deactivated." );
}

}

}
1 change: 0 additions & 1 deletion inc/checks/class-plugin-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,4 @@ public function run() {
$this->set_message( "Plugin '{$this->name}' is '{$current_status}' as expected." );
}
}

}
4 changes: 1 addition & 3 deletions inc/checks/class-plugin-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function run() {
$update_count = 0;
foreach ( $plugins as $plugin ) {
if ( 'available' === $plugin['update'] ) {
$update_count++;
++$update_count;
}
}

Expand All @@ -28,7 +28,5 @@ public function run() {
$this->set_status( 'success' );
$this->set_message( 'Plugins are up to date.' );
}

}

}
1 change: 0 additions & 1 deletion inc/checks/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ protected static function get_plugins() {
self::$plugins = ! empty( $ret ) ? json_decode( $ret, true ) : array();
return self::$plugins;
}

}
4 changes: 1 addition & 3 deletions inc/checks/class-theme-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function run() {
$update_count = 0;
foreach ( $themes as $theme ) {
if ( 'available' === $theme['update'] ) {
$update_count++;
++$update_count;
}
}

Expand All @@ -31,7 +31,5 @@ public function run() {
$this->set_status( 'success' );
$this->set_message( 'Themes are up to date.' );
}

}

}
3 changes: 1 addition & 2 deletions inc/class-checks.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function register_config( $file ) {
if ( ! empty( $check_data['_']['inherit'] ) ) {
$inherited = $check_data['_']['inherit'];
if ( 'default' === $inherited ) {
$inherited = dirname( dirname( __FILE__ ) ) . '/doctor.yml';
$inherited = dirname( __DIR__ ) . '/doctor.yml';
}
$inherited = self::absolutize( $inherited, dirname( $file ) );
if ( isset( $check_data['_']['skipped_checks'] ) ) {
Expand Down Expand Up @@ -129,5 +129,4 @@ private static function absolutize( $path, $base ) {
}
return $path;
}

}
9 changes: 4 additions & 5 deletions inc/class-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ static function () {
$check_count = count( $results );
$results = array_filter(
$results,
function( $check ) {
function ( $check ) {
return in_array( $check['status'], array( 'warning', 'error' ), true );
}
);
Expand All @@ -202,7 +202,7 @@ function( $check ) {

$results_with_error = array_filter(
$results,
function( $check ) {
function ( $check ) {
return 'error' === $check['status'];
}
);
Expand Down Expand Up @@ -309,7 +309,7 @@ private function load_wordpress_with_template() {

WP_CLI::add_wp_hook(
'wp_redirect',
function( $to ) {
function ( $to ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
ob_start();
debug_print_backtrace();
$message = ob_get_clean();
Expand Down Expand Up @@ -372,7 +372,6 @@ private static function remove_decorations( $comment ) {
* Get the path to the default config file
*/
private static function get_default_config() {
return dirname( dirname( __FILE__ ) ) . '/doctor.yml';
return dirname( __DIR__ ) . '/doctor.yml';
}

}

0 comments on commit ad8446b

Please sign in to comment.