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

Breadcrumb

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

class DescriptionTSniff

Checks that string values for #description in render arrays are translated.

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

Hierarchy

  • class \DrupalPractice\Sniffs\General\DescriptionTSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of DescriptionTSniff

File

vendor/drupal/coder/coder_sniffer/DrupalPractice/Sniffs/General/DescriptionTSniff.php, line 22

Namespace

DrupalPractice\Sniffs\General
View source
class DescriptionTSniff implements Sniff {
    
    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array<int|string>
     */
    public function register() {
        return [
            T_CONSTANT_ENCAPSED_STRING,
        ];
    }
    
    //end register()
    
    /**
     * Processes this test, when one of its tokens is encountered.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
     * @param int                         $stackPtr  The position of the function
     *                                               name in the stack.
     *
     * @return void
     */
    public function process(File $phpcsFile, $stackPtr) {
        // Look for the string "#description".
        $tokens = $phpcsFile->getTokens();
        if ($tokens[$stackPtr]['content'] !== '"#description"' && $tokens[$stackPtr]['content'] !== "'#description'") {
            return;
        }
        // Look for an array pattern that starts to define #description values.
        $statementEnd = $phpcsFile->findNext(T_SEMICOLON, $stackPtr + 1);
        $arrayString = $phpcsFile->getTokensAsString($stackPtr + 1, $statementEnd - $stackPtr);
        // Cut out all the white space.
        $arrayString = preg_replace('/\\s+/', '', $arrayString);
        if (strpos($arrayString, '=>"') !== 0 && strpos($arrayString, ']="') !== 0 && strpos($arrayString, "=>'") !== 0 && strpos($arrayString, "]='") !== 0) {
            return;
        }
        $stringToken = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $stackPtr + 1);
        $content = strip_tags($tokens[$stringToken]['content']);
        if (strlen($content) > 5) {
            $warning = '#description values usually have to run through t() for translation';
            $phpcsFile->addWarning($warning, $stringToken, 'DescriptionT');
        }
    }
    
    //end process()

}

Members

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