diff --git a/includes/CLI/Plugin_Check_Command.php b/includes/CLI/Plugin_Check_Command.php index 255af27c3..0529611be 100644 --- a/includes/CLI/Plugin_Check_Command.php +++ b/includes/CLI/Plugin_Check_Command.php @@ -111,6 +111,12 @@ public function __construct( Plugin_Context $plugin_context ) { * [--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. * @@ -138,15 +144,17 @@ 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, + 'include-low-severity-warnings' => false, + 'slug' => '', ) ); @@ -235,8 +243,10 @@ static function ( $dirs ) use ( $excluded_files ) { $formatter = $this->get_formatter( $assoc_args, $default_fields ); // Severity. - $error_severity = ! empty( $options['error-severity'] ) ? $options['error-severity'] : $options['severity']; - $warning_severity = ! empty( $options['warning-severity'] ) ? $options['warning-severity'] : $options['severity']; + $error_severity = ! empty( $options['error-severity'] ) ? $options['error-severity'] : $options['severity']; + $warning_severity = ! empty( $options['warning-severity'] ) ? $options['warning-severity'] : $options['severity']; + $include_low_severity_errors = ! empty( $options['include-low-severity-errors'] ) ? true : false; + $include_low_severity_warnings = ! empty( $options['include-low-severity-warnings'] ) ? true : false; // Print the formatted results. // Go over all files with errors first and print them, combined with any warnings in the same file. @@ -249,7 +259,7 @@ static function ( $dirs ) use ( $excluded_files ) { $file_results = $this->flatten_file_results( $file_errors, $file_warnings ); if ( '' !== $error_severity || '' !== $warning_severity ) { - $file_results = $this->get_filtered_results_by_severity( $file_results, intval( $error_severity ), intval( $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 ); } if ( ! empty( $file_results ) ) { @@ -262,7 +272,7 @@ static function ( $dirs ) use ( $excluded_files ) { $file_results = $this->flatten_file_results( array(), $file_warnings ); if ( '' !== $error_severity || '' !== $warning_severity ) { - $file_results = $this->get_filtered_results_by_severity( $file_results, intval( $error_severity ), intval( $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 ); } if ( ! empty( $file_results ) ) { @@ -626,25 +636,32 @@ private function display_results( $formatter, $file_name, $file_results ) { * * @since 1.1.0 * - * @param array $results Check results. - * @param int $error_severity Error severity level. - * @param int $warning_severity Warning severity level. + * @param array $results Check results. + * @param int $error_severity Error severity level. + * @param int $warning_severity Warning severity level. + * @param bool $include_low_severity_errors Include less level of severity issues as warning. + * @param bool $include_low_severity_warnings Include less level of severity issues as warning. + * + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) * @return array Filtered results. */ - private function get_filtered_results_by_severity( $results, $error_severity, $warning_severity ) { - $errors = array_filter( - $results, - function ( $item ) use ( $error_severity ) { - return ( 'ERROR' === $item['type'] && $item['severity'] >= $error_severity ); - } - ); + private function get_filtered_results_by_severity( $results, $error_severity, $warning_severity, $include_low_severity_errors = false, $include_low_severity_warnings = false ) { + $errors = array(); + $warnings = array(); - $warnings = array_filter( - $results, - function ( $item ) use ( $warning_severity ) { - return ( 'WARNING' === $item['type'] && $item['severity'] >= $warning_severity ); + foreach ( $results as $item ) { + if ( 'ERROR' === $item['type'] && $item['severity'] >= $error_severity ) { + $errors[] = $item; + } elseif ( $include_low_severity_errors && 'ERROR' === $item['type'] && $item['severity'] < $error_severity ) { + $item['type'] = 'ERRORS_LOW_SEVERITY'; + $errors[] = $item; + } elseif ( $include_low_severity_warnings && 'WARNING' === $item['type'] && $item['severity'] < $warning_severity ) { + $item['type'] = 'WARNINGS_LOW_SEVERITY'; + $warnings[] = $item; + } elseif ( 'WARNING' === $item['type'] && $item['severity'] >= $warning_severity ) { + $warnings[] = $item; } - ); + } return array_merge( $errors, $warnings ); } diff --git a/tests/behat/features/plugin-check-severity.feature b/tests/behat/features/plugin-check-severity.feature index 824e10515..3e82d13f9 100644 --- a/tests/behat/features/plugin-check-severity.feature +++ b/tests/behat/features/plugin-check-severity.feature @@ -192,5 +192,33 @@ 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: + """ + WordPress.WP.AlternativeFunctions.rand_mt_rand,ERRORS_LOW_SEVERITY,5 + """ + And STDOUT should contain: + """ + WordPress.Security.EscapeOutput.OutputNotEscaped,ERRORS_LOW_SEVERITY,5 + """ + + When I run the WP-CLI command `plugin check foo-bar-wp --format=csv --fields=code,type,severity --warning-severity=7 --include-low-severity-warnings` + Then STDOUT should contain: + """ + allow_unfiltered_uploads_detected,ERROR,7 + """ + And STDOUT should contain: + """ + upgrade_notice_limit,WARNINGS_LOW_SEVERITY,5 + """ + And STDOUT should contain: + """ + default_readme_text,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