Skip to content

Commit

Permalink
phpmd test fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
davidperezgar committed Oct 11, 2024
1 parent 2526c94 commit 5437ba4
Showing 1 changed file with 52 additions and 29 deletions.
81 changes: 52 additions & 29 deletions includes/CLI/Plugin_Check_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ static function ( $dirs ) use ( $excluded_files ) {
$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 ), $show_error_severity );
$file_results = $this->get_filtered_results( $file_results, intval( $error_severity ), intval( $warning_severity ), $show_error_severity );
}

if ( ! empty( $file_results ) ) {
Expand All @@ -288,7 +288,7 @@ static function ( $dirs ) use ( $excluded_files ) {
$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( $file_results, intval( $error_severity ), intval( $warning_severity ) );

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

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L291

Added line #L291 was not covered by tests
}

if ( ! empty( $file_results ) ) {
Expand Down Expand Up @@ -666,50 +666,73 @@ private function has_runtime_check( array $checks ) {
}

/**
* Returns check results filtered by severity level.
*
* @since 1.1.0
* Returns check results filtered by severity level, with optional error-to-warning conversion.
*
* @param array $results Check results.
* @param int $error_severity Error severity level.
* @param int $warning_severity Warning severity level.
* @param bool $show_error_severity Show error severity as warning.
* @return array Filtered results.
*/
private function get_filtered_results_by_severity( $results, $error_severity, $warning_severity, $show_error_severity = false ) {
private function get_filtered_results( $results, $error_severity, $warning_severity, $show_error_severity = false ) {
// First, get the filtered errors and warnings.
$filtered_results = $this->get_filtered_results_by_severity( $results, $error_severity, $warning_severity );

// If requested, convert certain errors to warnings.
if ( $show_error_severity ) {
$errors_as_warnings = $this->convert_errors_to_warnings( $results, $error_severity );
return array_merge( $filtered_results, $errors_as_warnings );

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

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L683-L684

Added lines #L683 - L684 were not covered by tests
}

return $filtered_results;
}
/**
* Returns check results filtered by severity level.
*
* @param array $results Check results.
* @param int $error_severity Error severity level.
* @param int $warning_severity Warning severity level.
* @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 );
}
$results,
function ( $item ) use ( $error_severity ) {
return ( 'ERROR' === $item['type'] && $item['severity'] >= $error_severity );
}
);

// Converts errors to warnings if severity is less than the error severity level.
$errors_warning = array();
if ( $show_error_severity ) {
$errors_warning = array_filter(
$warnings = array_filter(
$results,
function ( $item ) use ( $warning_severity ) {
return ( 'WARNING' === $item['type'] && $item['severity'] >= $warning_severity );
}
);

return array_merge( $errors, $warnings );
}
/**
* Converts errors below the severity threshold to warnings.
*
* @param array $results Check results.
* @param int $error_severity Error severity level threshold.
* @return array Converted results where applicable.
*/
private function convert_errors_to_warnings( $results, $error_severity ) {
$errors_warning = array_filter(
$results,
function ( $item ) use ( $error_severity ) {
return ( 'ERROR' === $item['type'] && $item['severity'] < $error_severity );
return ( 'ERROR' === $item['type'] && $item['severity'] < $error_severity );

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

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L721-L725

Added lines #L721 - L725 were not covered by tests
}
);
);

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

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

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L729-L734

Added lines #L729 - L734 were not covered by tests
);
}

$warnings = array_filter(
$results,
function ( $item ) use ( $warning_severity ) {
return ( 'WARNING' === $item['type'] && $item['severity'] >= $warning_severity );
}
);

return array_merge( $errors, $errors_warning, $warnings );
}

}

0 comments on commit 5437ba4

Please sign in to comment.