Skip to content

Commit

Permalink
Add single value concat support to EncapsedStringsToSprintfRector (#4897
Browse files Browse the repository at this point in the history
)
  • Loading branch information
TomasVotruba committed Sep 2, 2023
1 parent 521b6f8 commit 412d0bd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ final class EncapsedStringsToSprintfShouldEscapePercent
return "$value%";
}
}

?>
-----
<?php
Expand All @@ -19,7 +20,8 @@ final class EncapsedStringsToSprintfShouldEscapePercent
{
public function run(string $value)
{
return sprintf('%s%%', $value);
return $value . '%';
}
}

?>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class JustValue
public function testCommand(): void
{
$flag = 'hey';
$expected = [sprintf('--%s', $flag)];
$expected = ['--' . $flag];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Rector\Tests\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector\Fixture;

final class Fixture2
final class SingleEncapsed
{
private $format = 'json';

public function run(string $format)
{
return "Unsupported format {$this->format}";
return "{$this->format}";
}
}

Expand All @@ -18,13 +18,13 @@ final class Fixture2

namespace Rector\Tests\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector\Fixture;

final class Fixture2
final class SingleEncapsed
{
private $format = 'json';

public function run(string $format)
{
return sprintf('Unsupported format %s', $this->format);
return $this->format;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ public function getRuleDefinition(): RuleDefinition
[
new CodeSample(
<<<'CODE_SAMPLE'
echo "Unsupported format {$format}";
echo "Unsupported format {$format} - use another";
echo "Try {$allowed}";
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
echo sprintf('Unsupported format %s', $format);
echo sprintf('Unsupported format %s - use another', $format);
echo 'Try ' . $allowed;
CODE_SAMPLE
),
]
Expand Down Expand Up @@ -152,6 +156,11 @@ private function createSprintfFuncCallOrConcat(string $mask, array $argumentVari
return $this->nodeFactory->createConcat($argumentVariables);
}

$singleValueConcat = $this->createSingleValueEdgeConcat($argumentVariables, $mask);
if ($singleValueConcat instanceof Concat) {
return $singleValueConcat;
}

// checks for windows or linux line ending. \n is contained in both.
if (\str_contains($mask, "\n")) {
return null;
Expand All @@ -164,4 +173,32 @@ private function createSprintfFuncCallOrConcat(string $mask, array $argumentVari

return new FuncCall(new Name('sprintf'), $arguments);
}

/**
* @param Expr[] $argumentVariables
*/
private function createSingleValueEdgeConcat(array $argumentVariables, string $mask): ?Concat
{
if (count($argumentVariables) !== 1) {
return null;
}

if (substr_count($mask, '%s') !== 1) {
return null;
}

$cleanMask = Strings::replace($mask, '#\%\%#', '%');

if (str_ends_with($mask, '%s')) {
$bareString = new String_(substr($cleanMask, 0, -2));
return new Concat($bareString, $argumentVariables[0]);
}

if (str_starts_with($mask, '%s')) {
$bareString = new String_(substr($cleanMask, 2));
return new Concat($argumentVariables[0], $bareString);
}

return null;
}
}

0 comments on commit 412d0bd

Please sign in to comment.