comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Strings/UnnecessaryStringConcatSniff.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php 1 <?php
2 /** 2 /**
3 * Generic_Sniffs_Strings_UnnecessaryStringConcatSniff. 3 * \Drupal\Sniffs\Strings\UnnecessaryStringConcatSniff.
4 * 4 *
5 * @category PHP 5 * @category PHP
6 * @package PHP_CodeSniffer 6 * @package PHP_CodeSniffer
7 * @author Greg Sherwood <gsherwood@squiz.net> 7 * @author Greg Sherwood <gsherwood@squiz.net>
8 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) 8 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
9 * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence 9 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt 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 10 * @link http://pear.php.net/package/PHP_CodeSniffer
12 */ 11 */
13 12
13 namespace Drupal\Sniffs\Strings;
14
15 use Drupal\Sniffs\Files\LineLengthSniff;
16 use PHP_CodeSniffer\Files\File;
17 use PHP_CodeSniffer\Standards\Generic\Sniffs\Strings\UnnecessaryStringConcatSniff as GenericUnnecessaryStringConcatSniff;
18 use PHP_CodeSniffer\Util\Tokens;
19
14 /** 20 /**
15 * Generic_Sniffs_Strings_UnnecessaryStringConcatSniff. 21 * Checks that two strings are not concatenated together; suggests using one string instead.
16 *
17 * Checks that two strings are not concatenated together; suggests
18 * using one string instead.
19 * 22 *
20 * @category PHP 23 * @category PHP
21 * @package PHP_CodeSniffer 24 * @package PHP_CodeSniffer
22 * @author Greg Sherwood <gsherwood@squiz.net> 25 * @author Greg Sherwood <gsherwood@squiz.net>
23 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) 26 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
24 * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence 27 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25 * @version Release: 1.3.1
26 * @link http://pear.php.net/package/PHP_CodeSniffer 28 * @link http://pear.php.net/package/PHP_CodeSniffer
27 */ 29 */
28 class Drupal_Sniffs_Strings_UnnecessaryStringConcatSniff extends Generic_Sniffs_Strings_UnnecessaryStringConcatSniff 30 class UnnecessaryStringConcatSniff extends GenericUnnecessaryStringConcatSniff
29 { 31 {
30 32
31 33
32 /** 34 /**
33 * Processes this sniff, when one of its tokens is encountered. 35 * Processes this sniff, when one of its tokens is encountered.
34 * 36 *
35 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 37 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
36 * @param int $stackPtr The position of the current token 38 * @param int $stackPtr The position of the current token
37 * in the stack passed in $tokens. 39 * in the stack passed in $tokens.
38 * 40 *
39 * @return void 41 * @return void
40 */ 42 */
41 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 43 public function process(File $phpcsFile, $stackPtr)
42 { 44 {
43 // Work out which type of file this is for. 45 // Work out which type of file this is for.
44 $tokens = $phpcsFile->getTokens(); 46 $tokens = $phpcsFile->getTokens();
45 if ($tokens[$stackPtr]['code'] === T_STRING_CONCAT) { 47 if ($tokens[$stackPtr]['code'] === T_STRING_CONCAT) {
46 if ($phpcsFile->tokenizerType === 'JS') { 48 if ($phpcsFile->tokenizerType === 'JS') {
56 $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); 58 $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
57 if ($prev === false || $next === false) { 59 if ($prev === false || $next === false) {
58 return; 60 return;
59 } 61 }
60 62
61 $stringTokens = PHP_CodeSniffer_Tokens::$stringTokens; 63 $stringTokens = Tokens::$stringTokens;
62 if (in_array($tokens[$prev]['code'], $stringTokens) === true 64 if (in_array($tokens[$prev]['code'], $stringTokens) === true
63 && in_array($tokens[$next]['code'], $stringTokens) === true 65 && in_array($tokens[$next]['code'], $stringTokens) === true
64 ) { 66 ) {
65 if ($tokens[$prev]['content'][0] === $tokens[$next]['content'][0]) { 67 if ($tokens[$prev]['content'][0] === $tokens[$next]['content'][0]) {
66 // Before we throw an error for PHP, allow strings to be 68 // Before we throw an error for PHP, allow strings to be
75 } 77 }
76 } 78 }
77 79
78 // Before we throw an error check if the string is longer than 80 // Before we throw an error check if the string is longer than
79 // the line length limit. 81 // the line length limit.
80 $lineLengthLimitSniff = new Drupal_Sniffs_Files_LineLengthSniff; 82 $lineLengthLimitSniff = new LineLengthSniff;
81 83
82 $lineLenght = $lineLengthLimitSniff->getLineLength($phpcsFile, $tokens[$prev]['line']); 84 $lineLenght = $lineLengthLimitSniff->getLineLength($phpcsFile, $tokens[$prev]['line']);
83 $stringLength = ($lineLenght + strlen($tokens[$next]['content']) - 4); 85 $stringLength = ($lineLenght + strlen($tokens[$next]['content']) - 4);
84 if ($stringLength > $lineLengthLimitSniff->lineLimit) { 86 if ($stringLength > $lineLengthLimitSniff->lineLimit) {
85 return; 87 return;