Skip to content

Commit c0d3801

Browse files
authored
Merge pull request #856 from WordPress/769-exclude-codes
Introduce `--ignore-codes` in CLI
2 parents 77f2b8a + bf37198 commit c0d3801

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

docs/CLI.md

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Runs plugin check.
1616
: Exclude checks provided as an argument in comma-separated values, e.g. i18n_usage, late_escaping.
1717
Applies after evaluating `--checks`.
1818
19+
[--ignore-codes=<codes>]
20+
: Ignore error codes provided as an argument in comma-separated values.
21+
1922
[--format=<format>]
2023
: Format to display the results. Options are table, csv, and json. The default will be a table.
2124
---
@@ -57,6 +60,12 @@ By default, `.git`, `vendor` and `node_modules` directories are excluded.
5760
[--warning-severity=<warning-severity>]
5861
: Warning severity level.
5962
63+
[--include-low-severity-errors]
64+
: Include errors with lower severity than the threshold as other type.
65+
66+
[--include-low-severity-warnings]
67+
: Include warnings with lower severity than the threshold as other type.
68+
6069
[--slug=<slug>]
6170
: Slug to override the default.
6271
```

includes/CLI/Plugin_Check_Command.php

+33
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public function __construct( Plugin_Context $plugin_context ) {
7070
* : Exclude checks provided as an argument in comma-separated values, e.g. i18n_usage, late_escaping.
7171
* Applies after evaluating `--checks`.
7272
*
73+
* [--ignore-codes=<codes>]
74+
* : Ignore error codes provided as an argument in comma-separated values.
75+
*
7376
* [--format=<format>]
7477
* : Format to display the results. Options are table, csv, and json. The default will be a table.
7578
* ---
@@ -155,13 +158,17 @@ public function check( $args, $assoc_args ) {
155158
'include-low-severity-errors' => false,
156159
'include-low-severity-warnings' => false,
157160
'slug' => '',
161+
'ignore-codes' => '',
158162
)
159163
);
160164

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

169+
// Ignore codes.
170+
$ignore_codes = isset( $options['ignore-codes'] ) ? wp_parse_list( $options['ignore-codes'] ) : array();
171+
165172
// Create the categories array from CLI arguments.
166173
$categories = isset( $options['categories'] ) ? wp_parse_list( $options['categories'] ) : array();
167174

@@ -258,6 +265,10 @@ static function ( $dirs ) use ( $excluded_files ) {
258265
}
259266
$file_results = $this->flatten_file_results( $file_errors, $file_warnings );
260267

268+
if ( ! empty( $ignore_codes ) ) {
269+
$file_results = $this->get_filtered_results_by_ignore_codes( $file_results, $ignore_codes );
270+
}
271+
261272
if ( '' !== $error_severity || '' !== $warning_severity ) {
262273
$file_results = $this->get_filtered_results_by_severity( $file_results, intval( $error_severity ), intval( $warning_severity ), $include_low_severity_errors, $include_low_severity_warnings );
263274
}
@@ -271,6 +282,10 @@ static function ( $dirs ) use ( $excluded_files ) {
271282
foreach ( $warnings as $file_name => $file_warnings ) {
272283
$file_results = $this->flatten_file_results( array(), $file_warnings );
273284

285+
if ( ! empty( $ignore_codes ) ) {
286+
$file_results = $this->get_filtered_results_by_ignore_codes( $file_results, $ignore_codes );
287+
}
288+
274289
if ( '' !== $error_severity || '' !== $warning_severity ) {
275290
$file_results = $this->get_filtered_results_by_severity( $file_results, intval( $error_severity ), intval( $warning_severity ), $include_low_severity_errors, $include_low_severity_warnings );
276291
}
@@ -666,4 +681,22 @@ private function get_filtered_results_by_severity( $results, $error_severity, $w
666681

667682
return array_merge( $errors, $warnings );
668683
}
684+
685+
/**
686+
* Returns check results filtered by ignore codes.
687+
*
688+
* @since 1.4.0
689+
*
690+
* @param array $results Check results.
691+
* @param array $ignore_codes Array of error codes to be ignored.
692+
* @return array Filtered results.
693+
*/
694+
private function get_filtered_results_by_ignore_codes( $results, $ignore_codes ) {
695+
return array_filter(
696+
$results,
697+
static function ( $result ) use ( $ignore_codes ) {
698+
return ! in_array( $result['code'], $ignore_codes, true );
699+
}
700+
);
701+
}
669702
}

tests/behat/features/plugin-check.feature

+30
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,36 @@ Feature: Test that the WP-CLI command works.
119119
WordPress.Security.EscapeOutput.OutputNotEscaped
120120
"""
121121

122+
When I run the WP-CLI command `plugin check foo-single.php --ignore-codes=WordPress.WP.AlternativeFunctions.rand_mt_rand`
123+
Then STDOUT should not contain:
124+
"""
125+
WordPress.WP.AlternativeFunctions.rand_mt_rand
126+
"""
127+
And STDOUT should contain:
128+
"""
129+
WordPress.Security.EscapeOutput.OutputNotEscaped
130+
"""
131+
132+
When I run the WP-CLI command `plugin check foo-single.php --ignore-codes=WordPress.Security.EscapeOutput.OutputNotEscaped`
133+
Then STDOUT should not contain:
134+
"""
135+
WordPress.Security.EscapeOutput.OutputNotEscaped
136+
"""
137+
And STDOUT should contain:
138+
"""
139+
WordPress.WP.AlternativeFunctions.rand_mt_rand
140+
"""
141+
142+
When I run the WP-CLI command `plugin check foo-single.php --ignore-codes="WordPress.WP.AlternativeFunctions.rand_mt_rand,WordPress.Security.EscapeOutput.OutputNotEscaped"`
143+
Then STDOUT should not contain:
144+
"""
145+
WordPress.Security.EscapeOutput.OutputNotEscaped
146+
"""
147+
And STDOUT should not contain:
148+
"""
149+
WordPress.WP.AlternativeFunctions.rand_mt_rand
150+
"""
151+
122152
Scenario: Check plugin with special chars in plugin name
123153
Given a WP install with the Plugin Check plugin
124154
And a wp-content/plugins/johns-post-counter/johns-post-counter.php file:

0 commit comments

Comments
 (0)