Skip to content

Commit

Permalink
[Downgrade 7.3] Add DowngradeArrayKeyFirstLastRector (#6137)
Browse files Browse the repository at this point in the history
Co-authored-by: kaizen-ci <info@kaizen-ci.org>
  • Loading branch information
TomasVotruba and kaizen-ci authored Apr 14, 2021
1 parent a07e49c commit 219b45c
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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';
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

?>
Original file line number Diff line number Diff line change
@@ -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);
}
}

?>
Original file line number Diff line number Diff line change
@@ -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);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->set(ArrayKeyFirstLastRector::class);
};
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 219b45c

Please sign in to comment.