Skip to content

Commit

Permalink
Merge pull request #414 from ernilambar/402-detect-missing-headers
Browse files Browse the repository at this point in the history
Detect missing readme headers
  • Loading branch information
davidperezgar authored Jun 30, 2024
2 parents 12666c6 + 367d847 commit d017f6a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 15 deletions.
84 changes: 71 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 the readme file for default text.
$this->check_default_text( $result, $readme_file, $parser );

Expand Down Expand Up @@ -119,6 +122,47 @@ private function check_name( Check_Result $result, string $readme_file, Parser $
}
}

/**
* Checks the readme file for missing headers.
*
* @since 1.0.2
*
* @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 );

$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',
),
);

$parser_warnings = is_array( $parser->warnings ) ? $parser->warnings : array();

foreach ( $fields as $field_key => $field ) {
if ( empty( $parser->{$field_key} ) && ! in_array( $field['ignore_key'], $ignored_warnings, true ) && ! isset( $parser_warnings[ $field['ignore_key'] ] ) ) {
$this->add_result_warning_for_file(
$result,
sprintf(
/* translators: %s: plugin header tag */
__( 'The "%s" field is missing.', 'plugin-check' ),
$field['label']
),
'missing_readme_header',
$readme_file
);
}
}
}

/**
* Checks the readme file for default text.
*
Expand Down Expand Up @@ -333,10 +377,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 @@ -387,15 +427,7 @@ private function check_for_warnings( Check_Result $result, string $readme_file,
),
);

/**
* Filter the list of ignored readme parser warnings.
*
* @since 1.0.0
*
* @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 );

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

Expand Down Expand Up @@ -446,4 +478,30 @@ private function get_wordpress_stable_version() {

return $version;
}

/**
* Returns ignored warnings.
*
* @since 1.0.2
*
* @param Parser $parser The Parser object.
* @return array Ignored warnings.
*/
private function get_ignored_warnings( Parser $parser ) {
$ignored_warnings = array(
'contributor_ignored',
);

/**
* Filter the list of ignored readme parser warnings.
*
* @since 1.0.2
*
* @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 );

return $ignored_warnings;
}
}
11 changes: 9 additions & 2 deletions tests/phpunit/tests/Checker/Checks/Plugin_Readme_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,21 @@ public function test_run_with_errors_empty_name() {

$this->assertNotEmpty( $errors );
$this->assertArrayHasKey( 'readme.txt', $errors );
$this->assertEmpty( $warnings );
$this->assertEquals( 0, $check_result->get_warning_count() );
// After https://github.com/WordPress/plugin-check/issues/402 there will be warning of missing "Tested up to" field.
$this->assertNotEmpty( $warnings );
$this->assertEquals( 1, $check_result->get_warning_count() );

// Check for empty name error.
$this->assertArrayHasKey( 0, $errors['readme.txt'] );
$this->assertArrayHasKey( 0, $errors['readme.txt'][0] );
$this->assertArrayHasKey( 'code', $errors['readme.txt'][0][0][0] );
$this->assertEquals( 'empty_plugin_name', $errors['readme.txt'][0][0][0]['code'] );

// Tested up to 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( 'missing_readme_header', $warnings['readme.txt'][0][0][0]['code'] );
}

public function test_run_with_errors_default_text() {
Expand Down

0 comments on commit d017f6a

Please sign in to comment.