Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Squiz.Strings.DoubleQuoteUsage replaces tabs with spaces when fixing #1640

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,37 @@ public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

// We are only interested in the first token in a multi-line string.
if ($tokens[$stackPtr]['code'] === $tokens[($stackPtr - 1)]['code']) {
return;
// 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'];
} else {
$workingString = $tokens[$stackPtr]['content'];
}

$workingString = $tokens[$stackPtr]['content'];
$lastStringToken = $stackPtr;

$i = ($stackPtr + 1);
if (isset($tokens[$i]) === true) {
while ($i < $phpcsFile->numTokens
&& $tokens[$i]['code'] === $tokens[$stackPtr]['code']
) {
$workingString .= $tokens[$i]['content'];
if (isset($tokens[$i]['orig_content']) === true) {
$workingString .= $tokens[$i]['orig_content'];
} else {
$workingString .= $tokens[$i]['content'];
}

$lastStringToken = $i;
$i++;
}
}

// Check if it's a double quoted string.
if (strpos($workingString, '"') === false) {
return;
}
$skipTo = ($lastStringToken + 1);

// Make sure it's not a part of a string started in a previous line.
// If it is, then we have already checked it.
if ($workingString[0] !== '"') {
return;
// Check if it's a double quoted string.
if ($workingString[0] !== '"' || substr($workingString, -1) !== '"') {
return $skipTo;
}

// The use of variables in double quoted strings is not allowed.
Expand All @@ -85,7 +88,7 @@ public function process(File $phpcsFile, $stackPtr)
}
}

return;
return $skipTo;
}//end if

$allowedChars = array(
Expand All @@ -111,7 +114,7 @@ public function process(File $phpcsFile, $stackPtr)

foreach ($allowedChars as $testChar) {
if (strpos($workingString, $testChar) !== false) {
return;
return $skipTo;
}
}

Expand All @@ -133,6 +136,8 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->fixer->endChangeset();
}

return $skipTo;

}//end process()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ $string = "\123 \234"."\u123"."\e";
echo "window.location = \"".$url."\";\n";
echo ""

$string = "Hello
there";

function test() {
echo "It Worked';
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ $string = "\123 \234"."\u123"."\e";
echo 'window.location = "'.$url."\";\n";
echo ''

$string = 'Hello
there';

function test() {
echo "It Worked';
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function getErrorList()
22 => 1,
29 => 1,
30 => 1,
32 => 1,
);

}//end getErrorList()
Expand Down