2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2018-10-17 12:27:22 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\Assign;
|
2018-10-12 16:42:12 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\PhpParser\Node\AssignAndBinaryMap;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
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\Assign\CombinedAssignRector\CombinedAssignRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
final class CombinedAssignRector extends \Rector\Core\Rector\AbstractRector
|
2018-10-12 16:42:12 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Node\AssignAndBinaryMap
|
2018-10-12 16:42:12 +00:00
|
|
|
*/
|
2018-10-27 14:02:49 +00:00
|
|
|
private $assignAndBinaryMap;
|
2021-05-10 22:23:08 +00:00
|
|
|
public function __construct(\Rector\Core\PhpParser\Node\AssignAndBinaryMap $assignAndBinaryMap)
|
2018-10-25 21:59:19 +00:00
|
|
|
{
|
2018-10-27 14:02:49 +00:00
|
|
|
$this->assignAndBinaryMap = $assignAndBinaryMap;
|
2018-10-25 21:59:19 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
2018-10-12 16:42:12 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Simplify $value = $value + 5; assignments to shorter ones', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample('$value = $value + 5;', '$value += 5;')]);
|
2018-10-12 16:42:12 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-10-12 16:42:12 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-10-12 16:42:12 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return [\PhpParser\Node\Expr\Assign::class];
|
2018-10-12 16:42:12 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 09:57:54 +00:00
|
|
|
* @param \PhpParser\Node $node
|
2018-10-12 16:42:12 +00:00
|
|
|
*/
|
2021-12-10 09:57:54 +00:00
|
|
|
public function refactor($node) : ?\PhpParser\Node
|
2018-10-12 16:42:12 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$node->expr instanceof \PhpParser\Node\Expr\BinaryOp) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-12 16:42:12 +00:00
|
|
|
}
|
|
|
|
/** @var BinaryOp $binaryNode */
|
2018-10-15 04:36:58 +00:00
|
|
|
$binaryNode = $node->expr;
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($node->var, $binaryNode->left)) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-12 16:42:12 +00:00
|
|
|
}
|
2018-10-27 14:02:49 +00:00
|
|
|
$assignClass = $this->assignAndBinaryMap->getAlternative($binaryNode);
|
2018-10-25 21:59:19 +00:00
|
|
|
if ($assignClass === null) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-12 16:42:12 +00:00
|
|
|
}
|
2018-10-25 21:59:19 +00:00
|
|
|
return new $assignClass($node->var, $binaryNode->right);
|
2018-10-12 16:42:12 +00:00
|
|
|
}
|
|
|
|
}
|