mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-11 11:02:38 +02:00
[CodingStyle] Add EncapsedStringsToSprintfRector (#1722)
[CodingStyle] Add EncapsedStringsToSprintfRector
This commit is contained in:
commit
08837ff459
@ -23,3 +23,4 @@ services:
|
||||
Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector: ~
|
||||
|
||||
Rector\CodingStyle\Rector\ClassConst\VarConstantCommentRector: ~
|
||||
Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector: ~
|
||||
|
@ -0,0 +1,75 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Rector\Encapsed;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Scalar\Encapsed;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\CodingStyle\Tests\Rector\Encapsed\EncapsedStringsToSprintfRector\EncapsedStringsToSprintfRectorTest
|
||||
*/
|
||||
final class EncapsedStringsToSprintfRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Convert enscaped {$string} to more readable sprintf', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(string $format)
|
||||
{
|
||||
return "Unsupported format {$format}";
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(string $format)
|
||||
{
|
||||
return sprintf('Unsupported format %s', $format);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Encapsed::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Encapsed $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$string = '';
|
||||
$arguments = [];
|
||||
|
||||
foreach ($node->parts as $part) {
|
||||
if ($part instanceof Node\Scalar\EncapsedStringPart) {
|
||||
$string .= $part->value;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($part instanceof Node\Expr\Variable) {
|
||||
$string .= '%s';
|
||||
$arguments[] = new Node\Arg($part);
|
||||
}
|
||||
}
|
||||
|
||||
$arguments = array_merge([new Node\Arg(new Node\Scalar\String_($string))], $arguments);
|
||||
|
||||
return new Node\Expr\FuncCall(new Node\Name('sprintf'), $arguments);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Encapsed\EncapsedStringsToSprintfRector;
|
||||
|
||||
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class EncapsedStringsToSprintfRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/numberz.php.inc']);
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return EncapsedStringsToSprintfRector::class;
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Encapsed\EncapsedStringsToSprintfRector\Fixture;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(string $format)
|
||||
{
|
||||
return "Unsupported format {$format}";
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Encapsed\EncapsedStringsToSprintfRector\Fixture;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(string $format)
|
||||
{
|
||||
return sprintf('Unsupported format %s', $format);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Encapsed\EncapsedStringsToSprintfRector\Fixture;
|
||||
|
||||
final class Numberz
|
||||
{
|
||||
public function run(string $format, int $value, float $money)
|
||||
{
|
||||
return "Format {$format} from {$value} to {$money}";
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Encapsed\EncapsedStringsToSprintfRector\Fixture;
|
||||
|
||||
final class Numberz
|
||||
{
|
||||
public function run(string $format, int $value, float $money)
|
||||
{
|
||||
return sprintf('Format %s from %s to %s', $format, $value, $money);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user