From 5437ba4eb30e14fb99b734cbcfee15739733d537 Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Fri, 11 Oct 2024 19:11:59 +0200 Subject: [PATCH] phpmd test fixed --- includes/CLI/Plugin_Check_Command.php | 81 +++++++++++++++++---------- 1 file changed, 52 insertions(+), 29 deletions(-) diff --git a/includes/CLI/Plugin_Check_Command.php b/includes/CLI/Plugin_Check_Command.php index 4639be4b6..d5b7eb69f 100644 --- a/includes/CLI/Plugin_Check_Command.php +++ b/includes/CLI/Plugin_Check_Command.php @@ -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 ) ) { @@ -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 ) ); } if ( ! empty( $file_results ) ) { @@ -666,9 +666,7 @@ 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. @@ -676,40 +674,65 @@ private function has_runtime_check( array $checks ) { * @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 ); + } + + 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 ); } - ); + ); - $errors_warning = array_map( + return array_map( function ( $item ) { - $item['type'] = 'WARNING'; - return $item; + $item['type'] = 'WARNING'; + return $item; }, $errors_warning - ); - } - - $warnings = array_filter( - $results, - function ( $item ) use ( $warning_severity ) { - return ( 'WARNING' === $item['type'] && $item['severity'] >= $warning_severity ); - } ); - - return array_merge( $errors, $errors_warning, $warnings ); } + }