From 71dff85f84584da25d2afdb10bf980556014114f Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 4 Sep 2017 02:19:22 +0200 Subject: [PATCH 1/3] 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. --- .../Sniffs/Strings/DoubleQuoteUsageSniff.php | 16 ++++++++++++++-- .../Tests/Strings/DoubleQuoteUsageUnitTest.inc | 3 +++ .../Strings/DoubleQuoteUsageUnitTest.inc.fixed | 3 +++ .../Tests/Strings/DoubleQuoteUsageUnitTest.php | 1 + 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php b/src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php index c1369aa664..19dc860c70 100644 --- a/src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php +++ b/src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php @@ -49,7 +49,14 @@ public function process(File $phpcsFile, $stackPtr) return; } - $workingString = $tokens[$stackPtr]['content']; + // 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']; + } + $lastStringToken = $stackPtr; $i = ($stackPtr + 1); @@ -57,7 +64,12 @@ public function process(File $phpcsFile, $stackPtr) 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++; } diff --git a/src/Standards/Squiz/Tests/Strings/DoubleQuoteUsageUnitTest.inc b/src/Standards/Squiz/Tests/Strings/DoubleQuoteUsageUnitTest.inc index d0b22ceba4..c8cc638369 100644 --- a/src/Standards/Squiz/Tests/Strings/DoubleQuoteUsageUnitTest.inc +++ b/src/Standards/Squiz/Tests/Strings/DoubleQuoteUsageUnitTest.inc @@ -29,6 +29,9 @@ $string = "\123 \234"."\u123"."\e"; echo "window.location = \"".$url."\";\n"; echo "" +$string = "Hello + there"; + function test() { echo "It Worked'; } diff --git a/src/Standards/Squiz/Tests/Strings/DoubleQuoteUsageUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Strings/DoubleQuoteUsageUnitTest.inc.fixed index fb142e54ab..97309194ee 100644 --- a/src/Standards/Squiz/Tests/Strings/DoubleQuoteUsageUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/Strings/DoubleQuoteUsageUnitTest.inc.fixed @@ -29,6 +29,9 @@ $string = "\123 \234"."\u123"."\e"; echo 'window.location = "'.$url."\";\n"; echo '' +$string = 'Hello + there'; + function test() { echo "It Worked'; } diff --git a/src/Standards/Squiz/Tests/Strings/DoubleQuoteUsageUnitTest.php b/src/Standards/Squiz/Tests/Strings/DoubleQuoteUsageUnitTest.php index 83036db769..e0fda07bdf 100644 --- a/src/Standards/Squiz/Tests/Strings/DoubleQuoteUsageUnitTest.php +++ b/src/Standards/Squiz/Tests/Strings/DoubleQuoteUsageUnitTest.php @@ -38,6 +38,7 @@ public function getErrorList() 22 => 1, 29 => 1, 30 => 1, + 32 => 1, ); }//end getErrorList() From 0d90d189bcc9f11f0c9a457993c77f96f3d2f41c Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 4 Sep 2017 02:06:29 +0200 Subject: [PATCH 2/3] Squiz/DoubleQuoteUsage: Skip past subsequent tokens in a multi-line string As the last token of the multi-line string is known, it can be returned to skip past subsequent tokens in a multi-line string instead of checking each string token. --- .../Sniffs/Strings/DoubleQuoteUsageSniff.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php b/src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php index 19dc860c70..d3b8eed8ea 100644 --- a/src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php +++ b/src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php @@ -44,11 +44,6 @@ 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) { @@ -75,15 +70,17 @@ public function process(File $phpcsFile, $stackPtr) } } + $skipTo = ($lastStringToken + 1); + // Check if it's a double quoted string. if (strpos($workingString, '"') === false) { - return; + return $skipTo; } // 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; + return $skipTo; } // The use of variables in double quoted strings is not allowed. @@ -97,7 +94,7 @@ public function process(File $phpcsFile, $stackPtr) } } - return; + return $skipTo; }//end if $allowedChars = array( @@ -123,7 +120,7 @@ public function process(File $phpcsFile, $stackPtr) foreach ($allowedChars as $testChar) { if (strpos($workingString, $testChar) !== false) { - return; + return $skipTo; } } @@ -145,6 +142,8 @@ public function process(File $phpcsFile, $stackPtr) $phpcsFile->fixer->endChangeset(); } + return $skipTo; + }//end process() From ba6afb84d665a9abbab24610bb3f883294197094 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 4 Sep 2017 02:08:35 +0200 Subject: [PATCH 3/3] Squiz/DoubleQuoteUsage: Make check for double quoted string a little more specific and strict Only check strings which are finished double quotes strings. This offers additional protection against examining unfinished strings during live code checking. --- .../Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php b/src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php index d3b8eed8ea..8077fa355c 100644 --- a/src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php +++ b/src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php @@ -73,13 +73,7 @@ public function process(File $phpcsFile, $stackPtr) $skipTo = ($lastStringToken + 1); // Check if it's a double quoted string. - if (strpos($workingString, '"') === false) { - return $skipTo; - } - - // 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] !== '"') { + if ($workingString[0] !== '"' || substr($workingString, -1) !== '"') { return $skipTo; }