rector/rules/CodeQuality/Rector/Concat/JoinStringConcatRector.php

144 lines
3.3 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\Concat;
use Nette\Utils\Strings;
use PhpParser\Node;
2021-03-21 21:31:18 +00:00
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Tests\CodeQuality\Rector\Concat\JoinStringConcatRector\JoinStringConcatRectorTest
2019-09-03 09:11:45 +00:00
*/
final class JoinStringConcatRector extends AbstractRector
{
/**
* @var int
*/
private const LINE_BREAK_POINT = 100;
2020-08-29 09:03:40 +00:00
/**
* @var bool
*/
private $nodeReplacementIsRestricted = false;
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Joins concat of 2 strings, unless the length is too long',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$name = 'Hi' . ' Tom';
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$name = 'Hi Tom';
}
}
CODE_SAMPLE
2021-05-06 18:51:25 +00:00
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Concat::class];
}
/**
* @param Concat $node
*/
public function refactor(Node $node): ?Node
{
$this->nodeReplacementIsRestricted = false;
if (! $this->isTopMostConcatNode($node)) {
return null;
}
$joinedNode = $this->joinConcatIfStrings($node);
if (! $joinedNode instanceof String_) {
return null;
}
if ($this->nodeReplacementIsRestricted) {
return null;
}
return $joinedNode;
}
private function isTopMostConcatNode(Concat $concat): bool
{
2021-01-06 21:10:51 +00:00
$parent = $concat->getAttribute(AttributeKey::PARENT_NODE);
return ! $parent instanceof Concat;
}
/**
* @return Concat|String_
*/
2021-03-21 21:31:18 +00:00
private function joinConcatIfStrings(Concat $node): Expr
{
$concat = clone $node;
2019-02-22 17:25:31 +00:00
if ($concat->left instanceof Concat) {
$concat->left = $this->joinConcatIfStrings($concat->left);
}
2019-02-22 17:25:31 +00:00
if ($concat->right instanceof Concat) {
$concat->right = $this->joinConcatIfStrings($concat->right);
}
2019-02-22 17:25:31 +00:00
if (! $concat->left instanceof String_) {
return $node;
}
2019-02-22 17:25:31 +00:00
if (! $concat->right instanceof String_) {
return $node;
}
2021-01-06 21:10:51 +00:00
$leftValue = $concat->left->value;
$rightValue = $concat->right->value;
if ($leftValue === "\n") {
$this->nodeReplacementIsRestricted = true;
return $node;
}
if ($rightValue === "\n") {
$this->nodeReplacementIsRestricted = true;
return $node;
}
$resultString = new String_($leftValue . $rightValue);
if (Strings::length($resultString->value) >= self::LINE_BREAK_POINT) {
$this->nodeReplacementIsRestricted = true;
return $node;
}
return $resultString;
}
}