Skip to content

Commit fa2c547

Browse files
SoeanTomasVotruba
authored andcommitted
[Php73] Add ArrayKeysToArrayKeyFirstLastRector
Reading a single key off array_keys() builds the full key array only to discard all but one entry. Turn it into the dedicated function instead: current(array_keys($items)) -> array_key_first($items) reset(array_keys($items)) -> array_key_first($items) end(array_keys($items)) -> array_key_last($items) This complements ArrayKeyFirstLastRector, which only covers the two-statement `reset($items); key($items);` form. array_keys() with a search value is skipped, as it returns just the matching keys, so its first entry is not the array's first key. Named args, spread, and first-class callables are skipped as well.
1 parent a9870fe commit fa2c547

9 files changed

Lines changed: 281 additions & 0 deletions

File tree

‎config/set/php-polyfills.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Rector\Config\RectorConfig;
66
use Rector\Php73\Rector\BooleanOr\IsCountableRector;
77
use Rector\Php73\Rector\FuncCall\ArrayKeyFirstLastRector;
8+
use Rector\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector;
89
use Rector\Php80\Rector\Identical\StrEndsWithRector;
910
use Rector\Php80\Rector\Identical\StrStartsWithRector;
1011
use Rector\Php80\Rector\NotIdentical\MbStrContainsRector;
@@ -23,6 +24,7 @@
2324
return static function (RectorConfig $rectorConfig): void {
2425
$rectorConfig->rules([
2526
ArrayKeyFirstLastRector::class,
27+
ArrayKeysToArrayKeyFirstLastRector::class,
2628
IsCountableRector::class,
2729
GetDebugTypeRector::class,
2830
StrStartsWithRector::class,

‎config/set/php73.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Rector\Php73\Rector\BooleanOr\IsCountableRector;
77
use Rector\Php73\Rector\ConstFetch\SensitiveConstantNameRector;
88
use Rector\Php73\Rector\FuncCall\ArrayKeyFirstLastRector;
9+
use Rector\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector;
910
use Rector\Php73\Rector\FuncCall\RegexDashEscapeRector;
1011
use Rector\Php73\Rector\FuncCall\SensitiveDefineRector;
1112
use Rector\Php73\Rector\FuncCall\SetCookieRector;
@@ -40,6 +41,7 @@
4041
IsCountableRector::class,
4142

4243
ArrayKeyFirstLastRector::class,
44+
ArrayKeysToArrayKeyFirstLastRector::class,
4345

4446
SensitiveDefineRector::class,
4547

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class ArrayKeysToArrayKeyFirstLastRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector\Fixture;
4+
5+
final class SomeClass
6+
{
7+
public function run(array $items)
8+
{
9+
$firstKey = current(array_keys($items));
10+
$alsoFirstKey = reset(array_keys($items));
11+
$lastKey = end(array_keys($items));
12+
13+
return [$firstKey, $alsoFirstKey, $lastKey];
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector\Fixture;
22+
23+
final class SomeClass
24+
{
25+
public function run(array $items)
26+
{
27+
$firstKey = array_key_first($items);
28+
$alsoFirstKey = array_key_first($items);
29+
$lastKey = array_key_last($items);
30+
31+
return [$firstKey, $alsoFirstKey, $lastKey];
32+
}
33+
}
34+
35+
?>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector\Fixture;
4+
5+
final class NestedExpression
6+
{
7+
public function run(array $items)
8+
{
9+
return trim((string) current(array_keys($this->resolveItems($items))));
10+
}
11+
12+
private function resolveItems(array $items): array
13+
{
14+
return $items;
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\Tests\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector\Fixture;
23+
24+
final class NestedExpression
25+
{
26+
public function run(array $items)
27+
{
28+
return trim((string) array_key_first($this->resolveItems($items)));
29+
}
30+
31+
private function resolveItems(array $items): array
32+
{
33+
return $items;
34+
}
35+
}
36+
37+
?>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector\Fixture;
4+
5+
final class SkipArrayKeysWithSearchValue
6+
{
7+
public function run(array $items)
8+
{
9+
// array_keys() with a search value returns only the matching keys
10+
$firstMatchingKey = current(array_keys($items, 'value'));
11+
$lastMatchingKey = end(array_keys($items, 'value', true));
12+
13+
return [$firstMatchingKey, $lastMatchingKey];
14+
}
15+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector\Fixture;
4+
5+
final class SkipOtherCalls
6+
{
7+
public function run(array $items, array $keys)
8+
{
9+
// not array_keys()
10+
$first = current(array_values($items));
11+
12+
// no inner call at all
13+
$stillFirst = current($keys);
14+
15+
// key() of the keys array is always 0, not the first key
16+
$zero = key(array_keys($items));
17+
18+
// first class callable
19+
$callable = current(...);
20+
21+
// spread
22+
$spread = current(...array_keys($items));
23+
24+
return [$first, $stillFirst, $zero, $callable, $spread];
25+
}
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector;
7+
8+
return RectorConfig::configure()
9+
->withRules([ArrayKeysToArrayKeyFirstLastRector::class]);
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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

Comments
 (0)