|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Php73\Rector\FuncCall; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\FuncCall; |
| 9 | +use PhpParser\Node\Identifier; |
| 10 | +use PhpParser\Node\Name; |
| 11 | +use Rector\Rector\AbstractRector; |
| 12 | +use Rector\ValueObject\PhpVersionFeature; |
| 13 | +use Rector\ValueObject\PolyfillPackage; |
| 14 | +use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
| 15 | +use Rector\VersionBonding\Contract\RelatedPolyfillInterface; |
| 16 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 17 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 18 | + |
| 19 | +/** |
| 20 | + * @see \Rector\Tests\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector\ArrayKeysToArrayKeyFirstLastRectorTest |
| 21 | + */ |
| 22 | +final class ArrayKeysToArrayKeyFirstLastRector extends AbstractRector implements MinPhpVersionInterface, RelatedPolyfillInterface |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var array<string, string> |
| 26 | + */ |
| 27 | + private const array PREVIOUS_TO_NEW_FUNCTIONS = [ |
| 28 | + 'current' => 'array_key_first', |
| 29 | + 'reset' => 'array_key_first', |
| 30 | + 'end' => 'array_key_last', |
| 31 | + ]; |
| 32 | + |
| 33 | + public function getRuleDefinition(): RuleDefinition |
| 34 | + { |
| 35 | + return new RuleDefinition( |
| 36 | + 'Make use of array_key_first() and array_key_last() instead of reading a single key off array_keys()', |
| 37 | + [ |
| 38 | + new CodeSample( |
| 39 | + <<<'CODE_SAMPLE' |
| 40 | +$firstKey = current(array_keys($items)); |
| 41 | +$lastKey = end(array_keys($items)); |
| 42 | +CODE_SAMPLE |
| 43 | + , |
| 44 | + <<<'CODE_SAMPLE' |
| 45 | +$firstKey = array_key_first($items); |
| 46 | +$lastKey = array_key_last($items); |
| 47 | +CODE_SAMPLE |
| 48 | + ), |
| 49 | + ] |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @return array<class-string<Node>> |
| 55 | + */ |
| 56 | + public function getNodeTypes(): array |
| 57 | + { |
| 58 | + return [FuncCall::class]; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param FuncCall $node |
| 63 | + */ |
| 64 | + public function refactor(Node $node): ?Node |
| 65 | + { |
| 66 | + if ($node->isFirstClassCallable()) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + $funcName = $this->getName($node); |
| 71 | + if ($funcName === null) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + if (! isset(self::PREVIOUS_TO_NEW_FUNCTIONS[$funcName])) { |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + $args = $node->getArgs(); |
| 80 | + if (count($args) !== 1) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + if ($args[0]->name instanceof Identifier || $args[0]->unpack) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + $arrayKeysFuncCall = $args[0]->value; |
| 89 | + if (! $arrayKeysFuncCall instanceof FuncCall) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + if (! $this->isName($arrayKeysFuncCall, 'array_keys')) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + if ($arrayKeysFuncCall->isFirstClassCallable()) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + $arrayKeysArgs = $arrayKeysFuncCall->getArgs(); |
| 102 | + |
| 103 | + // array_keys() with a search value returns only the matching keys, so the first one is not the array's first key |
| 104 | + if (count($arrayKeysArgs) !== 1) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + if ($arrayKeysArgs[0]->name instanceof Identifier || $arrayKeysArgs[0]->unpack) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + $node->name = new Name(self::PREVIOUS_TO_NEW_FUNCTIONS[$funcName]); |
| 113 | + $node->args = $arrayKeysArgs; |
| 114 | + |
| 115 | + return $node; |
| 116 | + } |
| 117 | + |
| 118 | + public function provideMinPhpVersion(): int |
| 119 | + { |
| 120 | + return PhpVersionFeature::ARRAY_KEY_FIRST_LAST; |
| 121 | + } |
| 122 | + |
| 123 | + public function providePolyfillPackage(): string |
| 124 | + { |
| 125 | + return PolyfillPackage::PHP_73; |
| 126 | + } |
| 127 | +} |
0 commit comments