Skip to content

Commit

Permalink
Add wrapper function add_result_message_for_file()
Browse files Browse the repository at this point in the history
  • Loading branch information
mukeshpanchal27 committed Oct 19, 2023
1 parent b40e7b8 commit 95e863e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 57 deletions.
30 changes: 9 additions & 21 deletions includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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']
);
}
}
}
Expand Down
22 changes: 7 additions & 15 deletions includes/Checker/Checks/File_Type_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
);
}
}
}
Expand Down
50 changes: 29 additions & 21 deletions includes/Traits/Amend_Check_Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 );
}
}

0 comments on commit 95e863e

Please sign in to comment.