3
* Generic_Sniffs_Strings_UnnecessaryStringConcatSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12
* @link http://pear.php.net/package/PHP_CodeSniffer
16
* Generic_Sniffs_Strings_UnnecessaryStringConcatSniff.
18
* Checks that two strings are not concatenated together; suggests
19
* using one string instead.
22
* @package PHP_CodeSniffer
23
* @author Greg Sherwood <gsherwood@squiz.net>
24
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
25
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
26
* @version Release: 1.5.3
27
* @link http://pear.php.net/package/PHP_CodeSniffer
29
class Generic_Sniffs_Strings_UnnecessaryStringConcatSniff implements PHP_CodeSniffer_Sniff
33
* A list of tokenizers this sniff supports.
37
public $supportedTokenizers = array(
43
* If true, an error will be thrown; otherwise a warning.
51
* Returns an array of tokens this test wants to listen for.
55
public function register()
66
* Processes this sniff, when one of its tokens is encountered.
68
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
69
* @param int $stackPtr The position of the current token
70
* in the stack passed in $tokens.
74
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
76
// Work out which type of file this is for.
77
$tokens = $phpcsFile->getTokens();
78
if ($tokens[$stackPtr]['code'] === T_STRING_CONCAT) {
79
if ($phpcsFile->tokenizerType === 'JS') {
83
if ($phpcsFile->tokenizerType === 'PHP') {
88
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
89
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
90
if ($prev === false || $next === false) {
94
$stringTokens = PHP_CodeSniffer_Tokens::$stringTokens;
95
if (in_array($tokens[$prev]['code'], $stringTokens) === true
96
&& in_array($tokens[$next]['code'], $stringTokens) === true
98
if ($tokens[$prev]['content'][0] === $tokens[$next]['content'][0]) {
99
// Before we throw an error for PHP, allow strings to be
100
// combined if they would have < and ? next to each other because
101
// this trick is sometimes required in PHP strings.
102
if ($phpcsFile->tokenizerType === 'PHP') {
103
$prevChar = substr($tokens[$prev]['content'], -2, 1);
104
$nextChar = $tokens[$next]['content'][1];
105
$combined = $prevChar.$nextChar;
106
if ($combined === '?'.'>' || $combined === '<'.'?') {
111
$error = 'String concat is not required here; use a single string instead';
112
if ($this->error === true) {
113
$phpcsFile->addError($error, $stackPtr, 'Found');
115
$phpcsFile->addWarning($error, $stackPtr, 'Found');