Skip to content

Commit

Permalink
Add severity level in check
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Jul 15, 2024
1 parent de56bdd commit aa03633
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 48 deletions.
3 changes: 3 additions & 0 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ By default, `.git`, `vendor` and `node_modules` directories are excluded.
[--exclude-files=<files>]
: Additional files to exclude from checks.
[--severity=<severity>]
: Severity level.
```
## EXAMPLES
```
Expand Down
43 changes: 41 additions & 2 deletions includes/CLI/Plugin_Check_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

/**
* Plugin check command.
*
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
final class Plugin_Check_Command {

Expand Down Expand Up @@ -101,6 +103,9 @@ public function __construct( Plugin_Context $plugin_context ) {
* [--exclude-files=<files>]
* : Additional files to exclude from checks.
*
* [--severity=<severity>]
* : Severity level.
*
* ## EXAMPLES
*
* wp plugin check akismet
Expand All @@ -118,6 +123,7 @@ public function __construct( Plugin_Context $plugin_context ) {
*
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function check( $args, $assoc_args ) {
// Get options based on the CLI arguments.
Expand All @@ -129,6 +135,7 @@ public function check( $args, $assoc_args ) {
'ignore-warnings' => false,
'ignore-errors' => false,
'include-experimental' => false,
'severity' => '',
)
);

Expand Down Expand Up @@ -237,13 +244,27 @@ static function ( $dirs ) use ( $excluded_files ) {
unset( $warnings[ $file_name ] );
}
$file_results = $this->flatten_file_results( $file_errors, $file_warnings );
$this->display_results( $formatter, $file_name, $file_results );

if ( '' !== $options['severity'] ) {
$file_results = $this->get_filtered_results_by_severity( $file_results, intval( $options['severity'] ) );

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

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L249

Added line #L249 was not covered by tests
}

if ( ! empty( $file_results ) ) {
$this->display_results( $formatter, $file_name, $file_results );
}
}

// If there are any files left with only warnings, print those next.
foreach ( $warnings as $file_name => $file_warnings ) {
$file_results = $this->flatten_file_results( array(), $file_warnings );
$this->display_results( $formatter, $file_name, $file_results );

if ( '' !== $options['severity'] ) {
$file_results = $this->get_filtered_results_by_severity( $file_results, intval( $options['severity'] ) );

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

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L262

Added line #L262 was not covered by tests
}

if ( ! empty( $file_results ) ) {
$this->display_results( $formatter, $file_name, $file_results );
}
}
}

Expand Down Expand Up @@ -599,4 +620,22 @@ private function has_runtime_check( array $checks ) {

return false;
}

/**
* Returns check results filtered by severity level.
*
* @since 1.1.0
*
* @param array $results Check results.
* @param int $severity Severity level.
* @return array Filtered results.
*/
private function get_filtered_results_by_severity( $results, $severity ) {
return array_filter(
$results,
function ( $item ) use ( $severity ) {
return ( $item['severity'] >= $severity );

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

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L633-L637

Added lines #L633 - L637 were not covered by tests
}
);
}
}
11 changes: 6 additions & 5 deletions includes/Checker/Check_Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ public function plugin() {
*/
public function add_message( $error, $message, $args = array() ) {
$defaults = array(
'code' => '',
'file' => '',
'line' => 0,
'column' => 0,
'link' => '',
'code' => '',
'file' => '',
'line' => 0,
'column' => 0,
'link' => '',
'severity' => 5,
);

$data = array_merge(
Expand Down
62 changes: 33 additions & 29 deletions includes/Traits/Amend_Check_Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,26 @@ trait Amend_Check_Result {
*
* @since 1.0.0
*
* @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 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).
* @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 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).
* @param int $severity Severity level. Default is 5.
*/
protected function add_result_message_for_file( Check_Result $result, $error, $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, $severity = 5 ) {
$result->add_message(
(bool) $error,
$message,
array(
'code' => $code,
'file' => str_replace( $result->plugin()->path(), '', $file ),
'line' => $line,
'column' => $column,
'link' => $this->get_file_editor_url( $result, $file, $line ),
'code' => $code,
'file' => str_replace( $result->plugin()->path(), '', $file ),
'line' => $line,
'column' => $column,
'link' => $this->get_file_editor_url( $result, $file, $line ),
'severity' => $severity,
)
);
}
Expand All @@ -50,30 +52,32 @@ protected function add_result_message_for_file( Check_Result $result, $error, $m
*
* @since 1.0.0
*
* @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 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).
* @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 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).
* @param int $severity Severity level. Default is 5.
*/
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 );
protected function add_result_error_for_file( Check_Result $result, $message, $code, $file, $line = 0, $column = 0, $severity = 5 ) {
$this->add_result_message_for_file( $result, true, $message, $code, $file, $line, $column, $severity );
}

/**
* Amends the given result with a warning message for the specified file.
*
* @since 1.0.0
*
* @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).
* @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).
* @param int $severity Severity level. Default is 5.
*/
protected function add_result_warning_for_file( Check_Result $result, $message, $code, $file, $line = 0, $column = 0 ) {
$this->add_result_message_for_file( $result, false, $message, $code, $file, $line, $column );
protected function add_result_warning_for_file( Check_Result $result, $message, $code, $file, $line = 0, $column = 0, $severity = 5 ) {
$this->add_result_message_for_file( $result, false, $message, $code, $file, $line, $column, $severity );
}
}
28 changes: 16 additions & 12 deletions tests/phpunit/tests/Checker/Check_Result_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ public function test_add_message_with_warning() {

// Tests the warning exists in the array.
$expected = array(
'message' => 'Warning message',
'code' => 'test_warning',
'link' => '',
'message' => 'Warning message',
'code' => 'test_warning',
'link' => '',
'severity' => 5,
);

$this->assertEquals( $expected, $warnings['test-plugin.php'][12][40][0] );
Expand Down Expand Up @@ -91,9 +92,10 @@ public function test_add_message_with_error() {

// Tests the error exists in the array.
$expected = array(
'message' => 'Error message',
'code' => 'test_error',
'link' => '',
'message' => 'Error message',
'code' => 'test_error',
'link' => '',
'severity' => 5,
);

$this->assertEquals( $expected, $errors['test-plugin.php'][22][30][0] );
Expand Down Expand Up @@ -122,9 +124,10 @@ public function test_get_errors_with_errors() {

// Tests the error exists in the array.
$expected = array(
'message' => 'Error message',
'code' => 'test_error',
'link' => '',
'message' => 'Error message',
'code' => 'test_error',
'link' => '',
'severity' => 5,
);

$this->assertEquals( $expected, $errors['test-plugin.php'][22][30][0] );
Expand Down Expand Up @@ -153,9 +156,10 @@ public function test_get_warnings_with_warnings() {

// Tests the warning exists in the array.
$expected = array(
'message' => 'Warning message',
'code' => 'test_warning',
'link' => '',
'message' => 'Warning message',
'code' => 'test_warning',
'link' => '',
'severity' => 5,
);

$this->assertEquals( $expected, $warnings['test-plugin.php'][22][30][0] );
Expand Down

0 comments on commit aa03633

Please sign in to comment.