1<?php 2/** 3 * Squiz_Sniffs_Strings_DoubleQuoteUsageSniff. 4 * 5 * PHP version 5 6 * 7 * @category PHP 8 * @package PHP_CodeSniffer 9 * @author Greg Sherwood <gsherwood@squiz.net> 10 * @author Marc McIntyre <mmcintyre@squiz.net> 11 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) 12 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence 13 * @link http://pear.php.net/package/PHP_CodeSniffer 14 */ 15 16/** 17 * Squiz_Sniffs_Strings_DoubleQuoteUsageSniff. 18 * 19 * Makes sure that any use of Double Quotes ("") are warranted. 20 * 21 * @category PHP 22 * @package PHP_CodeSniffer 23 * @author Greg Sherwood <gsherwood@squiz.net> 24 * @author Marc McIntyre <mmcintyre@squiz.net> 25 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) 26 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence 27 * @version Release: @package_version@ 28 * @link http://pear.php.net/package/PHP_CodeSniffer 29 */ 30class Squiz_Sniffs_Strings_DoubleQuoteUsageSniff implements PHP_CodeSniffer_Sniff 31{ 32 33 34 /** 35 * Returns an array of tokens this test wants to listen for. 36 * 37 * @return array 38 */ 39 public function register() 40 { 41 return array( 42 T_CONSTANT_ENCAPSED_STRING, 43 T_DOUBLE_QUOTED_STRING, 44 ); 45 46 }//end register() 47 48 49 /** 50 * Processes this test, when one of its tokens is encountered. 51 * 52 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 53 * @param int $stackPtr The position of the current token 54 * in the stack passed in $tokens. 55 * 56 * @return void 57 */ 58 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 59 { 60 $tokens = $phpcsFile->getTokens(); 61 62 // We are only interested in the first token in a multi-line string. 63 if ($tokens[$stackPtr]['code'] === $tokens[($stackPtr - 1)]['code']) { 64 return; 65 } 66 67 $workingString = $tokens[$stackPtr]['content']; 68 $lastStringToken = $stackPtr; 69 70 $i = ($stackPtr + 1); 71 if (isset($tokens[$i]) === true) { 72 while ($i < $phpcsFile->numTokens 73 && $tokens[$i]['code'] === $tokens[$stackPtr]['code'] 74 ) { 75 $workingString .= $tokens[$i]['content']; 76 $lastStringToken = $i; 77 $i++; 78 } 79 } 80 81 // Check if it's a double quoted string. 82 if (strpos($workingString, '"') === false) { 83 return; 84 } 85 86 // Make sure it's not a part of a string started in a previous line. 87 // If it is, then we have already checked it. 88 if ($workingString[0] !== '"') { 89 return; 90 } 91 92 // The use of variables in double quoted strings is not allowed. 93 if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING) { 94 $stringTokens = token_get_all('<?php '.$workingString); 95 foreach ($stringTokens as $token) { 96 if (is_array($token) === true && $token[0] === T_VARIABLE) { 97 $error = 'Variable "%s" not allowed in double quoted string; use concatenation instead'; 98 $data = array($token[1]); 99 $phpcsFile->addError($error, $stackPtr, 'ContainsVar', $data); 100 } 101 } 102 103 return; 104 }//end if 105 106 $allowedChars = array( 107 '\0', 108 '\1', 109 '\2', 110 '\3', 111 '\4', 112 '\5', 113 '\6', 114 '\7', 115 '\n', 116 '\r', 117 '\f', 118 '\t', 119 '\v', 120 '\x', 121 '\b', 122 '\e', 123 '\u', 124 '\'', 125 ); 126 127 foreach ($allowedChars as $testChar) { 128 if (strpos($workingString, $testChar) !== false) { 129 return; 130 } 131 } 132 133 $error = 'String %s does not require double quotes; use single quotes instead'; 134 $data = array(str_replace(array("\r", "\n"), array('\r', '\n'), $workingString)); 135 $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotRequired', $data); 136 137 if ($fix === true) { 138 $phpcsFile->fixer->beginChangeset(); 139 $innerContent = substr($workingString, 1, -1); 140 $innerContent = str_replace('\"', '"', $innerContent); 141 $phpcsFile->fixer->replaceToken($stackPtr, "'$innerContent'"); 142 while ($lastStringToken !== $stackPtr) { 143 $phpcsFile->fixer->replaceToken($lastStringToken, ''); 144 $lastStringToken--; 145 } 146 147 $phpcsFile->fixer->endChangeset(); 148 } 149 150 }//end process() 151 152 153}//end class 154