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