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

Implement Readme Parser #346

Merged
merged 5 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"composer/installers": "^v1.12.0 || ^2.2",
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.1 || ^0.5 || ^0.6.2 || ^0.7 || ^1.0",
"wp-coding-standards/wpcs": "^3.0.0",
"automattic/vipwpcs": "^3.0.0"
"automattic/vipwpcs": "^3.0.0",
"afragen/wordpress-plugin-readme-parser": "dev-master"
ernilambar marked this conversation as resolved.
Show resolved Hide resolved
},
"require-dev": {
"wp-cli/extension-command": "^2.1",
Expand Down
103 changes: 101 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 87 additions & 47 deletions includes/Checker/Checks/Plugin_Readme_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use WordPress\Plugin_Check\Traits\Amend_Check_Result;
use WordPress\Plugin_Check\Traits\Find_Readme;
use WordPress\Plugin_Check\Traits\Stable_Check;
use WordPressdotorg\Plugin_Directory\Readme\Parser;

/**
* Check the plugins readme file and contents.
Expand Down Expand Up @@ -69,42 +70,48 @@
return;
}

$readme_file = reset( $readme );

Check warning on line 73 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L73

Added line #L73 was not covered by tests

$parser = new Parser( $readme_file );

Check warning on line 75 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L75

Added line #L75 was not covered by tests

// Check the readme file for default text.
$this->check_default_text( $result, $readme );
$this->check_default_text( $result, $readme_file, $parser );

Check warning on line 78 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L78

Added line #L78 was not covered by tests

// Check the readme file for a valid license.
$this->check_license( $result, $readme );
$this->check_license( $result, $readme_file, $parser );

Check warning on line 81 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L81

Added line #L81 was not covered by tests

// Check the readme file for a valid version.
$this->check_stable_tag( $result, $readme );
$this->check_stable_tag( $result, $readme_file, $parser );

Check warning on line 84 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L84

Added line #L84 was not covered by tests

// Check the readme file for warnings.
$this->check_for_warnings( $result, $readme_file, $parser );

Check warning on line 87 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L87

Added line #L87 was not covered by tests
}

/**
* Checks the readme file for default text.
*
* @since n.e.x.t
*
* @param Check_Result $result The Check Result to amend.
* @param array $files Array of plugin files.
* @param Check_Result $result The Check Result to amend.
* @param string $readme_file Readme file.
* @param Parser $parser The Parser object.
*/
private function check_default_text( Check_Result $result, array $files ) {
$default_text_patterns = array(
'Here is a short description of the plugin.',
'Tags: tag1',
'Donate link: http://example.com/',
);
private function check_default_text( Check_Result $result, string $readme_file, Parser $parser ) {
$short_description = $parser->short_description;
$tags = $parser->tags;
$donate_link = $parser->donate_link;

Check warning on line 102 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L99-L102

Added lines #L99 - L102 were not covered by tests

foreach ( $default_text_patterns as $pattern ) {
$file = self::file_str_contains( $files, $pattern );
if ( $file ) {
$this->add_result_warning_for_file(
$result,
__( 'The readme appears to contain default text.', 'plugin-check' ),
'default_readme_text',
$file
);
break;
}
if (
in_array( 'tag1', $tags, true )
|| str_contains( $short_description, 'Here is a short description of the plugin.' )
|| str_contains( $donate_link, '//example.com/' )

Check warning on line 107 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L105-L107

Added lines #L105 - L107 were not covered by tests
) {
$this->add_result_warning_for_file(
$result,
__( 'The readme appears to contain default text.', 'plugin-check' ),
'default_readme_text',
$readme_file
);

Check warning on line 114 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L109-L114

Added lines #L109 - L114 were not covered by tests
}
}

Expand All @@ -113,25 +120,20 @@
*
* @since n.e.x.t
*
* @param Check_Result $result The Check Result to amend.
* @param array $files Array of plugin files.
* @param Check_Result $result The Check Result to amend.
* @param string $readme_file Readme file.
* @param Parser $parser The Parser object.
*/
private function check_license( Check_Result $result, array $files ) {
$matches = array();
// Get the license from the readme file.
$file = self::file_preg_match( '/(License:|License URI:)\s*(.+)*/i', $files, $matches );

if ( empty( $matches ) ) {
return;
}
private function check_license( Check_Result $result, string $readme_file, Parser $parser ) {
$license = $parser->license;

Check warning on line 128 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L127-L128

Added lines #L127 - L128 were not covered by tests

// Test for a valid SPDX license identifier.
if ( ! preg_match( '/^([a-z0-9\-\+\.]+)(\sor\s([a-z0-9\-\+\.]+))*$/i', $matches[2] ) ) {
if ( ! empty( $license ) && ! preg_match( '/^([a-z0-9\-\+\.]+)(\sor\s([a-z0-9\-\+\.]+))*$/i', $license ) ) {

Check warning on line 131 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L131

Added line #L131 was not covered by tests
$this->add_result_warning_for_file(
$result,
__( 'Your plugin has an invalid license declared. Please update your readme with a valid SPDX license identifier.', 'plugin-check' ),
'invalid_license',
$file
$readme_file

Check warning on line 136 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L136

Added line #L136 was not covered by tests
);
}
}
Expand All @@ -141,25 +143,19 @@
*
* @since n.e.x.t
*
* @param Check_Result $result The Check Result to amend.
* @param array $files Array of plugin files.
* @param Check_Result $result The Check Result to amend.
* @param string $readme_file Readme file.
* @param Parser $parser The Parser object.
*/
private function check_stable_tag( Check_Result $result, array $files ) {
$matches = array();
// Get the Stable tag from readme file.
$file = self::file_preg_match( '/Stable tag:\s*([a-z0-9\.]+)/i', $files, $matches );
if ( ! $file ) {
return;
}

$stable_tag = isset( $matches[1] ) ? $matches[1] : '';
private function check_stable_tag( Check_Result $result, string $readme_file, Parser $parser ) {
$stable_tag = $parser->stable_tag;

Check warning on line 151 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L150-L151

Added lines #L150 - L151 were not covered by tests

if ( 'trunk' === $stable_tag ) {
$this->add_result_error_for_file(
$result,
__( "It's recommended not to use 'Stable Tag: trunk'.", 'plugin-check' ),
'trunk_stable_tag',
$file
$readme_file

Check warning on line 158 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L158

Added line #L158 was not covered by tests
);
}

Expand All @@ -174,7 +170,51 @@
$result,
__( 'The Stable Tag in your readme file does not match the version in your main plugin file.', 'plugin-check' ),
'stable_tag_mismatch',
$file
$readme_file
);

Check warning on line 174 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L173-L174

Added lines #L173 - L174 were not covered by tests
}
}

/**
* Checks the readme file warnings.
*
* @since n.e.x.t
*
* @param Check_Result $result The Check Result to amend.
* @param string $readme_file Readme file.
* @param Parser $parser The Parser object.
*/
private function check_for_warnings( Check_Result $result, string $readme_file, Parser $parser ) {
$warnings = $parser->warnings ? $parser->warnings : array();

Check warning on line 188 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L187-L188

Added lines #L187 - L188 were not covered by tests

$warning_keys = array_keys( $warnings );

Check warning on line 190 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L190

Added line #L190 was not covered by tests

$ignored_warnings = array(
'contributor_ignored',
);

Check warning on line 194 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L192-L194

Added lines #L192 - L194 were not covered by tests

/**
* Filter the list of ignored readme parser warnings.
*
* @since n.e.x.t
*
* @param array $ignored_warnings Array of ignored warning keys.
* @param Parser $parser The Parser object.
*/
$ignored_warnings = (array) apply_filters( 'wp_plugin_check_ignored_readme_warnings', $ignored_warnings, $parser );

Check warning on line 204 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L204

Added line #L204 was not covered by tests

$warning_keys = array_diff( $warning_keys, $ignored_warnings );

Check warning on line 206 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L206

Added line #L206 was not covered by tests

if ( ! empty( $warning_keys ) ) {
$this->add_result_warning_for_file(
$result,
sprintf(

Check warning on line 211 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L208-L211

Added lines #L208 - L211 were not covered by tests
/* translators: list of warnings */
esc_html__( 'The following readme parser warnings were detected: %s', 'plugin-check' ),
esc_html( implode( ', ', $warning_keys ) )
),
'readme_parser_warnings',
$readme_file

Check warning on line 217 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L213-L217

Added lines #L213 - L217 were not covered by tests
);
}
}
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ parameters:
message: '/^Function str_contains not found.$/'
paths:
- includes/Checker/Checks/Abstract_File_Check.php
- includes/Checker/Checks/Plugin_Readme_Check.php
- includes/Traits/Find_Readme.php
- includes/Traits/File_Editor_URL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Plugin Name: Test Plugin Readme Errors (Parser Warnings)
* Plugin URI: https://github.com/WordPress/plugin-check
* Description: Test plugin for the Readme check.
* Requires at least: 6.0
* Requires PHP: 5.6
* Version: 1.0.0
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: test-plugin-check-errors-parser-warnings
*
* @package test-plugin-check-errors-parser-warnings
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

=== Test Plugin Readme Errors Parser Warnings ===

Contributors: plugin-check
Requires at least: 6.0
Tested up to: 6.1
Requires PHP: PHP 5.6
Stable tag: 1.0.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Tags: testing, security

Plugin description.
Loading