Skip to content

Commit

Permalink
Merge pull request #527 from WordPress/479-add-severity-level
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored Aug 14, 2024
2 parents df0a03c + c84b075 commit ec0557e
Show file tree
Hide file tree
Showing 15 changed files with 455 additions and 89 deletions.
15 changes: 12 additions & 3 deletions docs/CLI.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[Back to overview](./README.md)

# wp plugin check
# wp plugin check

Runs plugin check.

Expand Down Expand Up @@ -47,6 +47,15 @@ By default, `.git`, `vendor` and `node_modules` directories are excluded.
[--exclude-files=<files>]
: Additional files to exclude from checks.
[--severity=<severity>]
: Severity level.
[--error-severity=<error-severity>]
: Error severity level.
[--warning-severity=<warning-severity>]
: Warning severity level.
```
## EXAMPLES
```
Expand All @@ -55,7 +64,7 @@ wp plugin check akismet --checks=late_escaping
wp plugin check akismet --format=json
```

# wp plugin list-checks
# wp plugin list-checks

Lists the available checks for plugins.

Expand Down Expand Up @@ -86,7 +95,7 @@ wp plugin list-checks
wp plugin list-checks --format=json
```

# wp plugin list-check-categories
# wp plugin list-check-categories

Lists the available check categories for plugins.

Expand Down
35 changes: 35 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,38 @@
* [CLI Commands](CLI.md)
* [Running Unit tests](running-unit-tests.md)
* [Releasing a New Version of Plugin](releasing.md)

## Checks Developed in the Plugin

These are the results and severity levels for each check.

| Category | Check | Severity |
| -------- | ------- | ------- |
| General | Internationalization | 5 |
| Performance | Enqueued Resources | 5 |
| Performance | Scripts in Footer | 5 |
| Performance | Scripts Scope | 5 |
| Performance | Scripts Size | 5 |
| Performance | Styles Scope | 5 |
| Performance | Styles Size | 5 |
| Performance | Query Parameters | 5 |
| Plugin Repo | Code Obfuscation | 6 |
| Plugin Repo | File Type Check | 8 |
| Plugin Repo | LocalHost | 8 |
| Plugin Repo | Unfiltered Uploads | 7 |
| Plugin Repo | Plugin Header TextDomain | 6 |
| Plugin Repo | Readme Headers | 9 |
| Plugin Repo | Readme Default Text | 7 |
| Plugin Repo | Readme Check License | 9 |
| Plugin Repo | Readme Readme Stable Tag | 9 |
| Plugin Repo | Readme Upgrade Notice | 9 |
| Plugin Repo | Readme Contributor Ignored | 5 |
| Plugin Repo | Readme PHP Header Ignored | 5 |
| Plugin Repo | Readme Tested up to | 7 |
| Plugin Repo | Readme Too many tags | 5 |
| Plugin Repo | Readme Ignored Tags | 5 |
| Plugin Repo | Readme No short description | 6 |
| Plugin Repo | Readme Trimmed short description | 6 |
| Plugin Repo | PHP Coding Standards | 5 |
| Plugin Repo | Updater Check | 9 |
| Plugin Repo | Trademarks Check | 5 |
65 changes: 63 additions & 2 deletions includes/CLI/Plugin_Check_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

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

Expand Down Expand Up @@ -102,6 +104,15 @@ public function __construct( Plugin_Context $plugin_context ) {
* [--exclude-files=<files>]
* : Additional files to exclude from checks.
*
* [--severity=<severity>]
* : Severity level.
*
* [--error-severity=<error-severity>]
* : Error severity level.
*
* [--warning-severity=<warning-severity>]
* : Warning severity level.
*
* ## EXAMPLES
*
* wp plugin check akismet
Expand All @@ -119,6 +130,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 @@ -130,6 +142,9 @@ public function check( $args, $assoc_args ) {
'ignore-warnings' => false,
'ignore-errors' => false,
'include-experimental' => false,
'severity' => '',
'error-severity' => '',
'warning-severity' => '',
)
);

