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

Fix for Newline with EncapsedStringsToSprintfRector #5060

Closed
wants to merge 5 commits into from
Closed
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 @@ -97,6 +97,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 @@ -122,8 +123,16 @@ private function collectExpr(Expr $expr): void
private function createSprintfFuncCallOrConcat(string $string, array $argumentVariables): Node
{
// special case for variable with PHP_EOL
if ($string === '%s' && count($argumentVariables) === 2) {
return new Concat($argumentVariables[0], $argumentVariables[1]);
if ($string === '%s%s' && count($argumentVariables) === 2 &&
count(
array_filter(
$argumentVariables,
fn($argumentVar): bool => $argumentVar instanceof ConstFetch && $argumentVar->name.'' === 'PHP_EOL'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argument->name . '' should can be (string) argument->name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samsonasik thanks, guessing you're also building against >7.3 as it doesn't like the return type on the closure

)
)
)
{
return new Concat($argumentVariables[0], $argumentVariables[1]);
}

$arguments = [new Arg(new String_($string))];
Expand Down
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);
}
}

?>