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

CLI: Treat errors below severity threshold as warnings #696

Merged
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0c49f42
convert errors less severity to warnings
davidperezgar Oct 3, 2024
42ad9f6
updated behat tests
davidperezgar Oct 3, 2024
56c77f7
updated tests
davidperezgar Oct 3, 2024
fb7717e
Merge branch 'trunk' into 691-show-errors-with-severity-less-than-a-n…
davidperezgar Oct 10, 2024
398ab67
changes to show errors less severity
davidperezgar Oct 10, 2024
9fed972
phptests fixes
davidperezgar Oct 10, 2024
04fcb86
Merge branch 'trunk' into 691-show-errors-with-severity-less-than-a-n…
davidperezgar Oct 11, 2024
2526c94
added behat testing
davidperezgar Oct 11, 2024
5437ba4
phpmd test fixed
davidperezgar Oct 11, 2024
a76a7e5
fix phplint
davidperezgar Oct 11, 2024
3984d64
fix phpmd test
davidperezgar Oct 12, 2024
8b72701
Merge branch 'trunk' into 691-show-errors-with-severity-less-than-a-n…
davidperezgar Oct 12, 2024
9342dda
fix phplint
davidperezgar Oct 12, 2024
0b631d9
Merge branch 'trunk' into 691-show-errors-with-severity-less-than-a-n…
swissspidy Oct 15, 2024
9886683
changed as commented
davidperezgar Oct 16, 2024
505f3b6
Merge branch 'trunk' into 691-show-errors-with-severity-less-than-a-n…
davidperezgar Oct 16, 2024
68834e4
new line
davidperezgar Oct 16, 2024
e4275c1
remove flag boolean
davidperezgar Oct 16, 2024
2df3000
Update includes/CLI/Plugin_Check_Command.php
davidperezgar Oct 16, 2024
37fd096
suggested changes
davidperezgar Oct 16, 2024
a62fd0b
Update includes/CLI/Plugin_Check_Command.php
davidperezgar Oct 17, 2024
16a33d1
fix conditional
davidperezgar Oct 17, 2024
57950b1
changed to ERROR_EXTRA as suggested
davidperezgar Oct 18, 2024
10ab255
move to warnings
davidperezgar Oct 18, 2024
8967e53
errors-low-severity and warnings-low-severity
davidperezgar Nov 7, 2024
235c811
Merge branch 'trunk' into 691-show-errors-with-severity-less-than-a-n…
davidperezgar Nov 7, 2024
ed6d75f
fix error and severity in warnings
davidperezgar Nov 8, 2024
c4541cc
fix comment
davidperezgar Nov 9, 2024
6268281
Fix spacing issues in function docs
ernilambar Nov 12, 2024
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
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 @@
* [--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 @@
$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 @@
$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 @@
$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 );
davidperezgar marked this conversation as resolved.
Show resolved Hide resolved
}

if ( ! empty( $file_results ) ) {
Expand All @@ -262,7 +272,7 @@
$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 );

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

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L275

Added line #L275 was not covered by tests
}

if ( ! empty( $file_results ) ) {
Expand Down Expand Up @@ -626,25 +636,32 @@
*
* @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.
ernilambar marked this conversation as resolved.
Show resolved Hide resolved
*
* @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;

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

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L654

Added line #L654 was not covered by tests
} elseif ( $include_low_severity_errors && 'ERROR' === $item['type'] && $item['severity'] < $error_severity ) {
$item['type'] = 'ERRORS_LOW_SEVERITY';
$errors[] = $item;

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

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L656-L657

Added lines #L656 - L657 were not covered by tests
} elseif ( $include_low_severity_warnings && 'WARNING' === $item['type'] && $item['severity'] < $warning_severity ) {
$item['type'] = 'WARNINGS_LOW_SEVERITY';
$warnings[] = $item;

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

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L659-L660

Added lines #L659 - L660 were not covered by tests
} elseif ( 'WARNING' === $item['type'] && $item['severity'] >= $warning_severity ) {
$warnings[] = $item;

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

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L662

Added line #L662 was not covered by tests
}
);
}

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