You are here

public function DescriptionTSniff::process in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/DrupalPractice/Sniffs/General/DescriptionTSniff.php \DrupalPractice\Sniffs\General\DescriptionTSniff::process()
  2. 8.3.x coder_sniffer/DrupalPractice/Sniffs/General/DescriptionTSniff.php \DrupalPractice\Sniffs\General\DescriptionTSniff::process()

Processes this test, when one of its tokens is encountered.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

int $stackPtr The position of the function: name in the stack.

Return value

void

File

coder_sniffer/DrupalPractice/Sniffs/General/DescriptionTSniff.php, line 47

Class

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

Namespace

DrupalPractice\Sniffs\General

Code

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');
  }
}