rector/rules/php73/src/Rector/FuncCall/ArrayKeyFirstLastRector.php

134 lines
3.3 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
2018-10-10 15:51:14 +00:00
namespace Rector\Php73\Rector\FuncCall;
2018-10-10 15:51:14 +00:00
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2018-10-10 15:51:14 +00:00
/**
* @see https://www.tomasvotruba.cz/blog/2018/08/16/whats-new-in-php-73-in-30-seconds-in-diffs/#2-first-and-last-array-key
*
* This needs to removed 1 floor above, because only nodes in arrays can be removed why traversing,
* see https://github.com/nikic/PHP-Parser/issues/389
* @see \Rector\Php73\Tests\Rector\FuncCall\ArrayKeyFirstLastRector\ArrayKeyFirstLastRectorTest
2018-10-10 15:51:14 +00:00
*/
final class ArrayKeyFirstLastRector extends AbstractRector
{
/**
* @var string
*/
private const ARRAY_KEY_FIRST = 'array_key_first';
/**
* @var string
*/
private const ARRAY_KEY_LAST = 'array_key_last';
2018-10-10 15:51:14 +00:00
/**
* @var array<string, string>
2018-10-10 15:51:14 +00:00
*/
2020-02-18 22:09:25 +00:00
private const PREVIOUS_TO_NEW_FUNCTIONS = [
'reset' => self::ARRAY_KEY_FIRST,
'end' => self::ARRAY_KEY_LAST,
2018-10-10 15:51:14 +00:00
];
public function getRuleDefinition(): RuleDefinition
2018-10-10 15:51:14 +00:00
{
return new RuleDefinition(
2018-10-10 15:51:14 +00:00
'Make use of array_key_first() and array_key_last()',
[
new CodeSample(
<<<'CODE_SAMPLE'
2018-10-10 15:51:14 +00:00
reset($items);
$firstKey = key($items);
CODE_SAMPLE
2018-10-10 15:51:14 +00:00
,
<<<'CODE_SAMPLE'
2018-10-10 15:51:14 +00:00
$firstKey = array_key_first($items);
CODE_SAMPLE
2018-10-10 15:51:14 +00:00
),
new CodeSample(
<<<'CODE_SAMPLE'
2018-10-10 15:51:14 +00:00
end($items);
$lastKey = key($items);
CODE_SAMPLE
2018-10-10 15:51:14 +00:00
,
<<<'CODE_SAMPLE'
2018-10-10 15:51:14 +00:00
$lastKey = array_key_last($items);
CODE_SAMPLE
2018-10-10 15:51:14 +00:00
),
]
);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
2018-10-10 15:51:14 +00:00
}
/**
* @param FuncCall $node
2018-10-10 15:51:14 +00:00
*/
public function refactor(Node $node): ?Node
2018-10-10 15:51:14 +00:00
{
if ($this->shouldSkip($node)) {
return null;
2018-10-10 15:51:14 +00:00
}
$nextExpression = $this->getNextExpression($node);
if (! $nextExpression instanceof Node) {
return null;
2018-10-10 15:51:14 +00:00
}
$resetOrEndFuncCall = $node;
2018-10-10 15:51:14 +00:00
$keyFuncCall = $this->betterNodeFinder->findFirst($nextExpression, function (Node $node) use (
$resetOrEndFuncCall
): bool {
if (! $node instanceof FuncCall) {
return false;
}
if (! $this->isName($node, 'key')) {
return false;
}
return $this->areNodesEqual($resetOrEndFuncCall->args[0], $node->args[0]);
});
2018-10-10 15:51:14 +00:00
if (! $keyFuncCall instanceof FuncCall) {
return null;
2018-10-10 15:51:14 +00:00
}
2020-02-18 22:09:25 +00:00
$newName = self::PREVIOUS_TO_NEW_FUNCTIONS[$this->getName($node)];
$keyFuncCall->name = new Name($newName);
2018-10-10 15:51:14 +00:00
$this->removeNode($node);
2018-10-10 15:51:14 +00:00
return $node;
2018-10-10 15:51:14 +00:00
}
private function shouldSkip(FuncCall $funcCall): bool
{
if (! $this->isNames($funcCall, ['reset', 'end'])) {
return true;
}
if ($this->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_KEY_FIRST_LAST)) {
return false;
}
2019-12-29 21:47:10 +00:00
return ! (function_exists(self::ARRAY_KEY_FIRST) && function_exists(self::ARRAY_KEY_LAST));
}
2018-10-10 15:51:14 +00:00
}