mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-20 06:38:46 +01:00
[Code Quality] If the whole concatenated string is longer than 120 chars, skip it (#4052)
This commit is contained in:
parent
36b24b2bfc
commit
12691991bb
@ -11,6 +11,7 @@ use PhpParser\Node\Scalar\String_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
/**
|
||||
* @see \Rector\CodeQuality\Tests\Rector\Concat\JoinStringConcatRector\JoinStringConcatRectorTest
|
||||
@ -57,22 +58,29 @@ PHP
|
||||
return [Concat::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $nodeReplacementIsRestricted = false;
|
||||
|
||||
/**
|
||||
* @param Concat $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$originalNode = clone $node;
|
||||
$this->nodeReplacementIsRestricted = false;
|
||||
|
||||
if (! $this->isTopMostConcatNode($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$joinedNode = $this->joinConcatIfStrings($node);
|
||||
if (! $joinedNode instanceof String_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// the value is too long
|
||||
if (Strings::length($joinedNode->value) >= self::LINE_BREAK_POINT) {
|
||||
// this prevents keeping joins
|
||||
return $originalNode;
|
||||
if ($this->nodeReplacementIsRestricted) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $joinedNode;
|
||||
@ -81,8 +89,10 @@ PHP
|
||||
/**
|
||||
* @return Concat|String_
|
||||
*/
|
||||
private function joinConcatIfStrings(Concat $concat): Node
|
||||
private function joinConcatIfStrings(Concat $node): Node
|
||||
{
|
||||
$concat = clone $node;
|
||||
|
||||
if ($concat->left instanceof Concat) {
|
||||
$concat->left = $this->joinConcatIfStrings($concat->left);
|
||||
}
|
||||
@ -92,13 +102,24 @@ PHP
|
||||
}
|
||||
|
||||
if (! $concat->left instanceof String_) {
|
||||
return $concat;
|
||||
return $node;
|
||||
}
|
||||
|
||||
if (! $concat->right instanceof String_) {
|
||||
return $concat;
|
||||
return $node;
|
||||
}
|
||||
|
||||
return new String_($concat->left->value . $concat->right->value);
|
||||
$resultStringNode = new String_($concat->left->value . $concat->right->value);
|
||||
if (Strings::length($resultStringNode->value) >= self::LINE_BREAK_POINT) {
|
||||
$this->nodeReplacementIsRestricted = true;
|
||||
return $node;
|
||||
}
|
||||
|
||||
return $resultStringNode;
|
||||
}
|
||||
|
||||
private function isTopMostConcatNode(Concat $node): bool
|
||||
{
|
||||
return ! ($node->getAttribute(AttributeKey::PARENT_NODE) instanceof Concat);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Concat\JoinStringConcatRector\Fixture;
|
||||
|
||||
class SkipWhenOneOfTheLinesBecomesLongerThan120
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$name = 'How it works? ' .
|
||||
'1. It adds $uuid property to entities. ' .
|
||||
'2. It makes things better. ' .
|
||||
'And finally: ' .
|
||||
'Require for step-by-step migration from int to uuid. ' .
|
||||
'In following step it should be renamed to $id and replace it';
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user