comparison vendor/drupal/coder/coder_sniffer/DrupalPractice/Sniffs/FunctionCalls/TCheckPlainSniff.php @ 0:4c8ae668cc8c

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