Source for file DoubleQuoteUsageSniff.php
Documentation is available at DoubleQuoteUsageSniff.php
* Makes sure that any use of double quotes strings are warranted.
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Strings;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
class DoubleQuoteUsageSniff implements Sniff
* Returns an array of tokens this test wants to listen for.
public function register ()
T_CONSTANT_ENCAPSED_STRING ,
* Processes this test, when one of its tokens is encountered.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
public function process (File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens ();
// If tabs are being converted to spaces by the tokeniser, the
// original content should be used instead of the converted content.
if (isset ($tokens[$stackPtr]['orig_content']) === true ) {
$workingString = $tokens[$stackPtr]['orig_content'];
$workingString = $tokens[$stackPtr]['content'];
$lastStringToken = $stackPtr;
if (isset ($tokens[$i]) === true ) {
while ($i < $phpcsFile->numTokens
&& $tokens[$i]['code'] === $tokens[$stackPtr]['code']
if (isset ($tokens[$i]['orig_content']) === true ) {
$workingString .= $tokens[$i]['orig_content'];
$workingString .= $tokens[$i]['content'];
$skipTo = ($lastStringToken + 1 );
// Check if it's a double quoted string.
if ($workingString[0 ] !== '"' || substr($workingString, -1 ) !== '"') {
// The use of variables in double quoted strings is not allowed.
foreach ($stringTokens as $token) {
if (is_array($token) === true && $token[0 ] === T_VARIABLE ) {
$error = 'Variable "%s" not allowed in double quoted string; use concatenation instead';
$data = array ($token[1 ]);
$phpcsFile->addError ($error, $stackPtr, 'ContainsVar', $data);
foreach ($allowedChars as $testChar) {
if (strpos($workingString, $testChar) !== false ) {
$error = 'String %s does not require double quotes; use single quotes instead';
$data = array (str_replace(array ("\r", "\n"), array ('\r', '\n'), $workingString));
$fix = $phpcsFile->addFixableError ($error, $stackPtr, 'NotRequired', $data);
$phpcsFile->fixer ->beginChangeset ();
$innerContent = substr($workingString, 1 , -1 );
$innerContent = str_replace('\\$', '$', $innerContent);
$phpcsFile->fixer ->replaceToken ($stackPtr, " '$innerContent'" );
while ($lastStringToken !== $stackPtr) {
$phpcsFile->fixer ->replaceToken ($lastStringToken, '');
$phpcsFile->fixer ->endChangeset ();
Documentation generated on Mon, 11 Mar 2019 15:27:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|