2018-10-10 15:51:14 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\Php\Rector\FuncCall;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Name;
|
2019-02-23 09:11:01 +00:00
|
|
|
use PhpParser\Node\Stmt\Expression;
|
2019-04-13 09:20:27 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2018-10-10 15:51:14 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\RectorDefinition\CodeSample;
|
|
|
|
use Rector\RectorDefinition\RectorDefinition;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
final class ArrayKeyFirstLastRector extends AbstractRector
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private $previousToNewFunctions = [
|
|
|
|
'reset' => 'array_key_first',
|
|
|
|
'end' => 'array_key_last',
|
|
|
|
];
|
|
|
|
|
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
return new RectorDefinition(
|
|
|
|
'Make use of array_key_first() and array_key_last()',
|
|
|
|
[
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
reset($items);
|
|
|
|
$firstKey = key($items);
|
|
|
|
CODE_SAMPLE
|
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
$firstKey = array_key_first($items);
|
|
|
|
CODE_SAMPLE
|
|
|
|
),
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
end($items);
|
|
|
|
$lastKey = key($items);
|
|
|
|
CODE_SAMPLE
|
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
$lastKey = array_key_last($items);
|
|
|
|
CODE_SAMPLE
|
|
|
|
),
|
|
|
|
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
2018-10-22 15:53:21 +00:00
|
|
|
return [FuncCall::class];
|
2018-10-10 15:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-22 15:53:21 +00:00
|
|
|
* @param FuncCall $node
|
2018-10-10 15:51:14 +00:00
|
|
|
*/
|
2018-10-15 04:36:58 +00:00
|
|
|
public function refactor(Node $node): ?Node
|
2018-10-10 15:51:14 +00:00
|
|
|
{
|
2019-02-27 13:02:28 +00:00
|
|
|
if (! $this->isAtLeastPhpVersion('7.3')) {
|
2019-02-23 13:03:07 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-02-23 09:11:01 +00:00
|
|
|
if (! $this->isNames($node, ['reset', 'end'])) {
|
2018-10-22 15:53:21 +00:00
|
|
|
return null;
|
2018-10-10 15:51:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 09:11:01 +00:00
|
|
|
$nextExpression = $this->getNextExpression($node);
|
|
|
|
if ($nextExpression === null) {
|
2018-10-22 15:53:21 +00:00
|
|
|
return null;
|
2018-10-10 15:51:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 09:11:01 +00:00
|
|
|
$resetOrEndFuncCall = $node;
|
2018-10-10 15:51:14 +00:00
|
|
|
|
2019-02-23 09:11:01 +00:00
|
|
|
/** @var FuncCall|null $keyFuncCall */
|
|
|
|
$keyFuncCall = $this->betterNodeFinder->findFirst($nextExpression, function (Node $node) use (
|
|
|
|
$resetOrEndFuncCall
|
|
|
|
) {
|
|
|
|
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
|
|
|
|
2019-02-23 09:11:01 +00:00
|
|
|
if ($keyFuncCall === null) {
|
2018-10-22 15:53:21 +00:00
|
|
|
return null;
|
2018-10-10 15:51:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 09:11:01 +00:00
|
|
|
$newName = $this->previousToNewFunctions[$this->getName($node)];
|
|
|
|
$keyFuncCall->name = new Name($newName);
|
2018-10-10 15:51:14 +00:00
|
|
|
|
2019-02-23 09:11:01 +00:00
|
|
|
$this->removeNode($node);
|
2018-10-10 15:51:14 +00:00
|
|
|
|
2018-10-22 15:53:21 +00:00
|
|
|
return $node;
|
2018-10-10 15:51:14 +00:00
|
|
|
}
|
2019-02-23 09:11:01 +00:00
|
|
|
|
|
|
|
private function getNextExpression(Node $node): ?Node
|
|
|
|
{
|
|
|
|
/** @var Expression|null $currentExpression */
|
2019-04-13 09:20:27 +00:00
|
|
|
$currentExpression = $node->getAttribute(AttributeKey::CURRENT_EXPRESSION);
|
2019-02-23 09:11:01 +00:00
|
|
|
if ($currentExpression === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-04-13 09:20:27 +00:00
|
|
|
return $currentExpression->getAttribute(AttributeKey::NEXT_NODE);
|
2019-02-23 09:11:01 +00:00
|
|
|
}
|
2018-10-10 15:51:14 +00:00
|
|
|
}
|