comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Semantics/FunctionTSniff.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php 1 <?php
2 /** 2 /**
3 * Drupal_Sniffs_Semantics_FunctionTSniff 3 * \Drupal\Sniffs\Semantics\FunctionTSniff
4 * 4 *
5 * @category PHP 5 * @category PHP
6 * @package PHP_CodeSniffer 6 * @package PHP_CodeSniffer
7 * @link http://pear.php.net/package/PHP_CodeSniffer 7 * @link http://pear.php.net/package/PHP_CodeSniffer
8 */ 8 */
9
10 namespace Drupal\Sniffs\Semantics;
11
12 use PHP_CodeSniffer\Files\File;
13 use PHP_CodeSniffer\Util\Tokens;
9 14
10 /** 15 /**
11 * Check the usage of the t() function to not escape translateable strings with back 16 * Check the usage of the t() function to not escape translateable strings with back
12 * slashes. Also checks that the first argument does not use string concatenation. 17 * slashes. Also checks that the first argument does not use string concatenation.
13 * 18 *
14 * @category PHP 19 * @category PHP
15 * @package PHP_CodeSniffer 20 * @package PHP_CodeSniffer
16 * @link http://pear.php.net/package/PHP_CodeSniffer 21 * @link http://pear.php.net/package/PHP_CodeSniffer
17 */ 22 */
18 class Drupal_Sniffs_Semantics_FunctionTSniff extends Drupal_Sniffs_Semantics_FunctionCall 23 class FunctionTSniff extends FunctionCall
19 { 24 {
20 25
21 /** 26 /**
22 * We also want to catch $this->t() calls in Drupal 8. 27 * We also want to catch $this->t() calls in Drupal 8.
23 * 28 *
43 48
44 49
45 /** 50 /**
46 * Processes this function call. 51 * Processes this function call.
47 * 52 *
48 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 53 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
49 * @param int $stackPtr The position of the function call in 54 * @param int $stackPtr The position of the function call in
50 * the stack. 55 * the stack.
51 * @param int $openBracket The position of the opening 56 * @param int $openBracket The position of the opening
52 * parenthesis in the stack. 57 * parenthesis in the stack.
53 * @param int $closeBracket The position of the closing 58 * @param int $closeBracket The position of the closing
54 * parenthesis in the stack. 59 * parenthesis in the stack.
55 * 60 *
56 * @return void 61 * @return void
57 */ 62 */
58 public function processFunctionCall( 63 public function processFunctionCall(
59 PHP_CodeSniffer_File $phpcsFile, 64 File $phpcsFile,
60 $stackPtr, 65 $stackPtr,
61 $openBracket, 66 $openBracket,
62 $closeBracket 67 $closeBracket
63 ) { 68 ) {
64 $tokens = $phpcsFile->getTokens(); 69 $tokens = $phpcsFile->getTokens();
82 $warning = 'Do not pass empty strings to t()'; 87 $warning = 'Do not pass empty strings to t()';
83 $phpcsFile->addWarning($warning, $argument['start'], 'EmptyString'); 88 $phpcsFile->addWarning($warning, $argument['start'], 'EmptyString');
84 return; 89 return;
85 } 90 }
86 91
87 $concatAfter = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($closeBracket + 1), null, true, null, true); 92 $concatAfter = $phpcsFile->findNext(Tokens::$emptyTokens, ($closeBracket + 1), null, true, null, true);
88 if ($concatAfter !== false && $tokens[$concatAfter]['code'] === T_STRING_CONCAT) { 93 if ($concatAfter !== false && $tokens[$concatAfter]['code'] === T_STRING_CONCAT) {
89 $stringAfter = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($concatAfter + 1), null, true, null, true); 94 $stringAfter = $phpcsFile->findNext(Tokens::$emptyTokens, ($concatAfter + 1), null, true, null, true);
90 if ($stringAfter !== false 95 if ($stringAfter !== false
91 && $tokens[$stringAfter]['code'] === T_CONSTANT_ENCAPSED_STRING 96 && $tokens[$stringAfter]['code'] === T_CONSTANT_ENCAPSED_STRING
92 && $this->checkConcatString($tokens[$stringAfter]['content']) === false 97 && $this->checkConcatString($tokens[$stringAfter]['content']) === false
93 ) { 98 ) {
94 $warning = 'Do not concatenate strings to translatable strings, they should be part of the t() argument and you should use placeholders'; 99 $warning = 'Do not concatenate strings to translatable strings, they should be part of the t() argument and you should use placeholders';
148 153
149 if ($string === '') { 154 if ($string === '') {
150 return true; 155 return true;
151 } 156 }
152 157
153 if (in_array($string, ['(', ')', '[', ']', '-', '<', '>', '«', '»', '\n'], true)) { 158 $allowed_items = array(
154 return true; 159 '(',
160 ')',
161 '[',
162 ']',
163 '-',
164 '<',
165 '>',
166 '«',
167 '»',
168 '\n',
169 );
170 foreach ($allowed_items as $item) {
171 if ($item === $string) {
172 return true;
173 }
155 } 174 }
156 175
157 return false; 176 return false;
158 177
159 }//end checkConcatString() 178 }//end checkConcatString()