Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodingStyle] Add %s for pass PHP_EOL constant on EncapsedStringsToSprintfRector #5072

Merged
merged 8 commits into from
Jan 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Rector\CodingStyle\Rector\Encapsed;

use Nette\Utils\Strings;
use const PHP_EOL;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
Expand Down Expand Up @@ -97,6 +99,7 @@ private function collectEncapsedStringPart(EncapsedStringPart $encapsedStringPar
$stringValue = $encapsedStringPart->value;
if ($stringValue === "\n") {
$this->argumentVariables[] = new ConstFetch(new Name('PHP_EOL'));
$this->sprintfFormat .= '%s';
return;
}

Expand All @@ -117,20 +120,42 @@ private function collectExpr(Expr $expr): void

/**
* @param Expr[] $argumentVariables
* @return Concat|FuncCall
* @return Concat|FuncCall|null
*/
private function createSprintfFuncCallOrConcat(string $string, array $argumentVariables): Node
private function createSprintfFuncCallOrConcat(string $string, array $argumentVariables): ?Node
{
// special case for variable with PHP_EOL
if ($string === '%s' && count($argumentVariables) === 2) {
if ($string === '%s%s' && count($argumentVariables) === 2 && $this->hasEndOfLine($argumentVariables)) {
return new Concat($argumentVariables[0], $argumentVariables[1]);
}

if (Strings::contains($string, PHP_EOL)) {
return null;
}

$arguments = [new Arg(new String_($string))];
foreach ($argumentVariables as $argumentVariable) {
$arguments[] = new Arg($argumentVariable);
}

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

/**
* @param Expr[] $argumentVariables
*/
private function hasEndOfLine(array $argumentVariables): bool
{
foreach ($argumentVariables as $argumentVariable) {
if (! $argumentVariable instanceof ConstFetch) {
continue;
}

if ($this->isName($argumentVariable, 'PHP_EOL')) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

final class Newline2
{
public function run(string $format)
{
return "\n${format}";
}
}

?>
-----
<?php

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

final class Newline2
{
public function run(string $format)
{
return PHP_EOL . $format;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

final class PrefixedEol
{
public function run(string $format)
{
return "prefix $format\n";
}
}

?>
-----
<?php

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

final class PrefixedEol
{
public function run(string $format)
{
return sprintf('prefix %s%s', $format, PHP_EOL);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

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

final class SkipPrefixedMultiEol
{
public function run(string $format)
{
return "prefix {$format}\n\n";
}
}

?>