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 a2340ec
Show file tree
Hide file tree
Showing 6 changed files with 236 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 );

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
}

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

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 (
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.

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 @@ -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;

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 @@ -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;

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

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;

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( 'plugin_check_readme_warnings_ignored', $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: %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
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.
20 changes: 20 additions & 0 deletions tests/phpunit/tests/Checker/Checks/Plugin_Readme_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,24 @@ public function test_single_file_plugin_without_error_for_trademarks() {
$this->assertSame( 0, $check_result->get_error_count() );
$this->assertSame( 0, $check_result->get_warning_count() );
}

public function test_run_with_errors_parser_warnings() {
$readme_check = new Plugin_Readme_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-plugin-readme-parser-warnings/load.php' );
$check_result = new Check_Result( $check_context );

$readme_check->run( $check_result );

$warnings = $check_result->get_warnings();

$this->assertNotEmpty( $warnings );
$this->assertArrayHasKey( 'readme.txt', $warnings );
$this->assertEquals( 1, $check_result->get_warning_count() );

// Check for parser warning.
$this->assertArrayHasKey( 0, $warnings['readme.txt'] );
$this->assertArrayHasKey( 0, $warnings['readme.txt'][0] );
$this->assertArrayHasKey( 'code', $warnings['readme.txt'][0][0][0] );
$this->assertEquals( 'readme_parser_warnings', $warnings['readme.txt'][0][0][0]['code'] );
}
}

0 comments on commit a2340ec

Please sign in to comment.