Skip to content

Commit 68b88b1

Browse files
authored
Merge pull request #947 from WordPress/use-directory-readme
Use dotorg readme parser if available
2 parents 0c68a18 + e3cce52 commit 68b88b1

5 files changed

Lines changed: 160 additions & 46 deletions

File tree

includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
use WordPress\Plugin_Check\Checker\Check_Categories;
1111
use WordPress\Plugin_Check\Checker\Check_Result;
1212
use WordPress\Plugin_Check\Checker\Checks\Abstract_File_Check;
13-
use WordPress\Plugin_Check\Lib\Readme\Parser;
13+
use WordPress\Plugin_Check\Lib\Readme\Parser as PCPParser;
1414
use WordPress\Plugin_Check\Traits\Amend_Check_Result;
1515
use WordPress\Plugin_Check\Traits\Find_Readme;
1616
use WordPress\Plugin_Check\Traits\License_Utils;
1717
use WordPress\Plugin_Check\Traits\Stable_Check;
1818
use WordPress\Plugin_Check\Traits\Version_Utils;
19+
use WordPressdotorg\Plugin_Directory\Readme\Parser as DotorgParser;
1920

2021
/**
2122
* Check the plugins readme file and contents.
@@ -83,7 +84,7 @@ protected function check_files( Check_Result $result, array $files ) {
8384

8485
$readme_file = reset( $readme );
8586

86-
$parser = new Parser( $readme_file );
87+
$parser = class_exists( DotorgParser::class ) ? new DotorgParser( $readme_file ) : new PCPParser( $readme_file );
8788

8889
// Check the readme file for plugin name.
8990
$this->check_name( $result, $readme_file, $parser );
@@ -121,11 +122,11 @@ protected function check_files( Check_Result $result, array $files ) {
121122
*
122123
* @since 1.0.0
123124
*
124-
* @param Check_Result $result The Check Result to amend.
125-
* @param string $readme_file Readme file.
126-
* @param Parser $parser The Parser object.
125+
* @param Check_Result $result The Check Result to amend.
126+
* @param string $readme_file Readme file.
127+
* @param DotorgParser|PCPParser $parser The Parser object.
127128
*/
128-
private function check_name( Check_Result $result, string $readme_file, Parser $parser ) {
129+
private function check_name( Check_Result $result, string $readme_file, $parser ) {
129130
if ( isset( $parser->warnings['invalid_plugin_name_header'] ) && false === $parser->name ) {
130131
$this->add_result_error_for_file(
131132
$result,
@@ -188,13 +189,13 @@ private function check_name( Check_Result $result, string $readme_file, Parser $
188189
*
189190
* @since 1.0.2
190191
*
191-
* @param Check_Result $result The Check Result to amend.
192-
* @param string $readme_file Readme file.
193-
* @param Parser $parser The Parser object.
192+
* @param Check_Result $result The Check Result to amend.
193+
* @param string $readme_file Readme file.
194+
* @param DotorgParser|PCPParser $parser The Parser object.
194195
*
195196
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
196197
*/
197-
private function check_headers( Check_Result $result, string $readme_file, Parser $parser ) {
198+
private function check_headers( Check_Result $result, string $readme_file, $parser ) {
198199
$ignored_warnings = $this->get_ignored_warnings( $parser );
199200

200201
$fields = array(
@@ -302,11 +303,11 @@ private function check_headers( Check_Result $result, string $readme_file, Parse
302303
*
303304
* @since 1.0.0
304305
*
305-
* @param Check_Result $result The Check Result to amend.
306-
* @param string $readme_file Readme file.
307-
* @param Parser $parser The Parser object.
306+
* @param Check_Result $result The Check Result to amend.
307+
* @param string $readme_file Readme file.
308+
* @param DotorgParser|PCPParser $parser The Parser object.
308309
*/
309-
private function check_default_text( Check_Result $result, string $readme_file, Parser $parser ) {
310+
private function check_default_text( Check_Result $result, string $readme_file, $parser ) {
310311
$short_description = $parser->short_description;
311312
$tags = $parser->tags;
312313
$donate_link = $parser->donate_link;
@@ -334,11 +335,11 @@ private function check_default_text( Check_Result $result, string $readme_file,
334335
*
335336
* @since 1.0.0
336337
*
337-
* @param Check_Result $result The Check Result to amend.
338-
* @param string $readme_file Readme file.
339-
* @param Parser $parser The Parser object.
338+
* @param Check_Result $result The Check Result to amend.
339+
* @param string $readme_file Readme file.
340+
* @param DotorgParser|PCPParser $parser The Parser object.
340341
*/
341-
private function check_license( Check_Result $result, string $readme_file, Parser $parser ) {
342+
private function check_license( Check_Result $result, string $readme_file, $parser ) {
342343
$license = $parser->license;
343344
$matches_license = array();
344345
$plugin_main_file = $result->plugin()->main_file();
@@ -408,11 +409,11 @@ private function check_license( Check_Result $result, string $readme_file, Parse
408409
*
409410
* @since 1.0.0
410411
*
411-
* @param Check_Result $result The Check Result to amend.
412-
* @param string $readme_file Readme file.
413-
* @param Parser $parser The Parser object.
412+
* @param Check_Result $result The Check Result to amend.
413+
* @param string $readme_file Readme file.
414+
* @param DotorgParser|PCPParser $parser The Parser object.
414415
*/
415-
private function check_stable_tag( Check_Result $result, string $readme_file, Parser $parser ) {
416+
private function check_stable_tag( Check_Result $result, string $readme_file, $parser ) {
416417
$stable_tag = $parser->stable_tag;
417418

418419
if ( empty( $stable_tag ) ) {
@@ -487,11 +488,11 @@ private function check_stable_tag( Check_Result $result, string $readme_file, Pa
487488
*
488489
* @since 1.0.2
489490
*
490-
* @param Check_Result $result The Check Result to amend.
491-
* @param string $readme_file Readme file.
492-
* @param Parser $parser The Parser object.
491+
* @param Check_Result $result The Check Result to amend.
492+
* @param string $readme_file Readme file.
493+
* @param DotorgParser|PCPParser $parser The Parser object.
493494
*/
494-
private function check_upgrade_notice( Check_Result $result, string $readme_file, Parser $parser ) {
495+
private function check_upgrade_notice( Check_Result $result, string $readme_file, $parser ) {
495496
$notices = $parser->upgrade_notice;
496497

497498
$maximum_characters = 300;
@@ -521,13 +522,13 @@ private function check_upgrade_notice( Check_Result $result, string $readme_file
521522
*
522523
* @since 1.0.0
523524
*
524-
* @param Check_Result $result The Check Result to amend.
525-
* @param string $readme_file Readme file.
526-
* @param Parser $parser The Parser object.
525+
* @param Check_Result $result The Check Result to amend.
526+
* @param string $readme_file Readme file.
527+
* @param DotorgParser|PCPParser $parser The Parser object.
527528
*
528529
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
529530
*/
530-
private function check_for_warnings( Check_Result $result, string $readme_file, Parser $parser ) {
531+
private function check_for_warnings( Check_Result $result, string $readme_file, $parser ) {
531532
$warnings = $parser->warnings ? $parser->warnings : array();
532533

533534
// This should be ERROR rather than WARNING. So ignoring here to handle separately.
@@ -665,11 +666,11 @@ private function check_for_warnings( Check_Result $result, string $readme_file,
665666
*
666667
* @since 1.3.0
667668
*
668-
* @param Check_Result $result The Check Result to amend.
669-
* @param string $readme_file Readme file.
670-
* @param Parser $parser The Parser object.
669+
* @param Check_Result $result The Check Result to amend.
670+
* @param string $readme_file Readme file.
671+
* @param DotorgParser|PCPParser $parser The Parser object.
671672
*/
672-
private function check_for_donate_link( Check_Result $result, string $readme_file, Parser $parser ) {
673+
private function check_for_donate_link( Check_Result $result, string $readme_file, $parser ) {
673674
$donate_link = $parser->donate_link;
674675

675676
// Bail if empty donate link.
@@ -819,11 +820,11 @@ function ( $value ) {
819820
*
820821
* @since 1.5.0
821822
*
822-
* @param Check_Result $result The Check Result to amend.
823-
* @param string $readme_file Readme file.
824-
* @param Parser $parser The Parser object.
823+
* @param Check_Result $result The Check Result to amend.
824+
* @param string $readme_file Readme file.
825+
* @param DotorgParser|PCPParser $parser The Parser object.
825826
*/
826-
private function check_requires_headers( Check_Result $result, string $readme_file, Parser $parser ) {
827+
private function check_requires_headers( Check_Result $result, string $readme_file, $parser ) {
827828
$ignored_warnings = $this->get_ignored_warnings( $parser );
828829

829830
$found_warnings = $parser->warnings ? $parser->warnings : array();
@@ -881,10 +882,10 @@ private function check_requires_headers( Check_Result $result, string $readme_fi
881882
*
882883
* @since 1.0.2
883884
*
884-
* @param Parser $parser The Parser object.
885+
* @param DotorgParser|PCPParser $parser The Parser object.
885886
* @return array Ignored warnings.
886887
*/
887-
private function get_ignored_warnings( Parser $parser ) {
888+
private function get_ignored_warnings( $parser ) {
888889
$ignored_warnings = array(
889890
'contributor_ignored',
890891
);
@@ -894,8 +895,8 @@ private function get_ignored_warnings( Parser $parser ) {
894895
*
895896
* @since 1.0.2
896897
*
897-
* @param array $ignored_warnings Array of ignored warning keys.
898-
* @param Parser $parser The Parser object.
898+
* @param array $ignored_warnings Array of ignored warning keys.
899+
* @param DotorgParser|PCPParser $parser The Parser object.
899900
*/
900901
$ignored_warnings = (array) apply_filters( 'wp_plugin_check_ignored_readme_warnings', $ignored_warnings, $parser );
901902

includes/Checker/Checks/Plugin_Repo/Trademarks_Check.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
use WordPress\Plugin_Check\Checker\Check_Categories;
1212
use WordPress\Plugin_Check\Checker\Check_Result;
1313
use WordPress\Plugin_Check\Checker\Checks\Abstract_File_Check;
14-
use WordPress\Plugin_Check\Lib\Readme\Parser;
14+
use WordPress\Plugin_Check\Lib\Readme\Parser as PCPParser;
1515
use WordPress\Plugin_Check\Traits\Amend_Check_Result;
1616
use WordPress\Plugin_Check\Traits\Find_Readme;
1717
use WordPress\Plugin_Check\Traits\Stable_Check;
18+
use WordPressdotorg\Plugin_Directory\Readme\Parser as DotorgParser;
1819

1920
/**
2021
* Check for trademarks.
@@ -265,7 +266,7 @@ private function check_for_name_in_readme( Check_Result $result, array $files )
265266

266267
$readme_file = reset( $readme );
267268

268-
$parser = new Parser( $readme_file );
269+
$parser = class_exists( DotorgParser::class ) ? new DotorgParser( $readme_file ) : new PCPParser( $readme_file );
269270

270271
try {
271272
$this->validate_name_has_no_trademarks( $parser->name );

includes/Plugin_Context.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
namespace WordPress\Plugin_Check;
99

1010
use Exception;
11-
use WordPress\Plugin_Check\Lib\Readme\Parser;
11+
use WordPress\Plugin_Check\Lib\Readme\Parser as PCPParser;
1212
use WordPress\Plugin_Check\Traits\Find_Readme;
13+
use WordPressdotorg\Plugin_Directory\Readme\Parser as DotorgParser;
1314
use function WP_CLI\Utils\normalize_path;
1415

1516
/**
@@ -200,7 +201,7 @@ public function minimum_supported_wp() {
200201
$readme_files = $this->filter_files_for_readme( $readme_files, $this->path() );
201202
$readme_file = reset( $readme_files );
202203
if ( $readme_file ) {
203-
$parser = new Parser( $readme_file );
204+
$parser = class_exists( DotorgParser::class ) ? new DotorgParser( $readme_file ) : new PCPParser( $readme_file );
204205

205206
if ( ! empty( $parser->requires ) ) {
206207
$this->minimum_supported_wp = $parser->requires;

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ parameters:
1212
- tests/phpstan/stubs/wp-cli.php
1313
- tests/phpstan/stubs/exitexception.php
1414
- tests/phpstan/stubs/formatter.php
15+
- tests/phpstan/stubs/readme-parser.php
1516
dynamicConstantNames:
1617
- WP_PLUGIN_CHECK_PLUGIN_DIR_URL
1718
treatPhpDocTypesAsCertain: false
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace WordPressdotorg\Plugin_Directory\Readme;
4+
5+
class Parser {
6+
/**
7+
* @var string
8+
*/
9+
public $name = '';
10+
/**
11+
* @var array
12+
*/
13+
public $tags = array();
14+
/**
15+
* @var string
16+
*/
17+
public $requires = '';
18+
/**
19+
* @var string
20+
*/
21+
public $tested = '';
22+
/**
23+
* @var string
24+
*/
25+
public $requires_php = '';
26+
/**
27+
* @var array
28+
*/
29+
public $contributors = array();
30+
/**
31+
* @var string
32+
*/
33+
public $stable_tag = '';
34+
/**
35+
* @var string
36+
*/
37+
public $donate_link = '';
38+
/**
39+
* @var string
40+
*/
41+
public $short_description = '';
42+
/**
43+
* @var string
44+
*/
45+
public $license = '';
46+
/**
47+
* @var string
48+
*/
49+
public $license_uri = '';
50+
/**
51+
* @var array
52+
*/
53+
public $sections = array();
54+
/**
55+
* @var array
56+
*/
57+
public $upgrade_notice = array();
58+
/**
59+
* @var array
60+
*/
61+
public $screenshots = array();
62+
/**
63+
* @var array
64+
*/
65+
public $faq = array();
66+
/**
67+
* Warning flags which indicate specific parsing failures have occurred.
68+
*
69+
* @var array
70+
*/
71+
public $warnings = array();
72+
/**
73+
* These are the readme sections that we expect.
74+
*
75+
* @var array
76+
*/
77+
public $expected_sections = array('description', 'installation', 'faq', 'screenshots', 'changelog', 'upgrade_notice', 'other_notes');
78+
/**
79+
* We alias these sections, from => to
80+
*
81+
* @var array
82+
*/
83+
public $alias_sections = array('frequently_asked_questions' => 'faq', 'change_log' => 'changelog', 'screenshot' => 'screenshots');
84+
/**
85+
* These are the valid header mappings for the header.
86+
*
87+
* @var array
88+
*/
89+
public $valid_headers = array('tested' => 'tested', 'tested up to' => 'tested', 'requires' => 'requires', 'requires at least' => 'requires', 'requires php' => 'requires_php', 'tags' => 'tags', 'contributors' => 'contributors', 'donate link' => 'donate_link', 'stable tag' => 'stable_tag', 'license' => 'license', 'license uri' => 'license_uri');
90+
/**
91+
* These plugin tags are ignored.
92+
*
93+
* @var array
94+
*/
95+
public $ignore_tags = array('plugin', 'wordpress');
96+
/**
97+
* The maximum field lengths for the readme.
98+
*
99+
* @var array
100+
*/
101+
public $maximum_field_lengths = array('short_description' => 150, 'section' => 2500, 'section-changelog' => 5000, 'section-faq' => 5000);
102+
/**
103+
* The raw contents of the readme file.
104+
*
105+
* @var string
106+
*/
107+
public $raw_contents = '';
108+
109+
public function __construct($string = '') {}
110+
}

0 commit comments

Comments
 (0)