Unverified Commit f1d92858 authored by Mitch Portier's avatar Mitch Portier Committed by GitHub
Browse files

feat(DescriptionSniff): Add sniff for missing / empty descriptions in info...

feat(DescriptionSniff): Add sniff for missing / empty descriptions in info files (#2969173 by Arkener)
parent 35a81fff
Loading
Loading
Loading
Loading
+89 −0
Original line number Diff line number Diff line
<?php

/**
 * \DrupalPractice\Sniffs\InfoFiles\DescriptionSniff.
 *
 * @category PHP
 * @package  PHP_CodeSniffer
 * @link     http://pear.php.net/package/PHP_CodeSniffer
 */

namespace DrupalPractice\Sniffs\InfoFiles;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;

/**
 * Checks if the *.info.yml file contains a description.
 *
 * @category PHP
 * @package  PHP_CodeSniffer
 * @link     http://pear.php.net/package/PHP_CodeSniffer
 */
class DescriptionSniff implements Sniff
{


    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array<int|string>
     */
    public function register()
    {
        return [T_INLINE_HTML];

    }//end register()


    /**
     * Processes this test, when one of its tokens is encountered.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being processed.
     * @param int                         $stackPtr  The position of the current token
     *                                               in the stack passed in $tokens.
     *
     * @return int
     */
    public function process(File $phpcsFile, $stackPtr)
    {
        $filename      = $phpcsFile->getFilename();
        $fileExtension = strtolower(substr($filename, -9));
        if ($fileExtension !== '.info.yml') {
            return ($phpcsFile->numTokens + 1);
        }

        // Exclude config files which might contain the info.yml extension.
        $filenameWithoutExtension = substr($filename, 0, -9);
        if (strpos($filenameWithoutExtension, '.') !== false) {
            return ($phpcsFile->numTokens + 1);
        }

        try {
            $info = Yaml::parseFile($phpcsFile->getFilename());
        } catch (ParseException $e) {
            // If the YAML is invalid we ignore this file.
            return ($phpcsFile->numTokens + 1);
        }

        // Check if the type key is set, to verify if we're inside a project info.yml file.
        if (isset($info['type']) === false) {
            return ($phpcsFile->numTokens + 1);
        }

        if (isset($info['description']) === false) {
            $warning = '"Description" property is missing in the info.yml file';
            $phpcsFile->addWarning($warning, $stackPtr, 'Missing');
        } else if ($info['description'] === '') {
            $warning = '"Description" should not be empty';
            $phpcsFile->addWarning($warning, $stackPtr, 'Empty');
        }

        return ($phpcsFile->numTokens + 1);

    }//end process()


}//end class
+69 −0
Original line number Diff line number Diff line
<?php

namespace DrupalPractice\Test\InfoFiles;

use Drupal\Test\CoderSniffUnitTest;

class DescriptionUnitTest extends CoderSniffUnitTest
{


    /**
     * Returns the lines where errors should occur.
     *
     * The key of the array should represent the line number and the value
     * should represent the number of errors that should occur on that line.
     *
     * @param string $testFile The name of the file being tested.
     *
     * @return array<int, int>
     */
    protected function getErrorList(string $testFile): array
    {
        return [];

    }//end getErrorList()


    /**
     * Returns the lines where warnings should occur.
     *
     * The key of the array should represent the line number and the value
     * should represent the number of warnings that should occur on that line.
     *
     * @param string $testFile The name of the file being tested.
     *
     * @return array<int, int>
     */
    protected function getWarningList(string $testFile): array
    {
        switch ($testFile) {
        case 'description_missing.info.yml':
            return [1 => 1];
        case 'description_empty.info.yml':
            return [1 => 1];
        }

        return [];

    }//end getWarningList()


    /**
     * Returns a list of test files that should be checked.
     *
     * @param string $testFileBase The base path that the unit tests files will have.
     *
     * @return array<string>
     */
    protected function getTestFiles($testFileBase): array
    {
        return [
            __DIR__.'/description_empty.info.yml',
            __DIR__.'/description_missing.info.yml',
        ];

    }//end getTestFiles()


}//end class
+6 −0
Original line number Diff line number Diff line
name: Description
description: ''
type: module
package: Coder
version: VERSION
core_version_requirement: ^9
 No newline at end of file
+5 −0
Original line number Diff line number Diff line
name: Description
type: module
package: Coder
version: VERSION
core_version_requirement: ^9
 No newline at end of file