Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Low severity threshold #725

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 50 additions & 9 deletions includes/CLI/Plugin_Check_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@
* [--warning-severity=<warning-severity>]
* : Warning severity level.
*
* [--include-low-severity-errors]
* : Include errors with lower severity than the threshold.
*
* [--slug=<slug>]
* : Slug to override the default.
*
Expand Down Expand Up @@ -140,15 +143,16 @@
$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' => '',
)
);

Expand Down Expand Up @@ -259,6 +263,9 @@
$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 ) {
Expand All @@ -270,7 +277,15 @@
$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 ) );

Check warning on line 285 in includes/CLI/Plugin_Check_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L285

Added line #L285 was not covered by tests

$file_results = array_merge( $file_results, $low_severity_errors );

Check warning on line 287 in includes/CLI/Plugin_Check_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L287

Added line #L287 was not covered by tests
}
}

if ( ! empty( $file_results ) ) {
Expand Down Expand Up @@ -687,4 +702,30 @@

return array_merge( $errors, $warnings );
}

/**
* 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(
$results,
function ( $item ) use ( $error_severity ) {
return ( 'ERROR' === $item['type'] && $item['severity'] < $error_severity );
}
);

Check warning on line 721 in includes/CLI/Plugin_Check_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L715-L721

Added lines #L715 - L721 were not covered by tests

return array_map(
function ( $item ) {
$item['type'] = 'ERROR_EXTRA';
return $item;
},
$low_severity_errors
);

Check warning on line 729 in includes/CLI/Plugin_Check_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L723-L729

Added lines #L723 - L729 were not covered by tests
}
}
18 changes: 18 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,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