Skip to content

Commit

Permalink
Merge pull request #696 from WordPress/691-show-errors-with-severity-…
Browse files Browse the repository at this point in the history
…less-than-a-number-as-a-warning

CLI: Treat errors below severity threshold as warnings
  • Loading branch information
ernilambar authored Nov 12, 2024
2 parents 74abb6a + 6268281 commit fe8ea10
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 28 deletions.
73 changes: 45 additions & 28 deletions includes/CLI/Plugin_Check_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public function __construct( Plugin_Context $plugin_context ) {
* [--warning-severity=<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>]
* : Slug to override the default.
*
Expand Down Expand Up @@ -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' => '',
)
);

Expand Down Expand Up @@ -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.
Expand All @@ -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 ) ) {
Expand All @@ -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 ) ) {
Expand Down Expand Up @@ -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 );
}
Expand Down
28 changes: 28 additions & 0 deletions tests/behat/features/plugin-check-severity.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fe8ea10

Please sign in to comment.