View source
<?php
namespace Drupal\Sniffs\Semantics;
use Drupal\Sniffs\Semantics\FunctionCall;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
class FunctionTSniff extends FunctionCall {
protected $includeMethodCalls = true;
public function registerFunctionNames() {
return array(
't',
'TranslatableMarkup',
'TranslationWrapper',
);
}
public function processFunctionCall(File $phpcsFile, $stackPtr, $openBracket, $closeBracket) {
$tokens = $phpcsFile
->getTokens();
$argument = $this
->getArgument(1);
if ($argument === false) {
$error = 'Empty calls to t() are not allowed';
$phpcsFile
->addError($error, $stackPtr, 'EmptyT');
return;
}
if ($tokens[$argument['start']]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
$warning = 'Only string literals should be passed to t() where possible';
$phpcsFile
->addWarning($warning, $argument['start'], 'NotLiteralString');
return;
}
$string = $tokens[$argument['start']]['content'];
if ($string === '""' || $string === "''") {
$warning = 'Do not pass empty strings to t()';
$phpcsFile
->addWarning($warning, $argument['start'], 'EmptyString');
return;
}
$concatAfter = $phpcsFile
->findNext(Tokens::$emptyTokens, $closeBracket + 1, null, true, null, true);
if ($concatAfter !== false && $tokens[$concatAfter]['code'] === T_STRING_CONCAT) {
$stringAfter = $phpcsFile
->findNext(Tokens::$emptyTokens, $concatAfter + 1, null, true, null, true);
if ($stringAfter !== false && $tokens[$stringAfter]['code'] === T_CONSTANT_ENCAPSED_STRING && $this
->checkConcatString($tokens[$stringAfter]['content']) === false) {
$warning = 'Do not concatenate strings to translatable strings, they should be part of the t() argument and you should use placeholders';
$phpcsFile
->addWarning($warning, $stringAfter, 'ConcatString');
}
}
$lastChar = substr($string, -1);
if ($lastChar === '"' || $lastChar === "'") {
$message = substr($string, 1, -1);
if ($message !== trim($message)) {
$warning = 'Translatable strings must not begin or end with white spaces, use placeholders with t() for variables';
$phpcsFile
->addWarning($warning, $argument['start'], 'WhiteSpace');
}
}
$concatFound = $phpcsFile
->findNext(T_STRING_CONCAT, $argument['start'], $argument['end']);
if ($concatFound !== false) {
$error = 'Concatenating translatable strings is not allowed, use placeholders instead and only one string literal';
$phpcsFile
->addError($error, $concatFound, 'Concat');
}
if ($string[0] === "'" && strpos($string, "\\'") !== false && strpos($string, '"') === false) {
$warn = 'Avoid backslash escaping in translatable strings when possible, use "" quotes instead';
$phpcsFile
->addWarning($warn, $argument['start'], 'BackslashSingleQuote');
return;
}
if ($string[0] === '"' && strpos($string, '\\"') !== false && strpos($string, "'") === false) {
$warn = "Avoid backslash escaping in translatable strings when possible, use '' quotes instead";
$phpcsFile
->addWarning($warn, $argument['start'], 'BackslashDoubleQuote');
}
}
protected function checkConcatString($string) {
$string = trim($string, '"\'');
$string = trim(strip_tags($string));
if ($string === '') {
return true;
}
$allowed_items = array(
'(',
')',
'[',
']',
'-',
'<',
'>',
'«',
'»',
'\\n',
);
foreach ($allowed_items as $item) {
if ($item === $string) {
return true;
}
}
return false;
}
}