From c19f7b83eb1bae3f3e397128381148ad79790069 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Wed, 15 Jan 2025 15:41:51 +0545 Subject: [PATCH 1/7] Add exclude-codes argument in CLI --- includes/CLI/Plugin_Check_Command.php | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/includes/CLI/Plugin_Check_Command.php b/includes/CLI/Plugin_Check_Command.php index 3510091f8..bd2984958 100644 --- a/includes/CLI/Plugin_Check_Command.php +++ b/includes/CLI/Plugin_Check_Command.php @@ -70,6 +70,9 @@ public function __construct( Plugin_Context $plugin_context ) { * : Exclude checks provided as an argument in comma-separated values, e.g. i18n_usage, late_escaping. * Applies after evaluating `--checks`. * + * [--exclude-codes=] + * : Exclude error codes provided as an argument in comma-separated values. + * * [--format=] * : Format to display the results. Options are table, csv, and json. The default will be a table. * --- @@ -155,6 +158,7 @@ public function check( $args, $assoc_args ) { 'include-low-severity-errors' => false, 'include-low-severity-warnings' => false, 'slug' => '', + 'exclude-codes' => '', ) ); @@ -162,6 +166,9 @@ public function check( $args, $assoc_args ) { $plugin = isset( $args[0] ) ? $args[0] : ''; $checks = wp_parse_list( $options['checks'] ); + // Exclude codes. + $exclude_codes = isset( $options['exclude-codes'] ) ? wp_parse_list( $options['exclude-codes'] ) : array(); + // Create the categories array from CLI arguments. $categories = isset( $options['categories'] ) ? wp_parse_list( $options['categories'] ) : array(); @@ -258,6 +265,10 @@ static function ( $dirs ) use ( $excluded_files ) { } $file_results = $this->flatten_file_results( $file_errors, $file_warnings ); + if ( ! empty( $exclude_codes ) ) { + $file_results = $this->get_filtered_results_by_exclude_codes( $file_results, $exclude_codes ); + } + if ( '' !== $error_severity || '' !== $warning_severity ) { $file_results = $this->get_filtered_results_by_severity( $file_results, intval( $error_severity ), intval( $warning_severity ), $include_low_severity_errors, $include_low_severity_warnings ); } @@ -271,6 +282,10 @@ static function ( $dirs ) use ( $excluded_files ) { foreach ( $warnings as $file_name => $file_warnings ) { $file_results = $this->flatten_file_results( array(), $file_warnings ); + if ( ! empty( $exclude_codes ) ) { + $file_results = $this->get_filtered_results_by_exclude_codes( $file_results, $exclude_codes ); + } + if ( '' !== $error_severity || '' !== $warning_severity ) { $file_results = $this->get_filtered_results_by_severity( $file_results, intval( $error_severity ), intval( $warning_severity ), $include_low_severity_errors, $include_low_severity_warnings ); } @@ -666,4 +681,22 @@ private function get_filtered_results_by_severity( $results, $error_severity, $w return array_merge( $errors, $warnings ); } + + /** + * Returns check results filtered by exclude codes. + * + * @since 1.4.0 + * + * @param array $results Check results. + * @param array $exclude_codes Array of error codes to be excludes. + * @return array Filtered results. + */ + private function get_filtered_results_by_exclude_codes( $results, $exclude_codes ) { + return array_filter( + $results, + static function ( $result ) use ( $exclude_codes ) { + return ! in_array( $result['code'], $exclude_codes, true ); + } + ); + } } From 41bce7d2779937d54dfee62b432d6fbfc3ee6839 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Wed, 15 Jan 2025 16:06:26 +0545 Subject: [PATCH 2/7] Add feature tests for the exclude-codes argument --- tests/behat/features/plugin-check.feature | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/behat/features/plugin-check.feature b/tests/behat/features/plugin-check.feature index 80c951350..8514ef784 100644 --- a/tests/behat/features/plugin-check.feature +++ b/tests/behat/features/plugin-check.feature @@ -119,6 +119,36 @@ Feature: Test that the WP-CLI command works. WordPress.Security.EscapeOutput.OutputNotEscaped """ + When I run the WP-CLI command `plugin check foo-single.php --exclude-codes=WordPress.WP.AlternativeFunctions.rand_mt_rand` + Then STDOUT should not contain: + """ + WordPress.WP.AlternativeFunctions.rand_mt_rand + """ + And STDOUT should contain: + """ + WordPress.Security.EscapeOutput.OutputNotEscaped + """ + + When I run the WP-CLI command `plugin check foo-single.php --exclude-codes=WordPress.Security.EscapeOutput.OutputNotEscaped` + Then STDOUT should not contain: + """ + WordPress.Security.EscapeOutput.OutputNotEscaped + """ + And STDOUT should contain: + """ + WordPress.WP.AlternativeFunctions.rand_mt_rand + """ + + When I run the WP-CLI command `plugin check foo-single.php --exclude-codes="WordPress.WP.AlternativeFunctions.rand_mt_rand,WordPress.Security.EscapeOutput.OutputNotEscaped"` + Then STDOUT should not contain: + """ + WordPress.Security.EscapeOutput.OutputNotEscaped + """ + And STDOUT should not contain: + """ + WordPress.WP.AlternativeFunctions.rand_mt_rand + """ + Scenario: Check plugin with special chars in plugin name Given a WP install with the Plugin Check plugin And a wp-content/plugins/johns-post-counter/johns-post-counter.php file: From 95c1a7f7635131ebf7caa0ba967aad9a522ad9af Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 17 Jan 2025 10:15:50 +0545 Subject: [PATCH 3/7] Rename CLI argument --- includes/CLI/Plugin_Check_Command.php | 4 ++-- tests/behat/features/plugin-check.feature | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/includes/CLI/Plugin_Check_Command.php b/includes/CLI/Plugin_Check_Command.php index bd2984958..71e8c7852 100644 --- a/includes/CLI/Plugin_Check_Command.php +++ b/includes/CLI/Plugin_Check_Command.php @@ -70,8 +70,8 @@ public function __construct( Plugin_Context $plugin_context ) { * : Exclude checks provided as an argument in comma-separated values, e.g. i18n_usage, late_escaping. * Applies after evaluating `--checks`. * - * [--exclude-codes=] - * : Exclude error codes provided as an argument in comma-separated values. + * [--ignore-codes=] + * : Ignore error codes provided as an argument in comma-separated values. * * [--format=] * : Format to display the results. Options are table, csv, and json. The default will be a table. diff --git a/tests/behat/features/plugin-check.feature b/tests/behat/features/plugin-check.feature index 8514ef784..2b2212efd 100644 --- a/tests/behat/features/plugin-check.feature +++ b/tests/behat/features/plugin-check.feature @@ -119,7 +119,7 @@ Feature: Test that the WP-CLI command works. WordPress.Security.EscapeOutput.OutputNotEscaped """ - When I run the WP-CLI command `plugin check foo-single.php --exclude-codes=WordPress.WP.AlternativeFunctions.rand_mt_rand` + When I run the WP-CLI command `plugin check foo-single.php --ignore-codes=WordPress.WP.AlternativeFunctions.rand_mt_rand` Then STDOUT should not contain: """ WordPress.WP.AlternativeFunctions.rand_mt_rand @@ -129,7 +129,7 @@ Feature: Test that the WP-CLI command works. WordPress.Security.EscapeOutput.OutputNotEscaped """ - When I run the WP-CLI command `plugin check foo-single.php --exclude-codes=WordPress.Security.EscapeOutput.OutputNotEscaped` + When I run the WP-CLI command `plugin check foo-single.php --ignore-codes=WordPress.Security.EscapeOutput.OutputNotEscaped` Then STDOUT should not contain: """ WordPress.Security.EscapeOutput.OutputNotEscaped @@ -139,7 +139,7 @@ Feature: Test that the WP-CLI command works. WordPress.WP.AlternativeFunctions.rand_mt_rand """ - When I run the WP-CLI command `plugin check foo-single.php --exclude-codes="WordPress.WP.AlternativeFunctions.rand_mt_rand,WordPress.Security.EscapeOutput.OutputNotEscaped"` + When I run the WP-CLI command `plugin check foo-single.php --ignore-codes="WordPress.WP.AlternativeFunctions.rand_mt_rand,WordPress.Security.EscapeOutput.OutputNotEscaped"` Then STDOUT should not contain: """ WordPress.Security.EscapeOutput.OutputNotEscaped From 5c49c356fea154fe4c8ff9051e435924470b2dde Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 17 Jan 2025 10:22:13 +0545 Subject: [PATCH 4/7] Rename variables and methods --- includes/CLI/Plugin_Check_Command.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/includes/CLI/Plugin_Check_Command.php b/includes/CLI/Plugin_Check_Command.php index 71e8c7852..170b807f4 100644 --- a/includes/CLI/Plugin_Check_Command.php +++ b/includes/CLI/Plugin_Check_Command.php @@ -158,7 +158,7 @@ public function check( $args, $assoc_args ) { 'include-low-severity-errors' => false, 'include-low-severity-warnings' => false, 'slug' => '', - 'exclude-codes' => '', + 'ignore-codes' => '', ) ); @@ -166,8 +166,8 @@ public function check( $args, $assoc_args ) { $plugin = isset( $args[0] ) ? $args[0] : ''; $checks = wp_parse_list( $options['checks'] ); - // Exclude codes. - $exclude_codes = isset( $options['exclude-codes'] ) ? wp_parse_list( $options['exclude-codes'] ) : array(); + // Ignore codes. + $ignore_codes = isset( $options['ignore-codes'] ) ? wp_parse_list( $options['ignore-codes'] ) : array(); // Create the categories array from CLI arguments. $categories = isset( $options['categories'] ) ? wp_parse_list( $options['categories'] ) : array(); @@ -265,8 +265,8 @@ static function ( $dirs ) use ( $excluded_files ) { } $file_results = $this->flatten_file_results( $file_errors, $file_warnings ); - if ( ! empty( $exclude_codes ) ) { - $file_results = $this->get_filtered_results_by_exclude_codes( $file_results, $exclude_codes ); + if ( ! empty( $ignore_codes ) ) { + $file_results = $this->get_filtered_results_by_ignore_codes( $file_results, $ignore_codes ); } if ( '' !== $error_severity || '' !== $warning_severity ) { @@ -282,8 +282,8 @@ static function ( $dirs ) use ( $excluded_files ) { foreach ( $warnings as $file_name => $file_warnings ) { $file_results = $this->flatten_file_results( array(), $file_warnings ); - if ( ! empty( $exclude_codes ) ) { - $file_results = $this->get_filtered_results_by_exclude_codes( $file_results, $exclude_codes ); + if ( ! empty( $ignore_codes ) ) { + $file_results = $this->get_filtered_results_by_ignore_codes( $file_results, $ignore_codes ); } if ( '' !== $error_severity || '' !== $warning_severity ) { @@ -683,19 +683,19 @@ private function get_filtered_results_by_severity( $results, $error_severity, $w } /** - * Returns check results filtered by exclude codes. + * Returns check results filtered by ignore codes. * * @since 1.4.0 * * @param array $results Check results. - * @param array $exclude_codes Array of error codes to be excludes. + * @param array $ignore_codes Array of error codes to be ignored. * @return array Filtered results. */ - private function get_filtered_results_by_exclude_codes( $results, $exclude_codes ) { + private function get_filtered_results_by_ignore_codes( $results, $ignore_codes ) { return array_filter( $results, - static function ( $result ) use ( $exclude_codes ) { - return ! in_array( $result['code'], $exclude_codes, true ); + static function ( $result ) use ( $ignore_codes ) { + return ! in_array( $result['code'], $ignore_codes, true ); } ); } From 0c1affc15433f69a9cffc29480931dae9cc8575e Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 17 Jan 2025 10:24:38 +0545 Subject: [PATCH 5/7] Update CLI docs --- docs/CLI.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/CLI.md b/docs/CLI.md index 7c5926dc5..51bfb0e3f 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -16,6 +16,9 @@ Runs plugin check. : Exclude checks provided as an argument in comma-separated values, e.g. i18n_usage, late_escaping. Applies after evaluating `--checks`. +[--ignore-codes=] +: Ignore error codes provided as an argument in comma-separated values. + [--format=] : Format to display the results. Options are table, csv, and json. The default will be a table. --- @@ -57,6 +60,12 @@ By default, `.git`, `vendor` and `node_modules` directories are excluded. [--warning-severity=] : Warning severity level. +[--include-low-severity-errors] +: Include errors with lower severity than the threshold as other type. + +[--include-low-severity-warnings] +: Include warnings with lower severity than the threshold as other type. + [--slug=] : Slug to override the default. ``` From 39b973ddbeaca2010c5f18b3a3cb9a06cfc8b40b Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 17 Jan 2025 10:29:41 +0545 Subject: [PATCH 6/7] Fix PHPDoc spacing issue --- includes/CLI/Plugin_Check_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/CLI/Plugin_Check_Command.php b/includes/CLI/Plugin_Check_Command.php index 170b807f4..fdd7e1038 100644 --- a/includes/CLI/Plugin_Check_Command.php +++ b/includes/CLI/Plugin_Check_Command.php @@ -687,7 +687,7 @@ private function get_filtered_results_by_severity( $results, $error_severity, $w * * @since 1.4.0 * - * @param array $results Check results. + * @param array $results Check results. * @param array $ignore_codes Array of error codes to be ignored. * @return array Filtered results. */ From bf3719848aa97a0033696ec01588842cc185ed49 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 17 Jan 2025 10:31:19 +0545 Subject: [PATCH 7/7] Correct argument type in CLI --- docs/CLI.md | 2 +- includes/CLI/Plugin_Check_Command.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/CLI.md b/docs/CLI.md index 51bfb0e3f..cf87c2971 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -16,7 +16,7 @@ Runs plugin check. : Exclude checks provided as an argument in comma-separated values, e.g. i18n_usage, late_escaping. Applies after evaluating `--checks`. -[--ignore-codes=] +[--ignore-codes=] : Ignore error codes provided as an argument in comma-separated values. [--format=] diff --git a/includes/CLI/Plugin_Check_Command.php b/includes/CLI/Plugin_Check_Command.php index fdd7e1038..4c321ad32 100644 --- a/includes/CLI/Plugin_Check_Command.php +++ b/includes/CLI/Plugin_Check_Command.php @@ -70,7 +70,7 @@ public function __construct( Plugin_Context $plugin_context ) { * : Exclude checks provided as an argument in comma-separated values, e.g. i18n_usage, late_escaping. * Applies after evaluating `--checks`. * - * [--ignore-codes=] + * [--ignore-codes=] * : Ignore error codes provided as an argument in comma-separated values. * * [--format=]