2020-07-21 17:13:15 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodingStyle\Rector\Encapsed;
|
2020-07-21 17:13:15 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Scalar\Encapsed;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-07-21 17:13:15 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector\WrapEncapsedVariableInCurlyBracesRectorTest
|
2020-07-21 17:13:15 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class WrapEncapsedVariableInCurlyBracesRector extends AbstractRector
|
2020-07-21 17:13:15 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-07-21 17:13:15 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Wrap encapsed variables in curly braces', [new CodeSample(<<<'CODE_SAMPLE'
|
2020-07-21 17:13:15 +00:00
|
|
|
function run($world)
|
|
|
|
{
|
2021-07-02 10:36:52 +00:00
|
|
|
echo "Hello $world!";
|
2020-07-21 17:13:15 +00:00
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-07-21 17:13:15 +00:00
|
|
|
function run($world)
|
|
|
|
{
|
2021-07-02 10:36:52 +00:00
|
|
|
echo "Hello {$world}!";
|
2020-07-21 17:13:15 +00:00
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-07-21 17:13:15 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-07-21 17:13:15 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-07-21 17:13:15 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Encapsed::class];
|
2020-07-21 17:13:15 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Encapsed $node
|
2020-07-21 17:13:15 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-07-21 17:13:15 +00:00
|
|
|
{
|
|
|
|
$startTokenPos = $node->getStartTokenPos();
|
2021-05-09 20:15:43 +00:00
|
|
|
$hasVariableBeenWrapped = \false;
|
2020-07-21 17:13:15 +00:00
|
|
|
foreach ($node->parts as $index => $nodePart) {
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($nodePart instanceof Variable) {
|
2023-03-23 19:21:21 +00:00
|
|
|
$previousNode = $node->parts[$index - 1] ?? null;
|
2022-06-07 08:22:29 +00:00
|
|
|
$previousNodeEndTokenPosition = $previousNode instanceof Node ? $previousNode->getEndTokenPos() : $startTokenPos;
|
2020-07-21 17:13:15 +00:00
|
|
|
if ($previousNodeEndTokenPosition + 1 === $nodePart->getStartTokenPos()) {
|
2021-05-09 20:15:43 +00:00
|
|
|
$hasVariableBeenWrapped = \true;
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->parts[$index] = new Variable($nodePart->name);
|
2020-07-21 17:13:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$hasVariableBeenWrapped) {
|
2020-07-21 17:13:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|