Skip to content

Commit 71dff85

Browse files
committed
Squiz/DoubleQuoteUsage: Use original string when testing and fixing double quotes
If a string contains tabs, they were previously replaced by spaces while that is not the responsibility of this sniff.
1 parent e1d2859 commit 71dff85

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,27 @@ public function process(File $phpcsFile, $stackPtr)
4949
return;
5050
}
5151

52-
$workingString = $tokens[$stackPtr]['content'];
52+
// If tabs are being converted to spaces by the tokeniser, the
53+
// original content should be used instead of the converted content.
54+
if (isset($tokens[$stackPtr]['orig_content']) === true) {
55+
$workingString = $tokens[$stackPtr]['orig_content'];
56+
} else {
57+
$workingString = $tokens[$stackPtr]['content'];
58+
}
59+
5360
$lastStringToken = $stackPtr;
5461

5562
$i = ($stackPtr + 1);
5663
if (isset($tokens[$i]) === true) {
5764
while ($i < $phpcsFile->numTokens
5865
&& $tokens[$i]['code'] === $tokens[$stackPtr]['code']
5966
) {
60-
$workingString .= $tokens[$i]['content'];
67+
if (isset($tokens[$i]['orig_content']) === true) {
68+
$workingString .= $tokens[$i]['orig_content'];
69+
} else {
70+
$workingString .= $tokens[$i]['content'];
71+
}
72+
6173
$lastStringToken = $i;
6274
$i++;
6375
}

src/Standards/Squiz/Tests/Strings/DoubleQuoteUsageUnitTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ $string = "\123 \234"."\u123"."\e";
2929
echo "window.location = \"".$url."\";\n";
3030
echo ""
3131
32+
$string = "Hello
33+
there";
34+
3235
function test() {
3336
echo "It Worked';
3437
}

src/Standards/Squiz/Tests/Strings/DoubleQuoteUsageUnitTest.inc.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ $string = "\123 \234"."\u123"."\e";
2929
echo 'window.location = "'.$url."\";\n";
3030
echo ''
3131

32+
$string = 'Hello
33+
there';
34+
3235
function test() {
3336
echo "It Worked';
3437
}

src/Standards/Squiz/Tests/Strings/DoubleQuoteUsageUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function getErrorList()
3838
22 => 1,
3939
29 => 1,
4040
30 => 1,
41+
32 => 1,
4142
);
4243

4344
}//end getErrorList()

0 commit comments

Comments
 (0)