Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for severity levels for warnings and errors #527

Merged
merged 13 commits into from
Aug 14, 2024
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
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 | 8 |
| 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 |
43 changes: 41 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,9 @@
* [--exclude-files=<files>]
* : Additional files to exclude from checks.
*
* [--severity=<severity>]
* : Severity level.
*
* ## EXAMPLES
*
* wp plugin check akismet
Expand All @@ -119,6 +124,7 @@
*
* @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 +136,7 @@
'ignore-warnings' => false,
'ignore-errors' => false,
'include-experimental' => false,
'severity' => '',
)
);

Expand Down Expand Up @@ -238,13 +245,27 @@
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 250 in includes/CLI/Plugin_Check_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L250

Added line #L250 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 263 in includes/CLI/Plugin_Check_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L263

Added line #L263 was not covered by tests
}

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

Expand Down Expand Up @@ -617,4 +638,22 @@

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 655 in includes/CLI/Plugin_Check_Command.php

View check run for this annotation

Codecov / codecov/patch

includes/CLI/Plugin_Check_Command.php#L651-L655

Added lines #L651 - L655 were not covered by tests
}
);
}
}
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',
8
);
}
}
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',
8
);
}
}
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',
8
);
}
}
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