Skip to content

Commit 41033a6

Browse files
committed
[DowngradePhp85] Add DowngradeArrayFirstLastRector
1 parent 92b1b5b commit 41033a6

File tree

11 files changed

+196
-2
lines changed

11 files changed

+196
-2
lines changed

config/set/downgrade-php85.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector;
7+
use Rector\ValueObject\PhpVersion;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->phpVersion(PhpVersion::PHP_84);
11+
$rectorConfig->rules([
12+
DowngradeArrayFirstLastRector::class,
13+
]);
14+
};

config/set/level/down-to-php83.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6+
use Rector\Set\ValueObject\DowngradeLevelSetList;
67
use Rector\Set\ValueObject\DowngradeSetList;
78

89
return static function (RectorConfig $rectorConfig): void {
9-
$rectorConfig->sets([DowngradeSetList::PHP_84]);
10+
$rectorConfig->sets([DowngradeLevelSetList::DOWN_TO_PHP_84, DowngradeSetList::PHP_84]);
1011
};

config/set/level/down-to-php84.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Set\ValueObject\DowngradeSetList;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->sets([DowngradeSetList::PHP_85]);
10+
};
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\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DowngradeArrayFirstLastRectorTest 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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector\Fixture;
6+
7+
final class Fixture
8+
{
9+
public function run(array $array)
10+
{
11+
echo array_first($array);
12+
echo array_last($array);
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
declare(strict_types=1);
21+
22+
namespace Rector\Tests\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector\Fixture;
23+
24+
final class Fixture
25+
{
26+
public function run(array $array)
27+
{
28+
echo $array[array_key_first($array)];
29+
echo $array[array_key_last($array)];
30+
}
31+
}
32+
33+
?>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector\Fixture;
6+
7+
final class SkipDifferentFuncCall
8+
{
9+
public function run()
10+
{
11+
return strlen('test');
12+
}
13+
}
14+
15+
?>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(DowngradeArrayFirstLastRector::class);
10+
};

rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private function refactorArrayConstValue(Array_ $array): ?Array_
174174

175175
/**
176176
* @var ArrayItem $item
177-
* @var ClassConstFetch $value
177+
* @var ClassConstFetch
178178
* @var Identifier $name
179179
*/
180180
$value = $item->value;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DowngradePhp85\Rector\FuncCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\ArrayDimFetch;
9+
use PhpParser\Node\Expr\FuncCall;
10+
use Rector\Rector\AbstractRector;
11+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
12+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
13+
14+
/**
15+
* @see https://php.watch/versions/8.5/array_first-array_last
16+
* @see \Rector\Tests\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector\DowngradeArrayFirstLastRectorTest
17+
*/
18+
final class DowngradeArrayFirstLastRector extends AbstractRector
19+
{
20+
public function getNodeTypes(): array
21+
{
22+
return [FuncCall::class];
23+
}
24+
25+
public function getRuleDefinition(): RuleDefinition
26+
{
27+
return new RuleDefinition(
28+
'Replace array_first() and array_last() with $array[array_key_first($array)] and $array[array_key_last($array)]',
29+
[
30+
new CodeSample(
31+
<<<'CODE_SAMPLE'
32+
echo array_first($array);
33+
echo array_last($array);
34+
CODE_SAMPLE
35+
,
36+
<<<'CODE_SAMPLE'
37+
echo $array[array_key_first($array)];
38+
echo $array[array_key_last($array)];
39+
CODE_SAMPLE
40+
),
41+
]
42+
);
43+
}
44+
45+
/**
46+
* @param FuncCall $node
47+
*/
48+
public function refactor(Node $node): ?Node
49+
{
50+
if (! $this->isNames($node, ['array_first', 'array_last'])) {
51+
return null;
52+
}
53+
54+
if ($node->isFirstClassCallable()) {
55+
return null;
56+
}
57+
58+
$args = $node->getArgs();
59+
60+
if (count($args) !== 1) {
61+
return null;
62+
}
63+
64+
$functionName = $this->isName($node, 'array_first')
65+
? 'array_key_first'
66+
: 'array_key_last';
67+
68+
return new ArrayDimFetch(
69+
$args[0]->value,
70+
$this->nodeFactory->createFuncCall($functionName, [$args[0]->value])
71+
);
72+
}
73+
}

src/Set/ValueObject/DowngradeLevelSetList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
*/
1111
final class DowngradeLevelSetList
1212
{
13+
/**
14+
* @var string
15+
*/
16+
public const DOWN_TO_PHP_84 = __DIR__ . '/../../../config/set/level/down-to-php84.php';
17+
1318
/**
1419
* @var string
1520
*/

0 commit comments

Comments
 (0)