Skip to content

Commit bc7f896

Browse files
committed
bug #4440 SimpleToComplexStringVariableFixer - Fix $ bug (dmvdbrugge)
This PR was merged into the 2.15 branch. Discussion ---------- SimpleToComplexStringVariableFixer - Fix $ bug Account for $ being the last character of the encapsulating string, it needs to be escaped in order to not form a new simple string variable. Commits ------- 0820e76 SimpleToComplexStringVariableFixer - Fix $ bug
2 parents da6acd9 + 0820e76 commit bc7f896

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/Fixer/StringNotation/SimpleToComplexStringVariableFixer.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,16 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
9393
continue;
9494
}
9595

96-
$tokens->overrideRange($index, $index + 2, [
96+
$tokenOfStringBeforeToken = $tokens[$index - 1];
97+
$stringContent = $tokenOfStringBeforeToken->getContent();
98+
99+
if ('$' === substr($stringContent, -1) && '\\$' !== substr($stringContent, -2)) {
100+
$newContent = substr($stringContent, 0, -1).'\\$';
101+
$tokenOfStringBeforeToken = new Token([T_ENCAPSED_AND_WHITESPACE, $newContent]);
102+
}
103+
104+
$tokens->overrideRange($index - 1, $index + 2, [
105+
$tokenOfStringBeforeToken,
97106
new Token([T_CURLY_OPEN, '{']),
98107
new Token([T_VARIABLE, '$'.$varnameToken->getContent()]),
99108
new Token([CT::T_CURLY_CLOSE, '}']),

tests/Fixer/StringNotation/SimpleToComplexStringVariableFixerTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,48 @@ public function provideFixCases()
9595
<?php
9696
$name = 'World';
9797
echo "Hello \${name}";
98+
EXPECTED
99+
,
100+
],
101+
'double dollar' => [
102+
<<<'EXPECTED'
103+
<?php
104+
$name = 'World';
105+
echo "Hello \${$name}";
106+
EXPECTED
107+
,
108+
<<<'INPUT'
109+
<?php
110+
$name = 'World';
111+
echo "Hello $${name}";
112+
INPUT
113+
,
114+
],
115+
'double dollar heredoc' => [
116+
<<<'EXPECTED'
117+
<?php
118+
$name = 'World';
119+
echo <<<TEST
120+
Hello \${$name}!
121+
TEST;
122+
123+
EXPECTED
124+
,
125+
<<<'INPUT'
126+
<?php
127+
$name = 'World';
128+
echo <<<TEST
129+
Hello $${name}!
130+
TEST;
131+
132+
INPUT
133+
,
134+
],
135+
'double dollar single quote' => [
136+
<<<'EXPECTED'
137+
<?php
138+
$name = 'World';
139+
echo 'Hello $${name}';
98140
EXPECTED
99141
,
100142
],

0 commit comments

Comments
 (0)