mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 19:53:14 +01:00
Merge pull request #3457 from rectorphp/php72-improve
[PHP 7.2] Various improvements in ListEach and WhileEach Rectors
This commit is contained in:
commit
e9183e5980
@ -18,7 +18,7 @@ use Rector\NodeTypeResolver\Node\AttributeKey;
|
|||||||
/**
|
/**
|
||||||
* @source https://wiki.php.net/rfc/deprecations_php_7_2#each
|
* @source https://wiki.php.net/rfc/deprecations_php_7_2#each
|
||||||
*
|
*
|
||||||
* @see \Rector\Php72\Tests\Rector\Each\EachRectorTest
|
* @see \Rector\Php72\Tests\Rector\Each\ListEachRector\ListEachRectorTest
|
||||||
*/
|
*/
|
||||||
final class ListEachRector extends AbstractRector
|
final class ListEachRector extends AbstractRector
|
||||||
{
|
{
|
||||||
|
@ -19,7 +19,7 @@ use Rector\Core\RectorDefinition\RectorDefinition;
|
|||||||
/**
|
/**
|
||||||
* @source https://wiki.php.net/rfc/deprecations_php_7_2#each
|
* @source https://wiki.php.net/rfc/deprecations_php_7_2#each
|
||||||
*
|
*
|
||||||
* @see \Rector\Php72\Tests\Rector\Each\EachRectorTest
|
* @see \Rector\Php72\Tests\Rector\Each\WhileEachToForeachRector\WhileEachToForeachRectorTest
|
||||||
*/
|
*/
|
||||||
final class WhileEachToForeachRector extends AbstractRector
|
final class WhileEachToForeachRector extends AbstractRector
|
||||||
{
|
{
|
||||||
@ -110,9 +110,12 @@ PHP
|
|||||||
|
|
||||||
// is key included? add it to foreach
|
// is key included? add it to foreach
|
||||||
if (count($listNode->items) > 0) {
|
if (count($listNode->items) > 0) {
|
||||||
/** @var ArrayItem $keyItem */
|
/** @var ArrayItem|null $keyItem */
|
||||||
$keyItem = array_pop($listNode->items);
|
$keyItem = array_pop($listNode->items);
|
||||||
$foreachNode->keyVar = $keyItem->value;
|
|
||||||
|
if ($keyItem !== null) {
|
||||||
|
$foreachNode->keyVar = $keyItem->value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $foreachNode;
|
return $foreachNode;
|
||||||
|
@ -2,8 +2,7 @@
|
|||||||
|
|
||||||
namespace Rector\Php72\Tests\Rector\Each\Fixture;
|
namespace Rector\Php72\Tests\Rector\Each\Fixture;
|
||||||
|
|
||||||
function each2
|
function each2()
|
||||||
()
|
|
||||||
{
|
{
|
||||||
list($key, $val) = each($opt->option);
|
list($key, $val) = each($opt->option);
|
||||||
|
|
||||||
@ -18,8 +17,7 @@ function each2
|
|||||||
|
|
||||||
namespace Rector\Php72\Tests\Rector\Each\Fixture;
|
namespace Rector\Php72\Tests\Rector\Each\Fixture;
|
||||||
|
|
||||||
function each2
|
function each2()
|
||||||
()
|
|
||||||
{
|
{
|
||||||
$key = key($opt->option);
|
$key = key($opt->option);
|
||||||
$val = current($opt->option);
|
$val = current($opt->option);
|
@ -2,19 +2,18 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Rector\Php72\Tests\Rector\Each;
|
namespace Rector\Php72\Tests\Rector\Each\ListEachRector;
|
||||||
|
|
||||||
use Iterator;
|
use Iterator;
|
||||||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||||
use Rector\Php72\Rector\Each\ListEachRector;
|
use Rector\Php72\Rector\Each\ListEachRector;
|
||||||
use Rector\Php72\Rector\Each\WhileEachToForeachRector;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test battery inspired by:
|
* Test battery inspired by:
|
||||||
* - https://stackoverflow.com/q/46492621/1348344 + Drupal refactorings
|
* - https://stackoverflow.com/q/46492621/1348344 + Drupal refactorings
|
||||||
* - https://stackoverflow.com/a/51278641/1348344
|
* - https://stackoverflow.com/a/51278641/1348344
|
||||||
*/
|
*/
|
||||||
final class EachRectorTest extends AbstractRectorTestCase
|
final class ListEachRectorTest extends AbstractRectorTestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @dataProvider provideData()
|
* @dataProvider provideData()
|
||||||
@ -29,14 +28,8 @@ final class EachRectorTest extends AbstractRectorTestCase
|
|||||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected function getRectorClass(): string
|
||||||
* @return mixed[]
|
|
||||||
*/
|
|
||||||
protected function getRectorsWithConfiguration(): array
|
|
||||||
{
|
{
|
||||||
return [
|
return ListEachRector::class;
|
||||||
WhileEachToForeachRector::class => [],
|
|
||||||
ListEachRector::class => [],
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,9 +4,6 @@ namespace Rector\Php72\Tests\Rector\Each\Fixture;
|
|||||||
|
|
||||||
function each1()
|
function each1()
|
||||||
{
|
{
|
||||||
while (list($module) = each($module_list)) {
|
|
||||||
}
|
|
||||||
|
|
||||||
while (list($key, $callback) = each($callbacks)) {
|
while (list($key, $callback) = each($callbacks)) {
|
||||||
// comment
|
// comment
|
||||||
}
|
}
|
||||||
@ -26,9 +23,6 @@ namespace Rector\Php72\Tests\Rector\Each\Fixture;
|
|||||||
|
|
||||||
function each1()
|
function each1()
|
||||||
{
|
{
|
||||||
foreach (array_keys($module_list) as $module) {
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($callbacks as $key => $callback) {
|
foreach ($callbacks as $key => $callback) {
|
||||||
// comment
|
// comment
|
||||||
}
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rector\Php72\Tests\Rector\Each\WhileEachToForeachRector\Fixture;
|
||||||
|
|
||||||
|
use Rector\Core\Testing\Contract\RunnableInterface;
|
||||||
|
|
||||||
|
final class PartialListInWhile implements RunnableInterface
|
||||||
|
{
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
$iteratedLines = [];
|
||||||
|
$lines = ['a', 'b'];
|
||||||
|
|
||||||
|
while (list(, $line) = each($lines)) {
|
||||||
|
$iteratedLines[] = $line;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $iteratedLines;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
-----
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rector\Php72\Tests\Rector\Each\WhileEachToForeachRector\Fixture;
|
||||||
|
|
||||||
|
use Rector\Core\Testing\Contract\RunnableInterface;
|
||||||
|
|
||||||
|
final class PartialListInWhile implements RunnableInterface
|
||||||
|
{
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
$iteratedLines = [];
|
||||||
|
$lines = ['a', 'b'];
|
||||||
|
|
||||||
|
foreach ($lines as $line) {
|
||||||
|
$iteratedLines[] = $line;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $iteratedLines;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Rector\Php72\Tests\Rector\Each\WhileEachToForeachRector;
|
||||||
|
|
||||||
|
use Iterator;
|
||||||
|
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||||
|
use Rector\Php72\Rector\Each\WhileEachToForeachRector;
|
||||||
|
|
||||||
|
final class WhileEachToForeachRectorTest extends AbstractRectorTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @dataProvider provideData()
|
||||||
|
*/
|
||||||
|
public function test(string $file): void
|
||||||
|
{
|
||||||
|
$this->doTestFile($file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideData(): Iterator
|
||||||
|
{
|
||||||
|
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getRectorClass(): string
|
||||||
|
{
|
||||||
|
return WhileEachToForeachRector::class;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user