3
* Squiz_Sniffs_Strings_EchoedStringsSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @author Marc McIntyre <mmcintyre@squiz.net>
11
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
12
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13
* @link http://pear.php.net/package/PHP_CodeSniffer
17
* Squiz_Sniffs_Strings_EchoedStringsSniff.
19
* Makes sure that any strings that are "echoed" are not enclosed in brackets
20
* like a function call.
23
* @package PHP_CodeSniffer
24
* @author Greg Sherwood <gsherwood@squiz.net>
25
* @author Marc McIntyre <mmcintyre@squiz.net>
26
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
27
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
28
* @version Release: 1.5.0RC2
29
* @link http://pear.php.net/package/PHP_CodeSniffer
31
class Squiz_Sniffs_Strings_EchoedStringsSniff implements PHP_CodeSniffer_Sniff
36
* Returns an array of tokens this test wants to listen for.
40
public function register()
48
* Processes this test, when one of its tokens is encountered.
50
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
51
* @param int $stackPtr The position of the current token in the
52
* stack passed in $tokens.
56
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
58
$tokens = $phpcsFile->getTokens();
60
$firstContent = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
61
// If the first non-whitespace token is not an opening parenthesis, then we are not concerned.
62
if ($tokens[$firstContent]['code'] !== T_OPEN_PARENTHESIS) {
66
$endOfStatement = $phpcsFile->findNext(array(T_SEMICOLON), $stackPtr, null, false);
68
// If the token before the semi-colon is not a closing parenthesis, then we are not concerned.
69
if ($tokens[($endOfStatement - 1)]['code'] !== T_CLOSE_PARENTHESIS) {
73
if (($phpcsFile->findNext(PHP_CodeSniffer_Tokens::$operators, $stackPtr, $endOfStatement, false)) === false) {
74
// There are no arithmetic operators in this.
75
$error = 'Echoed strings should not be bracketed';
76
$phpcsFile->addError($error, $stackPtr, 'HasBracket');