Skip to content

Commit

Permalink
[DowngradePhp71] Add DowngradeKeysInListRector (#6170)
Browse files Browse the repository at this point in the history
Co-authored-by: kaizen-ci <info@kaizen-ci.org>
  • Loading branch information
samsonasik and kaizen-ci authored Apr 20, 2021
1 parent deafaf9 commit cd3725a
Show file tree
Hide file tree
Showing 18 changed files with 686 additions and 34 deletions.
2 changes: 2 additions & 0 deletions config/set/downgrade-php71.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rector\DowngradePhp71\Rector\ClassConst\DowngradeClassConstantVisibilityRector;
use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeNullableTypeDeclarationRector;
use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeVoidTypeDeclarationRector;
use Rector\DowngradePhp71\Rector\List_\DowngradeKeysInListRector;
use Rector\DowngradePhp71\Rector\String_\DowngradeNegativeStringOffsetToStrlenRector;
use Rector\DowngradePhp71\Rector\TryCatch\DowngradePipeToMultiCatchExceptionRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
Expand All @@ -23,4 +24,5 @@
$services->set(DowngradePipeToMultiCatchExceptionRector::class);
$services->set(SymmetricArrayDestructuringToListRector::class);
$services->set(DowngradeNegativeStringOffsetToStrlenRector::class);
$services->set(DowngradeKeysInListRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class DowngradeKeysInListRectorTest 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,39 @@
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class AsForeachValue
{
public function run(): void
{
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
foreach ($data as list("id" => $id1, "name" => $name1)) {
}
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class AsForeachValue
{
public function run(): void
{
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
foreach ($data as $singleData) {
$id1 = $singleData["id"];
$name1 = $singleData["name"];
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class AsForeachValue2
{
public function run(): void
{
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
foreach ($data as list("id" => $id1, "name" => $name1)) {
echo 'statement';
}
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class AsForeachValue2
{
public function run(): void
{
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
foreach ($data as $singleData) {
$id1 = $singleData["id"];
$name1 = $singleData["name"];
echo 'statement';
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class AsForeachValueKeyedArray
{
public function run(): void
{
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
foreach ($data as ["id" => $id1, "name" => $name1]) {
}
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class AsForeachValueKeyedArray
{
public function run(): void
{
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
foreach ($data as $singleData) {
$id1 = $singleData["id"];
$name1 = $singleData["name"];
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class AsForeachValueUsed
{
public function run($singleData): void
{
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
foreach ($data as list("id" => $id1, "name" => $name1)) {
echo $singleData;
}
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class AsForeachValueUsed
{
public function run($singleData): void
{
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
foreach ($data as $singleData1) {
$id1 = $singleData1["id"];
$name1 = $singleData1["name"];
echo $singleData;
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class Fixture
{
public function run(): void
{
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
list("id" => $id1, "name" => $name1) = $data[0];
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class Fixture
{
public function run(): void
{
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
$id1 = $data[0]["id"];
$name1 = $data[0]["name"];
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class MirrorComments
{
public function run(): void
{
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
// a comment
list("id" => $id1, "name" => $name1) = $data[0];
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class MirrorComments
{
public function run(): void
{
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
// a comment
$id1 = $data[0]["id"];
$name1 = $data[0]["name"];
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class SkipNotKeysInList
{
public function run(): void
{
$data = ['foo', 'bar', 'baz'];
list($foo, $bar, $baz) = $data;
}
}

?>

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class SkipNotKeysInList2
{
public function run(): void
{
$data = ['foo', 'bar', 'baz'];
list(, $bar, $baz) = $data;
}
}

?>

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class SkipNotKeysInList3
{
public function run(): void
{
$data = ['foo', 'bar', 'baz'];
list($bar, $baz, ) = $data;
}
}

?>

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class UseKeyedArray
{
public function run(): void
{
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
["id" => $id1, "name" => $name1] = $data[0];
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp71\Rector\List_\DowngradeKeysInListRector\Fixture;

class UseKeyedArray
{
public function run(): void
{
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
$id1 = $data[0]["id"];
$name1 = $data[0]["name"];
}
}

?>
Loading

0 comments on commit cd3725a

Please sign in to comment.