Skip to content

Commit 568c008

Browse files
committed
fixed VariableInDoubleQuotedStringSniff doesn't detect multiple variables in quotes
added fixer for sniff
1 parent dd1df37 commit 568c008

File tree

4 files changed

+43
-26
lines changed

4 files changed

+43
-26
lines changed

Sniffs/Strings/VariableInDoubleQuotedStringSniff.php

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,39 +59,47 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
5959

6060
$matches = array();
6161

62-
if (preg_match_all($varRegExp, $content, $matches, PREG_OFFSET_CAPTURE) !== 1) {
62+
if (preg_match_all($varRegExp, $content, $matches, PREG_OFFSET_CAPTURE) === 0) {
6363
return;
6464
}
6565

6666
foreach ($matches as $match) {
67-
list($var, $pos) = $match[0];
67+
foreach ($match as $info) {
68+
list($var, $pos) = $info;
6869

69-
if ($pos === 1 || $content[$pos - 1] !== '{') {
70-
// look for backslashes
71-
if ($content[$pos - 1] === "\\") {
72-
$bsPos = $pos - 1;
70+
if ($pos === 1 || $content[$pos - 1] !== '{') {
71+
// look for backslashes
72+
if ($content[$pos - 1] === "\\") {
73+
$bsPos = $pos - 1;
7374

74-
while ($content[$bsPos] === "\\") {
75-
$bsPos--;
76-
}
75+
while ($content[$bsPos] === "\\") {
76+
$bsPos--;
77+
}
7778

78-
if ((($pos - 1 - $bsPos) % 2) === 1) {
79-
continue;
79+
if ((($pos - 1 - $bsPos) % 2) === 1) {
80+
continue;
81+
}
8082
}
81-
}
82-
83-
$phpcsFile->addError(
84-
sprintf(
85-
'must surround variable %s with { }',
86-
$var
87-
),
88-
$stackPtr
89-
);
90-
continue;
91-
}
9283

84+
$fix = $phpcsFile->addFixableError(
85+
sprintf(
86+
'must surround variable %s with { }',
87+
$var
88+
),
89+
$stackPtr
90+
);
9391

92+
if ($fix) {
93+
$before = substr($content, 0, $pos);
94+
$after = substr($content, $pos + strlen($var));
95+
$newContent = $before . "{" . $var . "}" . $after;
9496

97+
$phpcsFile->fixer->beginChangeset();
98+
$phpcsFile->fixer->replaceToken($stackPtr, $newContent);
99+
$phpcsFile->fixer->endChangeset();
100+
}
101+
}
102+
}
95103
}
96104
}
97105
}

Tests/Strings/VariableInDoubleQuotedStringUnitTest.fail.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
$a = "$a";
44
$a = "\\\\$a";
5-
$a = "blafasel $a";
6-
$b = "blafasel \\$a";
5+
$a = "blafasel $a $b";
6+
$b = "blafasel \\$a$b";
77
$d = "blafasel $$a";
88
$d = "blafasel $_a";
99
$f = "100$missing needed";
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
$a = "{$a}";
4+
$a = "\\\\{$a}";
5+
$a = "blafasel {$a} {$b}";
6+
$b = "blafasel \\{$a}{$b}";
7+
$d = "blafasel ${$a}";
8+
$d = "blafasel {$_a}";
9+
$f = "100{$missing} needed";

Tests/Strings/VariableInDoubleQuotedStringUnitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ protected function getErrorList($testFile = '')
4848
return array(
4949
3 => 1,
5050
4 => 1,
51-
5 => 1,
52-
6 => 1,
51+
5 => 2,
52+
6 => 2,
5353
7 => 1,
5454
8 => 1,
5555
9 => 1,

0 commit comments

Comments
 (0)