DescriptionTSniff.php in Coder 8.3
File
coder_sniffer/DrupalPractice/Sniffs/General/DescriptionTSniff.php
View source
<?php
namespace DrupalPractice\Sniffs\General;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
class DescriptionTSniff implements Sniff {
public function register() {
return [
T_CONSTANT_ENCAPSED_STRING,
];
}
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile
->getTokens();
if ($tokens[$stackPtr]['content'] !== '"#description"' && $tokens[$stackPtr]['content'] !== "'#description'") {
return;
}
$statementEnd = $phpcsFile
->findNext(T_SEMICOLON, $stackPtr + 1);
$arrayString = $phpcsFile
->getTokensAsString($stackPtr + 1, $statementEnd - $stackPtr);
$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');
}
}
}
Classes
Name |
Description |
DescriptionTSniff |
Checks that string values for #description in render arrays are translated. |