Skip to content

Commit

Permalink
Detect missing readme headers
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Feb 6, 2024
1 parent 1be0e35 commit 71fcdb0
Showing 1 changed file with 68 additions and 13 deletions.
81 changes: 68 additions & 13 deletions includes/Checker/Checks/Plugin_Readme_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ protected function check_files( Check_Result $result, array $files ) {
// Check the readme file for plugin name.
$this->check_name( $result, $readme_file, $parser );

// Check the readme file for missing headers.
$this->check_headers( $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 default text.
$this->check_default_text( $result, $readme_file, $parser );

Expand Down Expand Up @@ -120,6 +123,45 @@ private function check_name( Check_Result $result, string $readme_file, Parser $
}
}

/**
* Checks the readme file for missing headers.
*
* @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_headers( Check_Result $result, string $readme_file, Parser $parser ) {
$ignored_warnings = $this->get_ignored_warnings( $parser );

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#L135-L136

Added lines #L135 - L136 were not covered by tests

$fields = array(
'tested' => array(
'label' => __( 'Tested up to', 'plugin-check' ),
'ignore_key' => 'tested_header_ignored',
),
'contributors' => array(
'label' => __( 'Contributors', 'plugin-check' ),
'ignore_key' => 'contributor_ignored',
),
);

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

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L138-L147

Added lines #L138 - L147 were not covered by tests

foreach ( $fields as $field_key => $field ) {
if ( empty( $parser->{$field_key} ) && ! in_array( $field['ignore_key'], $ignored_warnings, true ) ) {
$this->add_result_warning_for_file(
$result,
sprintf(

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

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L149-L153

Added lines #L149 - L153 were not covered by tests
/* translators: %s: plugin header tag */
__( 'The "%s" field is missing.', 'plugin-check' ),
$field['label']
),
'missing_readme_header',
$readme_file
);

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

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L155-L160

Added lines #L155 - L160 were not covered by tests
}
}
}

/**
* Checks the readme file for default text.
*
Expand Down Expand Up @@ -253,10 +295,6 @@ private function check_for_warnings( Check_Result $result, string $readme_file,

$latest_wordpress_version = (float) $this->get_wordpress_stable_version();

$ignored_warnings = array(
'contributor_ignored',
);

$messages = array(
'contributor_ignored' => sprintf(
/* translators: %s: plugin header tag */
Expand Down Expand Up @@ -307,15 +345,7 @@ private function check_for_warnings( Check_Result $result, string $readme_file,
),
);

/**
* 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 );
$ignored_warnings = $this->get_ignored_warnings( $parser );

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

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L348

Added line #L348 was not covered by tests

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

Expand Down Expand Up @@ -366,4 +396,29 @@ private function get_wordpress_stable_version() {

return $version;
}
/**
* Returns ignored warnings.
*
* @since n.e.x.t
*
* @param Parser $parser The Parser object.
* @return array Ignore warnings.
*/
private function get_ignored_warnings( Parser $parser ) {
$ignored_warnings = array(
'contributor_ignored',
);

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

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L407-L410

Added lines #L407 - L410 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 420 in includes/Checker/Checks/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L420

Added line #L420 was not covered by tests

return $ignored_warnings;

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

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Readme_Check.php#L422

Added line #L422 was not covered by tests
}
}

0 comments on commit 71fcdb0

Please sign in to comment.