Chris@0: Chris@17: * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) Chris@17: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@0: * @link http://pear.php.net/package/PHP_CodeSniffer Chris@0: */ Chris@0: Chris@17: namespace Drupal\Sniffs\Strings; Chris@17: Chris@17: use Drupal\Sniffs\Files\LineLengthSniff; Chris@17: use PHP_CodeSniffer\Files\File; Chris@17: use PHP_CodeSniffer\Standards\Generic\Sniffs\Strings\UnnecessaryStringConcatSniff as GenericUnnecessaryStringConcatSniff; Chris@17: use PHP_CodeSniffer\Util\Tokens; Chris@17: Chris@0: /** Chris@17: * Checks that two strings are not concatenated together; suggests using one string instead. Chris@0: * Chris@0: * @category PHP Chris@0: * @package PHP_CodeSniffer Chris@0: * @author Greg Sherwood Chris@17: * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) Chris@17: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@0: * @link http://pear.php.net/package/PHP_CodeSniffer Chris@0: */ Chris@17: class UnnecessaryStringConcatSniff extends GenericUnnecessaryStringConcatSniff Chris@0: { Chris@0: Chris@0: Chris@0: /** Chris@0: * Processes this sniff, when one of its tokens is encountered. Chris@0: * Chris@17: * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. Chris@17: * @param int $stackPtr The position of the current token Chris@17: * in the stack passed in $tokens. Chris@0: * Chris@0: * @return void Chris@0: */ Chris@17: public function process(File $phpcsFile, $stackPtr) Chris@0: { Chris@0: // Work out which type of file this is for. Chris@0: $tokens = $phpcsFile->getTokens(); Chris@0: if ($tokens[$stackPtr]['code'] === T_STRING_CONCAT) { Chris@0: if ($phpcsFile->tokenizerType === 'JS') { Chris@0: return; Chris@0: } Chris@0: } else { Chris@0: if ($phpcsFile->tokenizerType === 'PHP') { Chris@0: return; Chris@0: } Chris@0: } Chris@0: Chris@0: $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); Chris@0: $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); Chris@0: if ($prev === false || $next === false) { Chris@0: return; Chris@0: } Chris@0: Chris@17: $stringTokens = Tokens::$stringTokens; Chris@0: if (in_array($tokens[$prev]['code'], $stringTokens) === true Chris@0: && in_array($tokens[$next]['code'], $stringTokens) === true Chris@0: ) { Chris@0: if ($tokens[$prev]['content'][0] === $tokens[$next]['content'][0]) { Chris@0: // Before we throw an error for PHP, allow strings to be Chris@0: // combined if they would have < and ? next to each other because Chris@0: // this trick is sometimes required in PHP strings. Chris@0: if ($phpcsFile->tokenizerType === 'PHP') { Chris@0: $prevChar = substr($tokens[$prev]['content'], -2, 1); Chris@0: $nextChar = $tokens[$next]['content'][1]; Chris@0: $combined = $prevChar.$nextChar; Chris@0: if ($combined === '?'.'>' || $combined === '<'.'?') { Chris@0: return; Chris@0: } Chris@0: } Chris@0: Chris@0: // Before we throw an error check if the string is longer than Chris@0: // the line length limit. Chris@17: $lineLengthLimitSniff = new LineLengthSniff; Chris@0: Chris@0: $lineLenght = $lineLengthLimitSniff->getLineLength($phpcsFile, $tokens[$prev]['line']); Chris@0: $stringLength = ($lineLenght + strlen($tokens[$next]['content']) - 4); Chris@0: if ($stringLength > $lineLengthLimitSniff->lineLimit) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $error = 'String concat is not required here; use a single string instead'; Chris@0: if ($this->error === true) { Chris@0: $phpcsFile->addError($error, $stackPtr, 'Found'); Chris@0: } else { Chris@0: $phpcsFile->addWarning($error, $stackPtr, 'Found'); Chris@0: } Chris@0: }//end if Chris@0: }//end if Chris@0: Chris@0: }//end process() Chris@0: Chris@0: Chris@0: }//end class