Skip to content

Commit

Permalink
Add exclude-codes argument in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Jan 15, 2025
1 parent 77f2b8a commit c19f7b8
Showing 1 changed file with 33 additions and 0 deletions.
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 @@ public function __construct( Plugin_Context $plugin_context ) {
* : Exclude checks provided as an argument in comma-separated values, e.g. i18n_usage, late_escaping.
* Applies after evaluating `--checks`.
*
* [--exclude-codes=<checks>]
* : Exclude 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 @@ public function check( $args, $assoc_args ) {
'include-low-severity-errors' => false,
'include-low-severity-warnings' => false,
'slug' => '',
'exclude-codes' => '',
)
);

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

// Exclude codes.
$exclude_codes = isset( $options['exclude-codes'] ) ? wp_parse_list( $options['exclude-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 @@ static function ( $dirs ) use ( $excluded_files ) {
}
$file_results = $this->flatten_file_results( $file_errors, $file_warnings );

if ( ! empty( $exclude_codes ) ) {
$file_results = $this->get_filtered_results_by_exclude_codes( $file_results, $exclude_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 @@ static function ( $dirs ) use ( $excluded_files ) {
foreach ( $warnings as $file_name => $file_warnings ) {
$file_results = $this->flatten_file_results( array(), $file_warnings );

if ( ! empty( $exclude_codes ) ) {
$file_results = $this->get_filtered_results_by_exclude_codes( $file_results, $exclude_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 Down Expand Up @@ -666,4 +681,22 @@ private function get_filtered_results_by_severity( $results, $error_severity, $w

return array_merge( $errors, $warnings );
}

/**
* Returns check results filtered by exclude codes.
*
* @since 1.4.0
*
* @param array $results Check results.
* @param array $exclude_codes Array of error codes to be excludes.
* @return array Filtered results.
*/
private function get_filtered_results_by_exclude_codes( $results, $exclude_codes ) {
return array_filter(
$results,
static function ( $result ) use ( $exclude_codes ) {
return ! in_array( $result['code'], $exclude_codes, true );
}
);
}
}

0 comments on commit c19f7b8

Please sign in to comment.