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