8 use PHP_CodeSniffer\Sniffs\Sniff;
9 use PHP_CodeSniffer\Files\File;
26 public function register()
36 public function process(File $phpcsFile, $stackPtr)
38 $tokens = $phpcsFile->getTokens();
41 $previousOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1));
42 if ($previousOpenTag !==
false) {
47 $currentLineContent =
'';
50 for (; $tokenCount < $phpcsFile->numTokens; $tokenCount++) {
51 if (
$tokens[$tokenCount][
'line'] === $currentLine) {
52 $currentLineContent .=
$tokens[$tokenCount][
'content'];
54 $this->checkIfFirstArgumentConstant($phpcsFile, ($tokenCount - 1), $currentLineContent);
55 $currentLineContent =
$tokens[$tokenCount][
'content'];
60 $this->checkIfFirstArgumentConstant($phpcsFile, ($tokenCount - 1), $currentLineContent);
71 private function checkIfFirstArgumentConstant(
76 $previousLineRegexp =
'/(__|Phrase)\($/im';
77 $currentLineRegexp =
'/(__|Phrase)\(.+\)/';
78 $currentLineMatch = preg_match($currentLineRegexp, $lineContent) !== 0;
79 $previousLineMatch = preg_match($previousLineRegexp, $this->previousLineContent) !== 0;
80 $this->previousLineContent = $lineContent;
81 $error =
'Constants are not allowed as the first argument of translation function, use string literal instead';
82 $constantRegexp =
'[^\$\'"]+::[A-Z_0-9]+.*';
83 if ($currentLineMatch) {
84 $variableRegexp =
"/(__|Phrase)\({$constantRegexp}\)/";
85 if (preg_match($variableRegexp, $lineContent) !== 0) {
86 $phpcsFile->addError($error, $stackPtr,
'VariableTranslation');
88 }
elseif ($previousLineMatch) {
89 $variableRegexp =
"/^{$constantRegexp}/";
90 if (preg_match($variableRegexp, $lineContent) !== 0) {
91 $phpcsFile->addError($error, $stackPtr,
'VariableTranslation');
elseif(isset( $params[ 'redirect_parent']))
process(File $phpcsFile, $stackPtr)