2020-07-16 15:40:04 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodingStyle\Rector\Ternary;
|
2020-07-16 15:40:04 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\Ternary;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-07-16 15:40:04 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodingStyle\Rector\Ternary\TernaryConditionVariableAssignmentRector\TernaryConditionVariableAssignmentRectorTest
|
2020-07-16 15:40:04 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class TernaryConditionVariableAssignmentRector extends AbstractRector
|
2020-07-16 15:40:04 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-07-16 15:40:04 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Assign outcome of ternary condition to variable, where applicable', [new CodeSample(<<<'CODE_SAMPLE'
|
2020-07-16 15:40:04 +00:00
|
|
|
function ternary($value)
|
|
|
|
{
|
|
|
|
$value ? $a = 1 : $a = 0;
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-07-16 15:40:04 +00:00
|
|
|
function ternary($value)
|
|
|
|
{
|
|
|
|
$a = $value ? 1 : 0;
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-07-16 15:40:04 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-07-16 15:40:04 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-07-16 15:40:04 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Ternary::class];
|
2020-07-16 15:40:04 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Ternary $node
|
2020-07-16 15:40:04 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-07-16 15:40:04 +00:00
|
|
|
{
|
|
|
|
$nodeIf = $node->if;
|
|
|
|
$nodeElse = $node->else;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$nodeIf instanceof Assign) {
|
2020-12-24 16:28:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$nodeElse instanceof Assign) {
|
2020-07-16 15:40:04 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$nodeIfVar = $nodeIf->var;
|
|
|
|
$nodeElseVar = $nodeElse->var;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$nodeIfVar instanceof Variable) {
|
2020-12-24 16:28:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$nodeElseVar instanceof Variable) {
|
2020-07-16 15:40:04 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($nodeIfVar->name !== $nodeElseVar->name) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-05-22 07:35:57 +00:00
|
|
|
$assignedTo = $node->getAttribute(AttributeKey::IS_ASSIGNED_TO);
|
|
|
|
if ($assignedTo === \true) {
|
2020-07-16 15:40:04 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$node->if = $nodeIf->expr;
|
|
|
|
$node->else = $nodeElse->expr;
|
2022-06-07 08:22:29 +00:00
|
|
|
$variable = new Variable($nodeIfVar->name);
|
|
|
|
return new Assign($variable, $node);
|
2020-07-16 15:40:04 +00:00
|
|
|
}
|
|
|
|
}
|