Expand Down Expand Up @@ -229,6 +244,10 @@ static function ( $dirs ) use ( $excluded_files ) {
// Get formatter.
$formatter = $this->get_formatter( $assoc_args, $default_fields );

// Severity.
$error_severity = ! empty( $options['error-severity'] ) ? $options['error-severity'] : $options['severity'];
$warning_severity = ! empty( $options['warning-severity'] ) ? $options['warning-severity'] : $options['severity'];

// Print the formatted results.
// Go over all files with errors first and print them, combined with any warnings in the same file.
foreach ( $errors as $file_name => $file_errors ) {
Expand All @@ -238,13 +257,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 ( '' !== $error_severity || '' !== $warning_severity ) {
$file_results = $this->get_filtered_results_by_severity( $file_results, intval( $error_severity ), intval( $warning_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 ( '' !== $error_severity || '' !== $warning_severity ) {
$file_results = $this->get_filtered_results_by_severity( $file_results, intval( $error_severity ), intval( $warning_severity ) );
}

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

Expand Down Expand Up @@ -617,4 +650,32 @@ 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 $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 );
}
);

$warnings = array_filter(
$results,
function ( $item ) use ( $warning_severity ) {
return ( 'WARNING' === $item['type'] && $item['severity'] >= $warning_severity );
}
);

return array_merge( $errors, $warnings );
}
}
13 changes: 7 additions & 6 deletions includes/Checker/Check_Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ public function plugin() {
*/
public function add_message( $error, $message, $args = array() ) {
$defaults = array(
'code' => '',
'file' => '',
'line' => 0,
'column' => 0,
'link' => '',
'docs' => '',
'code' => '',
'file' => '',
'line' => 0,
'column' => 0,
'link' => '',
'docs' => '',
'severity' => 5,
);

$data = array_merge(
Expand Down
4 changes: 3 additions & 1 deletion includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ final public function run( Check_Result $result ) {
$file_message['source'],
$file_name,
$file_message['line'],
$file_message['column']
$file_message['column'],
'',
$file_message['severity']
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ protected function look_for_zendguard( Check_Result $result, array $php_files )
$file['file'],
$file['line'],
$file['column'],
'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource'
'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource',
6
);
}
}
Expand Down Expand Up @@ -140,7 +141,8 @@ protected function look_for_sourceguardian( Check_Result $result, array $php_fil
$file['file'],
$file['line'],
$file['column'],
'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource'
'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource',
6
);
}
}
Expand Down Expand Up @@ -170,7 +172,8 @@ protected function look_for_ioncube( Check_Result $result, array $php_files ) {
$file['file'],
$file['line'],
$file['column'],
'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource'
'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource',
6
);
}
}
Expand Down
30 changes: 25 additions & 5 deletions includes/Checker/Checks/Plugin_Repo/File_Type_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ protected function look_for_compressed_files( Check_Result $result, array $files
$result,
__( 'Compressed files are not permitted.', 'plugin-check' ),
'compressed_files',
$file
$file,
0,
0,
'',
8
);
}
}
Expand All @@ -130,7 +134,11 @@ protected function look_for_phar_files( Check_Result $result, array $files ) {
$result,
__( 'Phar files are not permitted.', 'plugin-check' ),
'phar_files',
$file
$file,
0,
0,
'',
8
);
}
}
Expand Down Expand Up @@ -170,7 +178,11 @@ function ( $directory ) use ( $directories ) {
$is_error,
__( 'Version control checkouts should not be present.', 'plugin-check' ),
'vcs_present',
$dir
$dir,
0,
0,
'',
8
);
}
}
Expand All @@ -193,7 +205,11 @@ protected function look_for_hidden_files( Check_Result $result, array $files ) {
$result,
__( 'Hidden files are not permitted.', 'plugin-check' ),
'hidden_files',
$file
$file,
0,
0,
'',
8
);
}
}
Expand All @@ -218,7 +234,11 @@ protected function look_for_application_files( Check_Result $result, array $file
$result,
__( 'Application files are not permitted.', 'plugin-check' ),
'application_detected',
$file
$file,
0,
0,
'',
8
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion includes/Checker/Checks/Plugin_Repo/Localhost_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ protected function check_files( Check_Result $result, array $files ) {
'localhost_code_detected',
$file['file'],
$file['line'],
$file['column']
$file['column'],
'',
8
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ protected function check_files( Check_Result $result, array $files ) {
$file,
0,
0,
'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#files-unfiltered-uploads'
'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#files-unfiltered-uploads',
7
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ public function run( Check_Result $result ) {
esc_html( $plugin_slug )
),
'textdomain_mismatch',
$plugin_main_file
$plugin_main_file,
0,
0,
'',
6
);
}
}
Expand Down
Loading

0 comments on commit ec0557e

Please sign in to comment.