annotate vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/Generic/Sniffs/Strings/UnnecessaryStringConcatSniff.php @ 9:1fc0ff908d1f

Add another data file
author Chris Cannam
date Mon, 05 Feb 2018 12:34:32 +0000
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@0 3 * Generic_Sniffs_Strings_UnnecessaryStringConcatSniff.
Chris@0 4 *
Chris@0 5 * PHP version 5
Chris@0 6 *
Chris@0 7 * @category PHP
Chris@0 8 * @package PHP_CodeSniffer
Chris@0 9 * @author Greg Sherwood <gsherwood@squiz.net>
Chris@0 10 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
Chris@0 11 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
Chris@0 12 * @link http://pear.php.net/package/PHP_CodeSniffer
Chris@0 13 */
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Generic_Sniffs_Strings_UnnecessaryStringConcatSniff.
Chris@0 17 *
Chris@0 18 * Checks that two strings are not concatenated together; suggests
Chris@0 19 * using one string instead.
Chris@0 20 *
Chris@0 21 * @category PHP
Chris@0 22 * @package PHP_CodeSniffer
Chris@0 23 * @author Greg Sherwood <gsherwood@squiz.net>
Chris@0 24 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
Chris@0 25 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
Chris@0 26 * @version Release: @package_version@
Chris@0 27 * @link http://pear.php.net/package/PHP_CodeSniffer
Chris@0 28 */
Chris@0 29 class Generic_Sniffs_Strings_UnnecessaryStringConcatSniff implements PHP_CodeSniffer_Sniff
Chris@0 30 {
Chris@0 31
Chris@0 32 /**
Chris@0 33 * A list of tokenizers this sniff supports.
Chris@0 34 *
Chris@0 35 * @var array
Chris@0 36 */
Chris@0 37 public $supportedTokenizers = array(
Chris@0 38 'PHP',
Chris@0 39 'JS',
Chris@0 40 );
Chris@0 41
Chris@0 42 /**
Chris@0 43 * If true, an error will be thrown; otherwise a warning.
Chris@0 44 *
Chris@0 45 * @var bool
Chris@0 46 */
Chris@0 47 public $error = true;
Chris@0 48
Chris@0 49 /**
Chris@0 50 * If true, strings concatenated over multiple lines are allowed.
Chris@0 51 *
Chris@0 52 * Useful if you break strings over multiple lines to work
Chris@0 53 * within a max line length.
Chris@0 54 *
Chris@0 55 * @var bool
Chris@0 56 */
Chris@0 57 public $allowMultiline = false;
Chris@0 58
Chris@0 59
Chris@0 60 /**
Chris@0 61 * Returns an array of tokens this test wants to listen for.
Chris@0 62 *
Chris@0 63 * @return array
Chris@0 64 */
Chris@0 65 public function register()
Chris@0 66 {
Chris@0 67 return array(
Chris@0 68 T_STRING_CONCAT,
Chris@0 69 T_PLUS,
Chris@0 70 );
Chris@0 71
Chris@0 72 }//end register()
Chris@0 73
Chris@0 74
Chris@0 75 /**
Chris@0 76 * Processes this sniff, when one of its tokens is encountered.
Chris@0 77 *
Chris@0 78 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
Chris@0 79 * @param int $stackPtr The position of the current token
Chris@0 80 * in the stack passed in $tokens.
Chris@0 81 *
Chris@0 82 * @return void
Chris@0 83 */
Chris@0 84 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
Chris@0 85 {
Chris@0 86 // Work out which type of file this is for.
Chris@0 87 $tokens = $phpcsFile->getTokens();
Chris@0 88 if ($tokens[$stackPtr]['code'] === T_STRING_CONCAT) {
Chris@0 89 if ($phpcsFile->tokenizerType === 'JS') {
Chris@0 90 return;
Chris@0 91 }
Chris@0 92 } else {
Chris@0 93 if ($phpcsFile->tokenizerType === 'PHP') {
Chris@0 94 return;
Chris@0 95 }
Chris@0 96 }
Chris@0 97
Chris@0 98 $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
Chris@0 99 $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
Chris@0 100 if ($prev === false || $next === false) {
Chris@0 101 return;
Chris@0 102 }
Chris@0 103
Chris@0 104 if (isset(PHP_CodeSniffer_Tokens::$stringTokens[$tokens[$prev]['code']]) === true
Chris@0 105 && isset(PHP_CodeSniffer_Tokens::$stringTokens[$tokens[$next]['code']]) === true
Chris@0 106 ) {
Chris@0 107 if ($tokens[$prev]['content'][0] === $tokens[$next]['content'][0]) {
Chris@0 108 // Before we throw an error for PHP, allow strings to be
Chris@0 109 // combined if they would have < and ? next to each other because
Chris@0 110 // this trick is sometimes required in PHP strings.
Chris@0 111 if ($phpcsFile->tokenizerType === 'PHP') {
Chris@0 112 $prevChar = substr($tokens[$prev]['content'], -2, 1);
Chris@0 113 $nextChar = $tokens[$next]['content'][1];
Chris@0 114 $combined = $prevChar.$nextChar;
Chris@0 115 if ($combined === '?'.'>' || $combined === '<'.'?') {
Chris@0 116 return;
Chris@0 117 }
Chris@0 118 }
Chris@0 119
Chris@0 120 if ($this->allowMultiline === true
Chris@0 121 && $tokens[$prev]['line'] !== $tokens[$next]['line']
Chris@0 122 ) {
Chris@0 123 return;
Chris@0 124 }
Chris@0 125
Chris@0 126 $error = 'String concat is not required here; use a single string instead';
Chris@0 127 if ($this->error === true) {
Chris@0 128 $phpcsFile->addError($error, $stackPtr, 'Found');
Chris@0 129 } else {
Chris@0 130 $phpcsFile->addWarning($error, $stackPtr, 'Found');
Chris@0 131 }
Chris@0 132 }//end if
Chris@0 133 }//end if
Chris@0 134
Chris@0 135 }//end process()
Chris@0 136
Chris@0 137
Chris@0 138 }//end class