Skip to content

Commit

Permalink
[CodingStyle] Use double quote to escape quotes in EncapsedStringsToS…
Browse files Browse the repository at this point in the history
…printfRector (#6326)

* add test fixture for rectorphp/rector#8838

* use double quote to escape quotes in EncapsedStringsToSprintfRector

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
TomasVotruba and actions-user authored Sep 23, 2024
1 parent 5170018 commit 398b04d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

final class TranslationFunction
{
public function run(string $pattern)
{
echo _("My translatable text with '{$pattern}'.");
}
}

?>
-----
<?php

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

final class TranslationFunction
{
public function run(string $pattern)
{
echo _(sprintf("My translatable text with '%s'.", $pattern));
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ private function createSprintfFuncCallOrConcat(string $mask, array $argumentVari
return null;
}

$arguments = [new Arg(new String_($mask))];
$string = $this->createString($mask);

$arguments = [new Arg($string)];
foreach ($argumentVariables as $argumentVariable) {
$arguments[] = new Arg($argumentVariable);
}
Expand Down Expand Up @@ -239,4 +241,13 @@ private function createSingleValueEdgeConcat(array $argumentVariables, string $m

return null;
}

private function createString(string $value): String_
{
$kind = str_contains($value, "'") ? String_::KIND_DOUBLE_QUOTED : String_::KIND_SINGLE_QUOTED;

return new String_($value, [
'kind' => $kind,
]);
}
}

0 comments on commit 398b04d

Please sign in to comment.