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

Introduce --ignore-codes in CLI #856

Merged
merged 7 commits into from
Jan 17, 2025
Merged
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
9 changes: 9 additions & 0 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Runs plugin check.
: Exclude checks provided as an argument in comma-separated values, e.g. i18n_usage, late_escaping.
Applies after evaluating `--checks`.

[--ignore-codes=<codes>]
: Ignore error codes provided as an argument in comma-separated values.

[--format=<format>]
: Format to display the results. Options are table, csv, and json. The default will be a table.
---
Expand Down Expand Up @@ -57,6 +60,12 @@ By default, `.git`, `vendor` and `node_modules` directories are excluded.
[--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
33 changes: 33 additions & 0 deletions includes/CLI/Plugin_Check_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
* : Exclude checks provided as an argument in comma-separated values, e.g. i18n_usage, late_escaping.
* Applies after evaluating `--checks`.
*
* [--ignore-codes=<codes>]
* : Ignore error codes provided as an argument in comma-separated values.
*
* [--format=<format>]
* : Format to display the results. Options are table, csv, and json. The default will be a table.
* ---
Expand Down Expand Up @@ -155,13 +158,17 @@
'include-low-severity-errors' => false,
'include-low-severity-warnings' => false,
'slug' => '',
'ignore-codes' => '',
)
);

// Create the plugin and checks array from CLI arguments.
$plugin = isset( $args[0] ) ? $args[0] : '';
$checks = wp_parse_list( $options['checks'] );

// Ignore codes.
$ignore_codes = isset( $options['ignore-codes'] ) ? wp_parse_list( $options['ignore-codes'] ) : array();

// Create the categories array from CLI arguments.
$categories = isset( $options['categories'] ) ? wp_parse_list( $options['categories'] ) : array();

Expand Down Expand Up @@ -258,6 +265,10 @@
}
$file_results = $this->flatten_file_results( $file_errors, $file_warnings );

if ( ! empty( $ignore_codes ) ) {
$file_results = $this->get_filtered_results_by_ignore_codes( $file_results, $ignore_codes );
}

if ( '' !== $error_severity || '' !== $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 );
}
Expand All @@ -271,6 +282,10 @@
foreach ( $warnings as $file_name => $file_warnings ) {
$file_results = $this->flatten_file_results( array(), $file_warnings );

if ( ! empty( $ignore_codes ) ) {
$file_results = $this->get_filtered_results_by_ignore_codes( $file_results, $ignore_codes );

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

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L286

Added line #L286 was not covered by tests
}

if ( '' !== $error_severity || '' !== $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 );
}
Expand Down Expand Up @@ -666,4 +681,22 @@

return array_merge( $errors, $warnings );
}

/**
* Returns check results filtered by ignore codes.
*
* @since 1.4.0
*
* @param array $results Check results.
* @param array $ignore_codes Array of error codes to be ignored.
* @return array Filtered results.
*/
private function get_filtered_results_by_ignore_codes( $results, $ignore_codes ) {
return array_filter(
$results,
static function ( $result ) use ( $ignore_codes ) {
return ! in_array( $result['code'], $ignore_codes, true );
}
);
}
}
30 changes: 30 additions & 0 deletions tests/behat/features/plugin-check.feature
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,36 @@ Feature: Test that the WP-CLI command works.
WordPress.Security.EscapeOutput.OutputNotEscaped
"""

When I run the WP-CLI command `plugin check foo-single.php --ignore-codes=WordPress.WP.AlternativeFunctions.rand_mt_rand`
Then STDOUT should not contain:
"""
WordPress.WP.AlternativeFunctions.rand_mt_rand
"""
And STDOUT should contain:
"""
WordPress.Security.EscapeOutput.OutputNotEscaped
"""

When I run the WP-CLI command `plugin check foo-single.php --ignore-codes=WordPress.Security.EscapeOutput.OutputNotEscaped`
Then STDOUT should not contain:
"""
WordPress.Security.EscapeOutput.OutputNotEscaped
"""
And STDOUT should contain:
"""
WordPress.WP.AlternativeFunctions.rand_mt_rand
"""

When I run the WP-CLI command `plugin check foo-single.php --ignore-codes="WordPress.WP.AlternativeFunctions.rand_mt_rand,WordPress.Security.EscapeOutput.OutputNotEscaped"`
Then STDOUT should not contain:
"""
WordPress.Security.EscapeOutput.OutputNotEscaped
"""
And STDOUT should not contain:
"""
WordPress.WP.AlternativeFunctions.rand_mt_rand
"""

Scenario: Check plugin with special chars in plugin name
Given a WP install with the Plugin Check plugin
And a wp-content/plugins/johns-post-counter/johns-post-counter.php file:
Expand Down
Loading