diff --git a/docs/CLI.md b/docs/CLI.md index 51bc584d0..b12ff179f 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -47,6 +47,9 @@ By default, `.git`, `vendor` and `node_modules` directories are excluded. [--exclude-files=] : Additional files to exclude from checks. + +[--severity=] +: Severity level. ``` ## EXAMPLES ``` diff --git a/includes/CLI/Plugin_Check_Command.php b/includes/CLI/Plugin_Check_Command.php index e84b7f41f..7ae711a99 100644 --- a/includes/CLI/Plugin_Check_Command.php +++ b/includes/CLI/Plugin_Check_Command.php @@ -20,6 +20,8 @@ /** * Plugin check command. + * + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */ final class Plugin_Check_Command { @@ -101,6 +103,9 @@ public function __construct( Plugin_Context $plugin_context ) { * [--exclude-files=] * : Additional files to exclude from checks. * + * [--severity=] + * : Severity level. + * * ## EXAMPLES * * wp plugin check akismet @@ -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. @@ -129,6 +135,7 @@ public function check( $args, $assoc_args ) { 'ignore-warnings' => false, 'ignore-errors' => false, 'include-experimental' => false, + 'severity' => '', ) ); @@ -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'] ) ); + } + + 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'] ) ); + } + + if ( ! empty( $file_results ) ) { + $this->display_results( $formatter, $file_name, $file_results ); + } } } @@ -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 ); + } + ); + } } diff --git a/includes/Checker/Check_Result.php b/includes/Checker/Check_Result.php index 88633f1aa..e79e5270d 100644 --- a/includes/Checker/Check_Result.php +++ b/includes/Checker/Check_Result.php @@ -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( diff --git a/includes/Traits/Amend_Check_Result.php b/includes/Traits/Amend_Check_Result.php index 8da3ae64e..75d1ab071 100644 --- a/includes/Traits/Amend_Check_Result.php +++ b/includes/Traits/Amend_Check_Result.php @@ -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, ) ); } @@ -50,15 +52,16 @@ 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 ); } /** @@ -66,14 +69,15 @@ protected function add_result_error_for_file( Check_Result $result, $message, $c * * @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 ); } } diff --git a/tests/phpunit/tests/Checker/Check_Result_Tests.php b/tests/phpunit/tests/Checker/Check_Result_Tests.php index 98398307d..2bc623caa 100644 --- a/tests/phpunit/tests/Checker/Check_Result_Tests.php +++ b/tests/phpunit/tests/Checker/Check_Result_Tests.php @@ -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] ); @@ -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] ); @@ -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] ); @@ -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] );