Skip to content

Commit

Permalink
Implement readme parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Dec 19, 2023
1 parent 0101c91 commit 370c255
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 47 deletions.
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"
},
"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.

128 changes: 84 additions & 44 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,14 +70,21 @@ protected function check_files( Check_Result $result, array $files ) {
return;
}

$readme_file = reset( $readme );

$parser = new Parser( $readme_file );

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

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

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

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

/**
Expand All @@ -85,26 +93,25 @@ protected function check_files( Check_Result $result, array $files ) {
* @since n.e.x.t
*
* @param Check_Result $result The Check Result to amend.
* @param array $files Array of plugin files.
* @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;

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 (
str_contains( $short_description, 'Here is a short description of the plugin.' )

Check failure on line 105 in includes/Checker/Checks/Plugin_Readme_Check.php

View workflow job for this annotation

GitHub Actions / PHP

Function str_contains not found.
|| in_array( 'tags', $tags, true )
|| str_contains( $donate_link, '//example.com/' )

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

View workflow job for this annotation

GitHub Actions / PHP

Function str_contains not found.
) {
$this->add_result_warning_for_file(
$result,
__( 'The readme appears to contain default text.', 'plugin-check' ),
'default_readme_text',
$readme_file
);
}
}

Expand All @@ -114,24 +121,19 @@ private function check_default_text( Check_Result $result, array $files ) {
* @since n.e.x.t
*
* @param Check_Result $result The Check Result to amend.
* @param array $files Array of plugin files.
* @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;

// 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 ) ) {
$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
);
}
}
Expand All @@ -142,24 +144,18 @@ private function check_license( Check_Result $result, array $files ) {
* @since n.e.x.t
*
* @param Check_Result $result The Check Result to amend.
* @param array $files Array of plugin files.
* @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;

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
);
}

Expand All @@ -174,7 +170,51 @@ private function check_stable_tag( Check_Result $result, array $files ) {
$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
);
}
}

/**
* 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;

$warning_keys = array_keys( $warnings );

$ignored_warnings = array(
'contributor_ignored',
);

/**
* 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( 'plugin_check_readme_warnings_ignored', $ignored_warnings, $parser );

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

if ( ! empty( $warning_keys ) ) {
$this->add_result_error_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#L209-L211

Added lines #L209 - L211 were not covered by tests
/* translators: %1$s: list of warnings */
__( 'The following readme parser warnings were detected: %1$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

0 comments on commit 370c255

Please sign in to comment.