-
-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add SplitListScalarAssignToSeparateLineRector
- Loading branch information
1 parent
7559395
commit 00797bb
Showing
10 changed files
with
314 additions
and
3 deletions.
There are no files selected for viewing
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
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
146 changes: 146 additions & 0 deletions
146
rules/code-quality/src/Rector/Assign/SplitListAssignToSeparateLineRector.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,146 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Rector\Assign; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\List_; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\RectorDefinition\CodeSample; | ||
use Rector\Core\RectorDefinition\RectorDefinition; | ||
|
||
/** | ||
* @see https://mobile.twitter.com/ivanhoe011/status/1246376872931401728 | ||
* | ||
* @see \Rector\CodeQuality\Tests\Rector\Assign\SplitListAssignToSeparateLineRector\SplitListAssignToSeparateLineRectorTest | ||
*/ | ||
final class SplitListAssignToSeparateLineRector extends AbstractRector | ||
{ | ||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition('Splits [$a, $b] = [5, 10] scalar assign to standalone lines', [ | ||
new CodeSample( | ||
<<<'PHP' | ||
final class SomeClass | ||
{ | ||
public function run(): void | ||
{ | ||
[$a, $b] = [1, 2]; | ||
} | ||
} | ||
PHP | ||
, | ||
<<<'PHP' | ||
final class SomeClass | ||
{ | ||
public function run(): void | ||
{ | ||
$a = 1; | ||
$b = 2; | ||
} | ||
} | ||
PHP | ||
|
||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Assign::class]; | ||
} | ||
|
||
/** | ||
* @param Assign $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($this->shouldSkip($node)) { | ||
return null; | ||
} | ||
|
||
/** @var Array_|List_ $leftArray */ | ||
$leftArray = $node->var; | ||
|
||
/** @var Array_ $rightArray */ | ||
$rightArray = $node->expr; | ||
|
||
$standaloneAssigns = $this->createStandaloneAssigns($leftArray, $rightArray); | ||
foreach ($standaloneAssigns as $standaloneAssign) { | ||
$this->addNodeAfterNode($standaloneAssign, $node); | ||
} | ||
|
||
$this->removeNode($node); | ||
|
||
return $node; | ||
} | ||
|
||
private function shouldSkip($node): bool | ||
{ | ||
if (! $node->var instanceof Array_ && ! $node->var instanceof List_) { | ||
return true; | ||
} | ||
|
||
if (! $node->expr instanceof Array_) { | ||
return true; | ||
} | ||
|
||
if (count($node->var->items) !== count($node->expr->items)) { | ||
return true; | ||
} | ||
|
||
// is value swap | ||
return $this->isValueSwap($node->var, $node->expr); | ||
} | ||
|
||
/** | ||
* @param Array_|List_ $node | ||
* @return Assign[] | ||
*/ | ||
private function createStandaloneAssigns(Node $node, Array_ $rightArray): array | ||
{ | ||
$standaloneAssigns = []; | ||
foreach ($node->items as $key => $leftArrayItem) { | ||
$rightArrayItem = $rightArray->items[$key]; | ||
|
||
$standaloneAssigns[] = new Assign($leftArrayItem->value, $rightArrayItem); | ||
} | ||
|
||
return $standaloneAssigns; | ||
} | ||
|
||
/** | ||
* @param Array_|List_ $node | ||
*/ | ||
private function getArrayItemsHash(Node $node): string | ||
{ | ||
$arrayItemsHashes = []; | ||
foreach ($node->items as $arrayItem) { | ||
$arrayItemsHashes[] = $this->printWithoutComments($arrayItem); | ||
} | ||
|
||
sort($arrayItemsHashes); | ||
|
||
$arrayItemsHash = implode('', $arrayItemsHashes); | ||
|
||
return sha1($arrayItemsHash); | ||
} | ||
|
||
/** | ||
* @param Array_|List_ $firstArray | ||
* @param Array_|List_ $secondArray | ||
*/ | ||
private function isValueSwap($firstArray, $secondArray): bool | ||
{ | ||
$firstArrayItemsHash = $this->getArrayItemsHash($firstArray); | ||
$secondArrayItemsHash = $this->getArrayItemsHash($secondArray); | ||
|
||
return $firstArrayItemsHash === $secondArrayItemsHash; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...e-quality/tests/Rector/Assign/SplitListAssignToSeparateLineRector/Fixture/fixture.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\CodeQuality\Tests\Rector\Assign\SplitListAssignToSeparateLineRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public function run(): void | ||
{ | ||
[$a, $b] = [1, 2]; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\Assign\SplitListAssignToSeparateLineRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public function run(): void | ||
{ | ||
$a = 1; | ||
$b = 2; | ||
} | ||
} | ||
|
||
?> |
30 changes: 30 additions & 0 deletions
30
...uality/tests/Rector/Assign/SplitListAssignToSeparateLineRector/Fixture/not_scalar.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,30 @@ | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\Assign\SplitListAssignToSeparateLineRector\Fixture; | ||
|
||
final class NotScalar | ||
{ | ||
public function run(): void | ||
{ | ||
$itemCount = 100; | ||
[$a, $b] = [1, $itemCount]; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\Assign\SplitListAssignToSeparateLineRector\Fixture; | ||
|
||
final class NotScalar | ||
{ | ||
public function run(): void | ||
{ | ||
$itemCount = 100; | ||
$a = 1; | ||
$b = $itemCount; | ||
} | ||
} | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
...sts/Rector/Assign/SplitListAssignToSeparateLineRector/Fixture/skip_split_of_array.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,12 @@ | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\Assign\SplitListAssignToSeparateLineRector\Fixture; | ||
|
||
final class SkipSplitOfArray | ||
{ | ||
public function run(): void | ||
{ | ||
$moreItems = [1, 2]; | ||
[$a, $b] = $moreItems; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...quality/tests/Rector/Assign/SplitListAssignToSeparateLineRector/Fixture/skip_swap.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,13 @@ | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\Assign\SplitListAssignToSeparateLineRector\Fixture; | ||
|
||
final class SkipSwap | ||
{ | ||
public function run(): void | ||
{ | ||
$a = 1; | ||
$b = 5; | ||
[$a, $b] = [$b, $a]; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ests/Rector/Assign/SplitListAssignToSeparateLineRector/Fixture/with_list_function.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\CodeQuality\Tests\Rector\Assign\SplitListAssignToSeparateLineRector\Fixture; | ||
|
||
final class WithListFunction | ||
{ | ||
public function run(): void | ||
{ | ||
list($a, $b) = [1, 2]; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\Assign\SplitListAssignToSeparateLineRector\Fixture; | ||
|
||
final class WithListFunction | ||
{ | ||
public function run(): void | ||
{ | ||
$a = 1; | ||
$b = 2; | ||
} | ||
} | ||
|
||
?> |
30 changes: 30 additions & 0 deletions
30
...or/Assign/SplitListAssignToSeparateLineRector/SplitListAssignToSeparateLineRectorTest.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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\Assign\SplitListAssignToSeparateLineRector; | ||
|
||
use Iterator; | ||
use Rector\CodeQuality\Rector\Assign\SplitListAssignToSeparateLineRector; | ||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class SplitListAssignToSeparateLineRectorTest 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 SplitListAssignToSeparateLineRector::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