mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-03 18:00:50 +00:00
7d36c3b0b9
a80cf5292d
revert to working scoper 0.14
71 lines
2.0 KiB
PHP
71 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\CodeQuality\Rector\LogicalAnd;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr\Assign;
|
|
use PhpParser\Node\Expr\BinaryOp\LogicalAnd;
|
|
use PhpParser\Node\Stmt\Expression;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @see https://3v4l.org/ji8bX
|
|
* @see \Rector\Tests\CodeQuality\Rector\LogicalAnd\AndAssignsToSeparateLinesRector\AndAssignsToSeparateLinesRectorTest
|
|
*/
|
|
final class AndAssignsToSeparateLinesRector extends \Rector\Core\Rector\AbstractRector
|
|
{
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Split 2 assigns ands to separate line', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
public function run()
|
|
{
|
|
$tokens = [];
|
|
$token = 4 and $tokens[] = $token;
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
public function run()
|
|
{
|
|
$tokens = [];
|
|
$token = 4;
|
|
$tokens[] = $token;
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Expr\BinaryOp\LogicalAnd::class];
|
|
}
|
|
/**
|
|
* @param LogicalAnd $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
if (!$node->left instanceof \PhpParser\Node\Expr\Assign) {
|
|
return null;
|
|
}
|
|
if (!$node->right instanceof \PhpParser\Node\Expr\Assign) {
|
|
return null;
|
|
}
|
|
$parentNode = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
|
|
if (!$parentNode instanceof \PhpParser\Node\Stmt\Expression) {
|
|
return null;
|
|
}
|
|
$this->addNodeAfterNode($node->right, $node);
|
|
return $node->left;
|
|
}
|
|
}
|