[Downgrade 7.3] Add DowngradeArrayKeyFirstLastRector (#6137)

Co-authored-by: kaizen-ci <info@kaizen-ci.org>
This commit is contained in:
Tomas Votruba 2021-04-14 18:08:07 +02:00 committed by GitHub
parent a07e49c43b
commit 219b45c096
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 196 additions and 1 deletions

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\Downgrade73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class DowngradeArrayKeyFirstLastRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Rector\Tests\Downgrade73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector\Fixture;
class SomeClass
{
public function run($items)
{
$lastItemKey = array_key_last($items);
}
}
?>
-----
<?php
namespace Rector\Tests\Downgrade73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector\Fixture;
class SomeClass
{
public function run($items)
{
end($items);
$lastItemKey = key($items);
}
}
?>

View File

@ -0,0 +1,28 @@
<?php
namespace Rector\Tests\Downgrade73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector\Fixture;
class SomeClass
{
public function run($items)
{
$firstItemKey = array_key_first($items);
}
}
?>
-----
<?php
namespace Rector\Tests\Downgrade73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector\Fixture;
class SomeClass
{
public function run($items)
{
reset($items);
$firstItemKey = key($items);
}
}
?>

View File

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
use Rector\Downgrade73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeArrayKeyFirstLastRector::class);
};

View File

@ -7,6 +7,5 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigura
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ArrayKeyFirstLastRector::class);
};

View File

@ -0,0 +1,96 @@
<?php
declare(strict_types=1);
namespace Rector\Downgrade73\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://wiki.php.net/rfc/array_key_first_last
*
* @see \Rector\Tests\Downgrade73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector\DowngradeArrayKeyFirstLastRectorTest
*/
final class DowngradeArrayKeyFirstLastRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Downgrade array_key_first() and array_key_last() functions', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($items)
{
$firstItemKey = array_key_first($items);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($items)
{
reset($items);
$firstItemKey = key($items);
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if ($this->isName($node, 'array_key_first')) {
return $this->refactorArrayKeyFirst($node);
}
if ($this->isName($node, 'array_key_last')) {
return $this->refactorArrayKeyLast($node);
}
return null;
}
private function refactorArrayKeyFirst(FuncCall $funcCall): FuncCall
{
$array = $funcCall->args[0]->value;
$resetFuncCall = $this->nodeFactory->createFuncCall('reset', [$array]);
$this->addNodeBeforeNode($resetFuncCall, $funcCall);
$funcCall->name = new Name('key');
return $funcCall;
}
private function refactorArrayKeyLast(FuncCall $funcCall): FuncCall
{
$array = $funcCall->args[0]->value;
$resetFuncCall = $this->nodeFactory->createFuncCall('end', [$array]);
$this->addNodeBeforeNode($resetFuncCall, $funcCall);
$funcCall->name = new Name('key');
return $funcCall;
}
}