diff --git a/config/level/php/php70.yml b/config/level/php/php70.yml index 3c8fd38d0cf..c2cb1ccceba 100644 --- a/config/level/php/php70.yml +++ b/config/level/php/php70.yml @@ -5,3 +5,7 @@ services: Rector\Php\Rector\FunctionLike\ExceptionHandlerTypehintRector: ~ Rector\Php\Rector\FuncCall\MultiDirnameRector: ~ Rector\Php\Rector\List_\ListSplitStringRector: ~ + Rector\Php\Rector\List_\EmptyListRector: ~ + + # be careful, run this just once, since it can keep swapping order back and forth + Rector\Php\Rector\List_\ListSwapArrayOrderRector: ~ diff --git a/packages/Php/src/Rector/List_/ListSwapArrayOrderRector.php b/packages/Php/src/Rector/List_/ListSwapArrayOrderRector.php new file mode 100644 index 00000000000..fef6fef6304 --- /dev/null +++ b/packages/Php/src/Rector/List_/ListSwapArrayOrderRector.php @@ -0,0 +1,79 @@ +betterStandardPrinter = $betterStandardPrinter; + } + + public function getDefinition(): RectorDefinition + { + return new RectorDefinition( + 'list() assigns variables in reverse order - relevant in array assign', + [new CodeSample('list($a[], $a[]) = [1, 2];', 'list($a[], $a[]) = array_reverse([1, 2])];')] + ); + } + + /** + * @return string[] + */ + public function getNodeTypes(): array + { + return [Assign::class]; + } + + /** + * @param Assign $assignNode + */ + public function refactor(Node $assignNode): ?Node + { + if (! $assignNode->var instanceof List_) { + return $assignNode; + } + + $printerVars = []; + + /** @var ArrayItem $item */ + foreach ($assignNode->var->items as $item) { + if ($item->value instanceof ArrayDimFetch) { + $printerVars[] = $this->betterStandardPrinter->prettyPrint([$item->value->var]); + } else { + return $assignNode; + } + } + + // relevant only in 1 variable type + if (count(array_unique($printerVars)) !== 1) { + return $assignNode; + } + + // wrap with array_reverse, to reflect reverse assign order in left + $assignNode->expr = new FuncCall(new Name('array_reverse'), [new Arg($assignNode->expr)]); + + return $assignNode; + } +} diff --git a/packages/Php/tests/Rector/List_/ListSwapArrayOrderRector/Correct/correct.php.inc b/packages/Php/tests/Rector/List_/ListSwapArrayOrderRector/Correct/correct.php.inc new file mode 100644 index 00000000000..c0db4c28f56 --- /dev/null +++ b/packages/Php/tests/Rector/List_/ListSwapArrayOrderRector/Correct/correct.php.inc @@ -0,0 +1,7 @@ +doTestFileMatchesExpectedContent($wrong, $fixed); + } + + public function provideWrongToFixedFiles(): Iterator + { + yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc']; + } + + protected function provideConfig(): string + { + return __DIR__ . '/config.yml'; + } +} diff --git a/packages/Php/tests/Rector/List_/ListSwapArrayOrderRector/Wrong/wrong.php.inc b/packages/Php/tests/Rector/List_/ListSwapArrayOrderRector/Wrong/wrong.php.inc new file mode 100644 index 00000000000..062ce692c4e --- /dev/null +++ b/packages/Php/tests/Rector/List_/ListSwapArrayOrderRector/Wrong/wrong.php.inc @@ -0,0 +1,7 @@ +