annotate vendor/drupal/coder/coder_sniffer/DrupalPractice/Sniffs/FunctionCalls/TCheckPlainSniff.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@0 3 * Drupal_Sniffs_Semantics_FunctionTSniff
Chris@0 4 *
Chris@0 5 * @category PHP
Chris@0 6 * @package PHP_CodeSniffer
Chris@0 7 * @link http://pear.php.net/package/PHP_CodeSniffer
Chris@0 8 */
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Check that "@" and "%" placeholders in t()/watchdog() are not escaped twice
Chris@0 12 * with check_plain().
Chris@0 13 *
Chris@0 14 * @category PHP
Chris@0 15 * @package PHP_CodeSniffer
Chris@0 16 * @link http://pear.php.net/package/PHP_CodeSniffer
Chris@0 17 */
Chris@0 18 class DrupalPractice_Sniffs_FunctionCalls_TCheckPlainSniff extends Drupal_Sniffs_Semantics_FunctionCall
Chris@0 19 {
Chris@0 20
Chris@0 21
Chris@0 22 /**
Chris@0 23 * Returns an array of function names this test wants to listen for.
Chris@0 24 *
Chris@0 25 * @return array
Chris@0 26 */
Chris@0 27 public function registerFunctionNames()
Chris@0 28 {
Chris@0 29 return array(
Chris@0 30 't',
Chris@0 31 'watchdog',
Chris@0 32 );
Chris@0 33
Chris@0 34 }//end registerFunctionNames()
Chris@0 35
Chris@0 36
Chris@0 37 /**
Chris@0 38 * Processes this function call.
Chris@0 39 *
Chris@0 40 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
Chris@0 41 * @param int $stackPtr The position of the function call in
Chris@0 42 * the stack.
Chris@0 43 * @param int $openBracket The position of the opening
Chris@0 44 * parenthesis in the stack.
Chris@0 45 * @param int $closeBracket The position of the closing
Chris@0 46 * parenthesis in the stack.
Chris@0 47 *
Chris@0 48 * @return void
Chris@0 49 */
Chris@0 50 public function processFunctionCall(
Chris@0 51 PHP_CodeSniffer_File $phpcsFile,
Chris@0 52 $stackPtr,
Chris@0 53 $openBracket,
Chris@0 54 $closeBracket
Chris@0 55 ) {
Chris@0 56 $tokens = $phpcsFile->getTokens();
Chris@0 57 if ($tokens[$stackPtr]['content'] === 't') {
Chris@0 58 $argument = $this->getArgument(2);
Chris@0 59 } else {
Chris@0 60 // For watchdog() the placeholders are in the third argument.
Chris@0 61 $argument = $this->getArgument(3);
Chris@0 62 }
Chris@0 63
Chris@0 64 if ($argument === false) {
Chris@0 65 return;
Chris@0 66 }
Chris@0 67
Chris@0 68 if ($tokens[$argument['start']]['code'] !== T_ARRAY) {
Chris@0 69 return;
Chris@0 70 }
Chris@0 71
Chris@0 72 $checkPlain = $argument['start'];
Chris@0 73 while (($checkPlain = $phpcsFile->findNext(T_STRING, ($checkPlain + 1), $tokens[$argument['start']]['parenthesis_closer'])) !== false) {
Chris@0 74 if ($tokens[$checkPlain]['content'] === 'check_plain') {
Chris@0 75 // The check_plain() could be embedded with string concatenation,
Chris@0 76 // which we want to allow.
Chris@0 77 $previous = $phpcsFile->findPrevious(T_WHITESPACE, ($checkPlain - 1), $argument['start'], true);
Chris@0 78 if ($previous === false || $tokens[$previous]['code'] !== T_STRING_CONCAT) {
Chris@0 79 $warning = 'The extra check_plain() is not necessary for placeholders, "@" and "%" will automatically run check_plain()';
Chris@0 80 $phpcsFile->addWarning($warning, $checkPlain, 'CheckPlain');
Chris@0 81 }
Chris@0 82 }
Chris@0 83 }
Chris@0 84
Chris@0 85 }//end processFunctionCall()
Chris@0 86
Chris@0 87
Chris@0 88 }//end class