From 95e863e339a7c7d757aa2d2076b11cadcc0e21b7 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Thu, 19 Oct 2023 10:11:11 +0530 Subject: [PATCH] Add wrapper function add_result_message_for_file() --- .../Checks/Abstract_PHP_CodeSniffer_Check.php | 30 ++++------- includes/Checker/Checks/File_Type_Check.php | 22 +++----- includes/Traits/Amend_Check_Result.php | 50 +++++++++++-------- 3 files changed, 45 insertions(+), 57 deletions(-) diff --git a/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php b/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php index ce426c276..8fb5dfc76 100644 --- a/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php +++ b/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php @@ -61,8 +61,6 @@ abstract protected function get_args(); * * @throws Exception Thrown when the check fails with a critical error (unrelated to any errors detected as part of * the check). - * - * @SuppressWarnings(PHPMD.NPathComplexity) */ final public function run( Check_Result $result ) { // Include the PHPCS autoloader. @@ -127,25 +125,15 @@ final public function run( Check_Result $result ) { } foreach ( $file_results['messages'] as $file_message ) { - if ( strtoupper( $file_message['type'] ) === 'ERROR' ) { - $this->add_result_error_for_file( - $result, - $file_message['message'], - $file_message['source'], - $file_name, - $file_message['line'], - $file_message['column'] - ); - } else { - $this->add_result_warning_for_file( - $result, - $file_message['message'], - $file_message['source'], - $file_name, - $file_message['line'], - $file_message['column'] - ); - } + $this->add_result_message_for_file( + $result, + strtoupper( $file_message['type'] ) === 'ERROR', + $file_message['message'], + $file_message['source'], + $file_name, + $file_message['line'], + $file_message['column'] + ); } } } diff --git a/includes/Checker/Checks/File_Type_Check.php b/includes/Checker/Checks/File_Type_Check.php index 4ac9b5286..0e375932d 100644 --- a/includes/Checker/Checks/File_Type_Check.php +++ b/includes/Checker/Checks/File_Type_Check.php @@ -164,21 +164,13 @@ function ( $directory ) use ( $directories ) { // Only use an error in production, otherwise a warning. $is_error = ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) && 'production' === wp_get_environment_type(); foreach ( $vcs_directories as $dir ) { - if ( $is_error ) { - $this->add_result_error_for_file( - $result, - __( 'Version control checkouts should not be present.', 'plugin-check' ), - 'vcs_present', - str_replace( $result->plugin()->path(), '', $dir ) - ); - } else { - $this->add_result_warning_for_file( - $result, - __( 'Version control checkouts should not be present.', 'plugin-check' ), - 'vcs_present', - str_replace( $result->plugin()->path(), '', $dir ) - ); - } + $this->add_result_message_for_file( + $result, + $is_error, + __( 'Version control checkouts should not be present.', 'plugin-check' ), + 'vcs_present', + str_replace( $result->plugin()->path(), '', $dir ) + ); } } } diff --git a/includes/Traits/Amend_Check_Result.php b/includes/Traits/Amend_Check_Result.php index fde5ace04..b36989b3e 100644 --- a/includes/Traits/Amend_Check_Result.php +++ b/includes/Traits/Amend_Check_Result.php @@ -10,27 +10,28 @@ use WordPress\Plugin_Check\Checker\Check_Result; /** - * Trait for check result. + * Trait for amending check results. * * @since n.e.x.t */ trait Amend_Check_Result { /** - * Amends the given result with an error for the given file, code, and message. + * Amends the given result with a message for the specified file, including error information. * * @since n.e.x.t * * @param Check_Result $result The check result to amend, including the plugin context to check. + * @param bool $error Whether it is an error or notice. * @param string $message Error message. * @param string $code Error code. - * @param string $file Absolute path to the file found. - * @param int $line The line on which the message occurred. Default 0 (unknown line). - * @param int $column The column on which the message occurred. Default 0 (unknown column). + * @param string $file Absolute path to the file where the issue was found. + * @param int $line The line on which the message occurred. Default is 0 (unknown line). + * @param int $column The column on which the message occurred. Default is 0 (unknown column). */ - protected function add_result_error_for_file( Check_Result $result, $message, $code, $file, $line = 0, $column = 0 ) { + protected function add_result_message_for_file( Check_Result $result, $error, $message, $code, $file, $line = 0, $column = 0 ) { $result->add_message( - true, + $error ? true : false, $message, array( 'code' => $code, @@ -42,27 +43,34 @@ protected function add_result_error_for_file( Check_Result $result, $message, $c } /** - * Amends the given result with a warning for the given file, code, and message. + * Amends the given result with an error message for the specified file. * * @since n.e.x.t * * @param Check_Result $result The check result to amend, including the plugin context to check. * @param string $message Error message. * @param string $code Error code. - * @param string $file Absolute path to the file found. - * @param int $line The line on which the message occurred. Default 0 (unknown line). - * @param int $column The column on which the message occurred. Default 0 (unknown column). + * @param string $file Absolute path to the file where the error was found. + * @param int $line The line on which the error occurred. Default is 0 (unknown line). + * @param int $column The column on which the error occurred. Default is 0 (unknown column). + */ + protected function add_result_error_for_file( Check_Result $result, $message, $code, $file, $line = 0, $column = 0 ) { + $this->add_result_message_for_file( $result, true, $message, $code, $file, $line, $column ); + } + + /** + * Amends the given result with a warning message for the specified file. + * + * @since n.e.x.t + * + * @param Check_Result $result The check result to amend, including the plugin context to check. + * @param string $message Error message. + * @param string $code Error code. + * @param string $file Absolute path to the file where the warning was found. + * @param int $line The line on which the warning occurred. Default is 0 (unknown line). + * @param int $column The column on which the warning occurred. Default is 0 (unknown column). */ protected function add_result_warning_for_file( Check_Result $result, $message, $code, $file, $line = 0, $column = 0 ) { - $result->add_message( - false, - $message, - array( - 'code' => $code, - 'file' => str_replace( $result->plugin()->path(), '', $file ), - 'line' => $line, - 'column' => $column, - ) - ); + $this->add_result_message_for_file( $result, false, $message, $code, $file, $line, $column ); } }