Skip to content

Commit

Permalink
minor: ExplicitStringVariableFixer - Fix to PHP8.2 compat code (#6424)
Browse files Browse the repository at this point in the history
ExplicitStringVariableFixer - Fix to PHP8.2 compat code
  • Loading branch information
SpacePossum authored Jul 10, 2022
1 parent 83071d9 commit aacc804
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion doc/rules/string_notation/explicit_string_variable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Example #1
-$a = "My name is $name !";
-$b = "I live in $state->country !";
-$c = "I have $farm[0] chickens !";
+$a = "My name is ${name} !";
+$a = "My name is {$name} !";
+$b = "I live in {$state->country} !";
+$c = "I have {$farm[0]} chickens !";
Expand Down
10 changes: 6 additions & 4 deletions src/Fixer/StringNotation/ExplicitStringVariableFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function getDefinition(): FixerDefinitionInterface
/**
* {@inheritdoc}
*
* Must run before SimpleToComplexStringVariableFixer.
* Must run after BacktickToShellExecFixer.
*/
public function getPriority(): int
Expand All @@ -79,6 +78,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$backtickStarted = false;
for ($index = \count($tokens) - 1; $index > 0; --$index) {
$token = $tokens[$index];

if ($token->equals('`')) {
$backtickStarted = !$backtickStarted;

Expand All @@ -90,6 +90,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
}

$prevToken = $tokens[$index - 1];

if (!$this->isStringPartToken($prevToken)) {
continue;
}
Expand All @@ -105,6 +106,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void

$nextIndex = $index + 1;
$squareBracketCount = 0;

while (!$this->isStringPartToken($tokens[$nextIndex])) {
if ($tokens[$nextIndex]->isGivenKind(T_CURLY_OPEN)) {
$nextIndex = $tokens->getNextTokenOfKind($nextIndex, [[CT::T_CURLY_CLOSE]]);
Expand Down Expand Up @@ -133,9 +135,9 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$singleVariableIndex = key($distinctVariableSet['tokens']);
$singleVariableToken = current($distinctVariableSet['tokens']);
$tokens->overrideRange($singleVariableIndex, $singleVariableIndex, [
new Token([T_DOLLAR_OPEN_CURLY_BRACES, '${']),
new Token([T_STRING_VARNAME, substr($singleVariableToken->getContent(), 1)]),
new Token([CT::T_DOLLAR_CLOSE_CURLY_BRACES, '}']),
new Token([T_CURLY_OPEN, '{']),
new Token([T_VARIABLE, $singleVariableToken->getContent()]),
new Token([CT::T_CURLY_CLOSE, '}']),
]);
} else {
foreach ($distinctVariableSet['tokens'] as $variablePartIndex => $variablePartToken) {
Expand Down
4 changes: 1 addition & 3 deletions tests/AutoReview/FixerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ public function testFixerWithNoneDefaultPriorityIsTested(): void
$knownIssues = [ // should only shrink
'final_class' => true,
'psr_autoloading' => true,
'simple_to_complex_string_variable' => true, // had prio case but no longer, left prio the same for BC reasons
'single_blank_line_before_namespace' => true,
];

Expand Down Expand Up @@ -402,9 +403,6 @@ private static function getFixersPriorityGraph(): array
'heredoc_to_nowdoc',
'single_quote',
],
'explicit_string_variable' => [
'simple_to_complex_string_variable',
],
'final_internal_class' => [
'protected_to_private',
'self_static_accessor',
Expand Down
28 changes: 14 additions & 14 deletions tests/Fixer/StringNotation/ExplicitStringVariableFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public function provideTestFixCases(): array
$input = $expected = '<?php';

for ($inc = 1; $inc < 15; ++$inc) {
$expected .= " \$var{$inc} = \"My name is {\$name}!\";";
$input .= " \$var{$inc} = \"My name is \$name!\";";
$expected .= " \$var{$inc} = \"My name is \${name}!\";";
}

return [
Expand All @@ -48,16 +48,16 @@ public function provideTestFixCases(): array
$input,
],
[
'<?php $a = "My name is ${name}!";',
'<?php $a = "My name is {$name}!";',
'<?php $a = "My name is $name!";',
],
[
'<?php "My name is ${james}${bond}!";',
'<?php "My name is {$james}{$bond}!";',
'<?php "My name is $james$bond!";',
],
[
'<?php $a = <<<EOF
My name is ${name}!
My name is {$name}!
EOF;
',
'<?php $a = <<<EOF
Expand All @@ -66,20 +66,20 @@ public function provideTestFixCases(): array
',
],
[
'<?php $a = "${b}";',
'<?php $a = "{$b}";',
'<?php $a = "$b";',
],
[
'<?php $a = "${b} start";',
'<?php $a = "{$b} start";',
'<?php $a = "$b start";',
],
[
'<?php $a = "end ${b}";',
'<?php $a = "end {$b}";',
'<?php $a = "end $b";',
],
[
'<?php $a = <<<EOF
${b}
{$b}
EOF;
',
'<?php $a = <<<EOF
Expand Down Expand Up @@ -126,11 +126,11 @@ public function provideTestFixCases(): array
'<?php $a = "My name is $array[$foo] !";',
],
[
'<?php $a = "My name is {$array[$foo]}[${bar}] !";',
'<?php $a = "My name is {$array[$foo]}[{$bar}] !";',
'<?php $a = "My name is $array[$foo][$bar] !";',
],
[
'<?php $a = "Closure not allowed ${closure}() text";',
'<?php $a = "Closure not allowed {$closure}() text";',
'<?php $a = "Closure not allowed $closure() text";',
],
[
Expand All @@ -142,7 +142,7 @@ public function provideTestFixCases(): array
'<?php $a = "Complex array chaining not allowed $array[1][2][MY_CONSTANT] text";',
],
[
'<?php $a = "Concatenation: ${james}${bond}{$object->property}{$array[1]}!";',
'<?php $a = "Concatenation: {$james}{$bond}{$object->property}{$array[1]}!";',
'<?php $a = "Concatenation: $james$bond$object->property$array[1]!";',
],
[
Expand Down Expand Up @@ -201,19 +201,19 @@ public function provideTestFixCases(): array
'<?php $a = `echo $foo`;',
],
[
'<?php $a = "My name is ${name}!"; $a = `echo $foo`; $a = "{$a->b} start";',
'<?php $a = "My name is {$name}!"; $a = `echo $foo`; $a = "{$a->b} start";',
'<?php $a = "My name is $name!"; $a = `echo $foo`; $a = "$a->b start";',
],
[
'<?php $mobileNumberVisible = "***-***-{$last4Digits[0]}{$last4Digits[1]}-{$last4Digits[2]}{$last4Digits[3]}";',
'<?php $mobileNumberVisible = "***-***-$last4Digits[0]$last4Digits[1]-$last4Digits[2]$last4Digits[3]";',
],
[
'<?php $pair = "${foo} {$bar[0]}";',
'<?php $pair = "{$foo} {$bar[0]}";',
'<?php $pair = "$foo {$bar[0]}";',
],
[
'<?php $pair = "${foo}{$bar[0]}";',
'<?php $pair = "{$foo}{$bar[0]}";',
'<?php $pair = "$foo{$bar[0]}";',
],
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Integration of fixers: backtick_to_shell_exec,explicit_string_variable.
{"backtick_to_shell_exec": true, "explicit_string_variable": true}
--EXPECT--
<?php
$a = shell_exec("echo ${foo}");
$a = shell_exec("echo {$foo}");

--INPUT--
<?php
Expand Down

0 comments on commit aacc804

Please sign in to comment.