2019-12-29 15:50:40 +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\CodeQuality\Rector\Ternary;
|
2019-12-29 15:50:40 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr\ArrayDimFetch;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Coalesce;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Expr\Ternary;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-12-29 15:50:40 +00:00
|
|
|
/**
|
2022-05-12 19:17:07 +00:00
|
|
|
* @changelog https://3v4l.org/f7itn
|
2019-12-29 15:50:40 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector\ArrayKeyExistsTernaryThenValueToCoalescingRectorTest
|
2019-12-29 15:50:40 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ArrayKeyExistsTernaryThenValueToCoalescingRector extends AbstractRector
|
2019-12-29 15:50:40 +00:00
|
|
|
{
|
2023-09-20 12:53:23 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\Value\ValueResolver
|
2023-09-20 12:53:23 +00:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
|
|
|
public function __construct(ValueResolver $valueResolver)
|
|
|
|
{
|
|
|
|
$this->valueResolver = $valueResolver;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-12-29 15:50:40 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change array_key_exists() ternary to coalescing', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-12-29 15:50:40 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($values, $keyToMatch)
|
|
|
|
{
|
|
|
|
$result = array_key_exists($keyToMatch, $values) ? $values[$keyToMatch] : null;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-12-29 15:50:40 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($values, $keyToMatch)
|
|
|
|
{
|
|
|
|
$result = $values[$keyToMatch] ?? null;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-12-29 15:50:40 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-12-29 15:50:40 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-12-29 15:50:40 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Ternary::class];
|
2019-12-29 15:50:40 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Ternary $node
|
2019-12-29 15:50:40 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-12-29 15:50:40 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node->cond instanceof FuncCall) {
|
2019-12-29 15:50:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isName($node->cond, 'array_key_exists')) {
|
2019-12-29 15:50:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node->if instanceof ArrayDimFetch) {
|
2019-12-29 15:50:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->areArrayKeysExistsArgsMatchingDimFetch($node->cond, $node->if)) {
|
2019-12-29 15:50:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isNull($node->else)) {
|
2020-09-27 20:34:02 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new Coalesce($node->if, $node->else);
|
2019-12-29 15:50:40 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Equals if:
|
|
|
|
*
|
|
|
|
* array_key_exists($key, $values);
|
|
|
|
* =
|
|
|
|
* $values[$key]
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function areArrayKeysExistsArgsMatchingDimFetch(FuncCall $funcCall, ArrayDimFetch $arrayDimFetch) : bool
|
2019-12-29 15:50:40 +00:00
|
|
|
{
|
2021-09-27 15:43:15 +00:00
|
|
|
$firstArg = $funcCall->args[0];
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$firstArg instanceof Arg) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
$keyExpr = $firstArg->value;
|
|
|
|
$secondArg = $funcCall->args[1];
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$secondArg instanceof Arg) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
$valuesExpr = $secondArg->value;
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($arrayDimFetch->var, $valuesExpr)) {
|
|
|
|
return \false;
|
2019-12-29 15:50:40 +00:00
|
|
|
}
|
2021-02-19 12:01:23 +00:00
|
|
|
return $this->nodeComparator->areNodesEqual($arrayDimFetch->dim, $keyExpr);
|
2019-12-29 15:50:40 +00:00
|
|
|
}
|
|
|
|
}
|