Create WrapEncapsedVariableInCurlyBracesRector

This commit is contained in:
Jonathan Goode 2020-07-21 18:13:15 +01:00
parent 972068e346
commit d0b791d823
9 changed files with 208 additions and 2 deletions

View File

@ -12,6 +12,7 @@ use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsPar
use Rector\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector;
use Rector\CodingStyle\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector;
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
use Rector\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector;
use Rector\CodingStyle\Rector\FuncCall\CallUserFuncCallToVariadicRector;
use Rector\CodingStyle\Rector\FuncCall\ConsistentImplodeRector;
use Rector\CodingStyle\Rector\FuncCall\ConsistentPregDelimiterRector;
@ -73,6 +74,8 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(EncapsedStringsToSprintfRector::class);
$services->set(WrapEncapsedVariableInCurlyBracesRector::class);
$services->set(NewlineBeforeNewAssignSetRector::class);
$services->set(ManualJsonStringToJsonEncodeArrayRector::class);

View File

@ -1,4 +1,4 @@
# All 527 Rectors Overview
# All 528 Rectors Overview
- [Projects](#projects)
- [General](#general)
@ -11,7 +11,7 @@
- [CakePHP](#cakephp) (6)
- [Celebrity](#celebrity) (3)
- [CodeQuality](#codequality) (54)
- [CodingStyle](#codingstyle) (35)
- [CodingStyle](#codingstyle) (36)
- [DeadCode](#deadcode) (40)
- [Decomplex](#decomplex) (1)
- [Decouple](#decouple) (1)
@ -2352,6 +2352,23 @@ Changes use of call to version compare function to use of PHP version constant
<br><br>
### `WrapEncapsedVariableInCurlyBracesRector`
- class: [`Rector\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector`](/../master/rules/coding-style/src/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector.php)
- [test fixtures](/../master/rules/coding-style/tests/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector/Fixture)
Wrap encapsed variables in curly braces
```diff
function run($world)
{
- echo "Hello $world!"
+ echo "Hello {$world}!"
}
```
<br><br>
### `YieldClassMethodToArrayClassMethodRector`
- class: [`Rector\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector`](/../master/rules/coding-style/src/Rector/ClassMethod/YieldClassMethodToArrayClassMethodRector.php)

View File

@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\Rector\Encapsed;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\Encapsed;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\NodeTypeResolver\Node\AttributeKey;
/**
* @see \Rector\CodingStyle\Tests\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector\WrapEncapsedVariableInCurlyBracesRectorTest
*/
final class WrapEncapsedVariableInCurlyBracesRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Wrap encapsed variables in curly braces', [
new CodeSample(
<<<'PHP'
function run($world)
{
echo "Hello $world!"
}
PHP
,
<<<'PHP'
function run($world)
{
echo "Hello {$world}!"
}
PHP
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Encapsed::class];
}
/**
* @param Encapsed $node
*/
public function refactor(Node $node): ?Node
{
$startTokenPos = $node->getStartTokenPos();
$hasVariableBeenWrapped = false;
foreach ($node->parts as $index => $nodePart) {
if ($nodePart instanceof Variable) {
$previousNode = $nodePart->getAttribute(AttributeKey::PREVIOUS_NODE);
$previousNodeEndTokenPosition = $previousNode === null ? $startTokenPos : $previousNode->getEndTokenPos();
if ($previousNodeEndTokenPosition + 1 === $nodePart->getStartTokenPos()) {
$hasVariableBeenWrapped = true;
$node->parts[$index] = new Variable($nodePart->name);
}
}
}
if (! $hasVariableBeenWrapped) {
return null;
}
return $node;
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector\Fixture;
function run($world)
{
return "Hello $world!";
}
?>
-----
<?php
namespace Rector\CodingStyle\Tests\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector\Fixture;
function run($world)
{
return "Hello {$world}!";
}
?>

View File

@ -0,0 +1,21 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector\Fixture;
function multiple_variables($hello, $world)
{
return "$hello $world!";
}
?>
-----
<?php
namespace Rector\CodingStyle\Tests\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector\Fixture;
function multiple_variables($hello, $world)
{
return "{$hello} {$world}!";
}
?>

View File

@ -0,0 +1,8 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector\Fixture;
function skip_braces_already_present($world)
{
return "Hello {$world}!";
}

View File

@ -0,0 +1,8 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector\Fixture;
function skip_single_quotes($world)
{
return 'Hello $world!';
}

View File

@ -0,0 +1,21 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector\Fixture;
function some_variables_with_braces($hello, $world)
{
return "$hello {$world}!";
}
?>
-----
<?php
namespace Rector\CodingStyle\Tests\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector\Fixture;
function some_variables_with_braces($hello, $world)
{
return "{$hello} {$world}!";
}
?>

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\Tests\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector;
use Iterator;
use Rector\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class WrapEncapsedVariableInCurlyBracesRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
protected function getRectorClass(): string
{
return WrapEncapsedVariableInCurlyBracesRector::class;
}
}