-
-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodingStyle] Add EncapsedStringsToSprintfRector
- Loading branch information
1 parent
f6751ac
commit 495063d
Showing
5 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/CodingStyle/src/Rector/Encapsed/EncapsedStringsToSprintfRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...sts/Rector/Encapsed/EncapsedStringsToSprintfRector/EncapsedStringsToSprintfRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
.../CodingStyle/tests/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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); | ||
} | ||
} | ||
|
||
?> |
27 changes: 27 additions & 0 deletions
27
.../CodingStyle/tests/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/numberz.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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); | ||
} | ||
} | ||
|
||
?> |