Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@17
|
3 * \Drupal\Sniffs\Strings\UnnecessaryStringConcatSniff.
|
Chris@0
|
4 *
|
Chris@0
|
5 * @category PHP
|
Chris@0
|
6 * @package PHP_CodeSniffer
|
Chris@0
|
7 * @author Greg Sherwood <gsherwood@squiz.net>
|
Chris@17
|
8 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
|
Chris@17
|
9 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
Chris@0
|
10 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
11 */
|
Chris@0
|
12
|
Chris@17
|
13 namespace Drupal\Sniffs\Strings;
|
Chris@17
|
14
|
Chris@17
|
15 use Drupal\Sniffs\Files\LineLengthSniff;
|
Chris@17
|
16 use PHP_CodeSniffer\Files\File;
|
Chris@17
|
17 use PHP_CodeSniffer\Standards\Generic\Sniffs\Strings\UnnecessaryStringConcatSniff as GenericUnnecessaryStringConcatSniff;
|
Chris@17
|
18 use PHP_CodeSniffer\Util\Tokens;
|
Chris@17
|
19
|
Chris@0
|
20 /**
|
Chris@17
|
21 * Checks that two strings are not concatenated together; suggests using one string instead.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @category PHP
|
Chris@0
|
24 * @package PHP_CodeSniffer
|
Chris@0
|
25 * @author Greg Sherwood <gsherwood@squiz.net>
|
Chris@17
|
26 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
|
Chris@17
|
27 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
Chris@0
|
28 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
29 */
|
Chris@17
|
30 class UnnecessaryStringConcatSniff extends GenericUnnecessaryStringConcatSniff
|
Chris@0
|
31 {
|
Chris@0
|
32
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Processes this sniff, when one of its tokens is encountered.
|
Chris@0
|
36 *
|
Chris@17
|
37 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
|
Chris@17
|
38 * @param int $stackPtr The position of the current token
|
Chris@17
|
39 * in the stack passed in $tokens.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @return void
|
Chris@0
|
42 */
|
Chris@17
|
43 public function process(File $phpcsFile, $stackPtr)
|
Chris@0
|
44 {
|
Chris@0
|
45 // Work out which type of file this is for.
|
Chris@0
|
46 $tokens = $phpcsFile->getTokens();
|
Chris@0
|
47 if ($tokens[$stackPtr]['code'] === T_STRING_CONCAT) {
|
Chris@0
|
48 if ($phpcsFile->tokenizerType === 'JS') {
|
Chris@0
|
49 return;
|
Chris@0
|
50 }
|
Chris@0
|
51 } else {
|
Chris@0
|
52 if ($phpcsFile->tokenizerType === 'PHP') {
|
Chris@0
|
53 return;
|
Chris@0
|
54 }
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
|
Chris@0
|
58 $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
|
Chris@0
|
59 if ($prev === false || $next === false) {
|
Chris@0
|
60 return;
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@17
|
63 $stringTokens = Tokens::$stringTokens;
|
Chris@0
|
64 if (in_array($tokens[$prev]['code'], $stringTokens) === true
|
Chris@0
|
65 && in_array($tokens[$next]['code'], $stringTokens) === true
|
Chris@0
|
66 ) {
|
Chris@0
|
67 if ($tokens[$prev]['content'][0] === $tokens[$next]['content'][0]) {
|
Chris@0
|
68 // Before we throw an error for PHP, allow strings to be
|
Chris@0
|
69 // combined if they would have < and ? next to each other because
|
Chris@0
|
70 // this trick is sometimes required in PHP strings.
|
Chris@0
|
71 if ($phpcsFile->tokenizerType === 'PHP') {
|
Chris@0
|
72 $prevChar = substr($tokens[$prev]['content'], -2, 1);
|
Chris@0
|
73 $nextChar = $tokens[$next]['content'][1];
|
Chris@0
|
74 $combined = $prevChar.$nextChar;
|
Chris@0
|
75 if ($combined === '?'.'>' || $combined === '<'.'?') {
|
Chris@0
|
76 return;
|
Chris@0
|
77 }
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 // Before we throw an error check if the string is longer than
|
Chris@0
|
81 // the line length limit.
|
Chris@17
|
82 $lineLengthLimitSniff = new LineLengthSniff;
|
Chris@0
|
83
|
Chris@0
|
84 $lineLenght = $lineLengthLimitSniff->getLineLength($phpcsFile, $tokens[$prev]['line']);
|
Chris@0
|
85 $stringLength = ($lineLenght + strlen($tokens[$next]['content']) - 4);
|
Chris@0
|
86 if ($stringLength > $lineLengthLimitSniff->lineLimit) {
|
Chris@0
|
87 return;
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 $error = 'String concat is not required here; use a single string instead';
|
Chris@0
|
91 if ($this->error === true) {
|
Chris@0
|
92 $phpcsFile->addError($error, $stackPtr, 'Found');
|
Chris@0
|
93 } else {
|
Chris@0
|
94 $phpcsFile->addWarning($error, $stackPtr, 'Found');
|
Chris@0
|
95 }
|
Chris@0
|
96 }//end if
|
Chris@0
|
97 }//end if
|
Chris@0
|
98
|
Chris@0
|
99 }//end process()
|
Chris@0
|
100
|
Chris@0
|
101
|
Chris@0
|
102 }//end class
|