-
-
Notifications
You must be signed in to change notification settings - Fork 699
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Downgrade 7.3] Add DowngradeArrayKeyFirstLastRector (#6137)
Co-authored-by: kaizen-ci <info@kaizen-ci.org>
- Loading branch information
1 parent
a07e49c
commit 219b45c
Showing
6 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...Rector/FuncCall/DowngradeArrayKeyFirstLastRector/DowngradeArrayKeyFirstLastRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...wngrade73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/array_key_last.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
...s/Downgrade73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...s/Downgrade73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
rules/Downgrade73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |