Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DescriptionSniff.php

class DescriptionSniff

Checks if the *.info.yml file contains a description.

@category PHP @package PHP_CodeSniffer @link http://pear.php.net/package/PHP_CodeSniffer

Hierarchy

  • class \DrupalPractice\Sniffs\InfoFiles\DescriptionSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of DescriptionSniff

File

vendor/drupal/coder/coder_sniffer/DrupalPractice/Sniffs/InfoFiles/DescriptionSniff.php, line 25

Namespace

DrupalPractice\Sniffs\InfoFiles
View source
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()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
DescriptionSniff::process public function Processes this test, when one of its tokens is encountered. Overrides Sniff::process
DescriptionSniff::register public function Returns an array of tokens this test wants to listen for. Overrides Sniff::register
RSS feed
Powered by Drupal