From 20aa699b24cb07196d45f107a6174a40051fb355 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 18 Oct 2024 16:05:54 +0545 Subject: [PATCH 1/4] Include low severity errors --- includes/CLI/Plugin_Check_Command.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/includes/CLI/Plugin_Check_Command.php b/includes/CLI/Plugin_Check_Command.php index 05f93faad..257457803 100644 --- a/includes/CLI/Plugin_Check_Command.php +++ b/includes/CLI/Plugin_Check_Command.php @@ -113,6 +113,9 @@ public function __construct( Plugin_Context $plugin_context ) { * [--warning-severity=] * : Warning severity level. * + * [--include-low-severity-errors] + * : Include errors with lower severity than the threshold. + * * [--slug=] * : Slug to override the default. * From 57ac528a7216e2a52a689c8b99c6a6d854a0dd5e Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 18 Oct 2024 16:18:59 +0545 Subject: [PATCH 2/4] Add test for lower severity --- .../features/plugin-check-severity.feature | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/behat/features/plugin-check-severity.feature b/tests/behat/features/plugin-check-severity.feature index 38323ae62..e3e55f815 100644 --- a/tests/behat/features/plugin-check-severity.feature +++ b/tests/behat/features/plugin-check-severity.feature @@ -192,5 +192,23 @@ Feature: Test that the severity level in plugin check works. upgrade_notice_limit,WARNING,5 """ + When I run the WP-CLI command `plugin check foo-bar-wp --format=csv --fields=code,type,severity --error-severity=7 --include-low-severity-errors` + Then STDOUT should contain: + """ + allow_unfiltered_uploads_detected,ERROR,7 + """ + And STDOUT should contain: + """ + obfuscated_code_detected,ERROR_EXTRA,6 + """ + And STDOUT should contain: + """ + WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR_EXTRA,5 + """ + And STDOUT should contain: + """ + outdated_tested_upto_header,ERROR,7 + """ + When I run the WP-CLI command `plugin check foo-bar-wp --format=csv --fields=code,type,severity --severity=10` Then STDOUT should be empty From 7a401219fc0194d5f22fe1d95e64f23749ad70d6 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 18 Oct 2024 20:44:27 +0545 Subject: [PATCH 3/4] Add condition for adding low severity errors --- includes/CLI/Plugin_Check_Command.php | 50 ++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/includes/CLI/Plugin_Check_Command.php b/includes/CLI/Plugin_Check_Command.php index 257457803..eabaa8f62 100644 --- a/includes/CLI/Plugin_Check_Command.php +++ b/includes/CLI/Plugin_Check_Command.php @@ -143,15 +143,16 @@ public function check( $args, $assoc_args ) { $options = $this->get_options( $assoc_args, array( - 'checks' => '', - 'format' => 'table', - 'ignore-warnings' => false, - 'ignore-errors' => false, - 'include-experimental' => false, - 'severity' => '', - 'error-severity' => '', - 'warning-severity' => '', - 'slug' => '', + 'checks' => '', + 'format' => 'table', + 'ignore-warnings' => false, + 'ignore-errors' => false, + 'include-experimental' => false, + 'severity' => '', + 'error-severity' => '', + 'warning-severity' => '', + 'include-low-severity-errors' => false, + 'slug' => '', ) ); @@ -262,6 +263,9 @@ static function ( $dirs ) use ( $excluded_files ) { $error_severity = ! empty( $options['error-severity'] ) ? $options['error-severity'] : $options['severity']; $warning_severity = ! empty( $options['warning-severity'] ) ? $options['warning-severity'] : $options['severity']; + // Low severity errors. + $include_low_severity_errors = ! empty( $options['include-low-severity-errors'] ) ? $options['include-low-severity-errors'] : false; + // Print the formatted results. // Go over all files with errors first and print them, combined with any warnings in the same file. foreach ( $errors as $file_name => $file_errors ) { @@ -273,7 +277,15 @@ static function ( $dirs ) use ( $excluded_files ) { $file_results = $this->flatten_file_results( $file_errors, $file_warnings ); if ( '' !== $error_severity || '' !== $warning_severity ) { + $original_results = $file_results; + $file_results = $this->get_filtered_results_by_severity( $file_results, intval( $error_severity ), intval( $warning_severity ) ); + + if ( absint( $error_severity ) > 0 && true === $include_low_severity_errors ) { + $low_severity_errors = $this->get_low_severity_errors( $original_results, absint( $error_severity ) ); + + $file_results = array_merge( $file_results, $low_severity_errors ); + } } if ( ! empty( $file_results ) ) { @@ -690,4 +702,24 @@ function ( $item ) use ( $warning_severity ) { return array_merge( $errors, $warnings ); } + + private function get_low_severity_errors( $file_results, $error_severity ) { + $low_severity_errors = array_filter( + $file_results, + function ( $item ) use ( $error_severity ) { + return ( 'ERROR' === $item['type'] && $item['severity'] < $error_severity ); + } + ); + + $low_severity_errors = array_map( + function ( $item ) { + $item['type'] = 'ERROR_EXTRA'; + + return $item; + }, + $low_severity_errors + ); + + return $low_severity_errors; + } } From 2c178ae2a6d7827591669b00e9fd3e438143d3b6 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 18 Oct 2024 20:50:33 +0545 Subject: [PATCH 4/4] Add missing function doc --- includes/CLI/Plugin_Check_Command.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/includes/CLI/Plugin_Check_Command.php b/includes/CLI/Plugin_Check_Command.php index eabaa8f62..402b32a37 100644 --- a/includes/CLI/Plugin_Check_Command.php +++ b/includes/CLI/Plugin_Check_Command.php @@ -703,23 +703,29 @@ function ( $item ) use ( $warning_severity ) { return array_merge( $errors, $warnings ); } - private function get_low_severity_errors( $file_results, $error_severity ) { + /** + * Returns low severity errors. + * + * @since 1.3.0 + * + * @param array $results Check results. + * @param int $error_severity Error severity level. + * @return array Filtered low severity errors. + */ + private function get_low_severity_errors( $results, $error_severity ) { $low_severity_errors = array_filter( - $file_results, + $results, function ( $item ) use ( $error_severity ) { return ( 'ERROR' === $item['type'] && $item['severity'] < $error_severity ); } ); - $low_severity_errors = array_map( + return array_map( function ( $item ) { $item['type'] = 'ERROR_EXTRA'; - return $item; }, $low_severity_errors ); - - return $low_severity_errors; } }