Skip to content

Commit 19ced07

Browse files
Khartirclaude
authored andcommitted
Fix VariableInDoubleQuotedString sniff wrapping property/array access
The sniff incorrectly fixed "$this->key" to "{$this}->key" instead of "{$this->key}" because the regex only matched bare variable names. Expand the regex to also capture ->property and [index] suffixes, matching PHP's simple string interpolation rules. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent cef4136 commit 19ced07

5 files changed

Lines changed: 9 additions & 1 deletion

MO4/Sniffs/Strings/VariableInDoubleQuotedStringSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function register(): array
6060
*/
6161
public function process(File $phpcsFile, $stackPtr): void
6262
{
63-
$varRegExp = '/\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/';
63+
$varRegExp = '/\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\->[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*|\[[^\]]*\])?/';
6464

6565
$tokens = $phpcsFile->getTokens();
6666
$content = $tokens[$stackPtr]['content'];

MO4/Tests/Strings/VariableInDoubleQuotedStringUnitTest.fail.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ $d = "blafasel $_a";
99
$f = "100$missing needed";
1010
$g = "foo {$a} bar $b";
1111
$h = " foo } bar { foo $a";
12+
$i = "value is $this->key ok";
13+
$j = "value is $arr[0] ok";

MO4/Tests/Strings/VariableInDoubleQuotedStringUnitTest.fail.inc.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ $d = "blafasel {$_a}";
99
$f = "100{$missing} needed";
1010
$g = "foo {$a} bar {$b}";
1111
$h = " foo } bar { foo {$a}";
12+
$i = "value is {$this->key} ok";
13+
$j = "value is {$arr[0]} ok";

MO4/Tests/Strings/VariableInDoubleQuotedStringUnitTest.pass.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ $o = "{$a['foo'][$b[0]]}";
2222
$p = "{$a[$b->foo($c['foo'][$d[0]])]}";
2323
$p = "{$good[$fail]}";
2424
$q = "{$prefix}/{$this->resolveName($id)}";
25+
$r = "{$this->key}";
26+
$s = "{$arr[0]}";

MO4/Tests/Strings/VariableInDoubleQuotedStringUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class VariableInDoubleQuotedStringUnitTest extends AbstractMo4SniffUnitTest
4444
9 => 1,
4545
10 => 1,
4646
11 => 1,
47+
12 => 1,
48+
13 => 1,
4749
],
4850
];
4951
}

0 commit comments

Comments
 (0)