Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions config/set/php-version-based.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\Php56\Rector\FuncCall\PowToExpRector;
use Rector\Php70\Rector\Assign\ListSplitStringRector;
use Rector\Php70\Rector\Assign\ListSwapArrayOrderRector;
use Rector\Php70\Rector\Break_\BreakNotInLoopOrSwitchToReturnRector;
use Rector\Php70\Rector\ClassMethod\Php4ConstructorRector;
use Rector\Php70\Rector\FuncCall\CallUserMethodRector;
Expand Down Expand Up @@ -195,7 +194,6 @@
MultiDirnameRector::class,
ListSplitStringRector::class,
EmptyListRector::class,
ListSwapArrayOrderRector::class,
CallUserMethodRector::class,
EregToPregMatchRector::class,
ReduceMultipleDefaultSwitchRector::class,
Expand Down
2 changes: 0 additions & 2 deletions config/set/php70.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Rector\Config\RectorConfig;
use Rector\Php70\Rector\Assign\ListSplitStringRector;
use Rector\Php70\Rector\Assign\ListSwapArrayOrderRector;
use Rector\Php70\Rector\Break_\BreakNotInLoopOrSwitchToReturnRector;
use Rector\Php70\Rector\ClassMethod\Php4ConstructorRector;
use Rector\Php70\Rector\FuncCall\CallUserMethodRector;
Expand All @@ -31,7 +30,6 @@
MultiDirnameRector::class,
ListSplitStringRector::class,
EmptyListRector::class,
ListSwapArrayOrderRector::class,
CallUserMethodRector::class,
EregToPregMatchRector::class,
ReduceMultipleDefaultSwitchRector::class,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

61 changes: 8 additions & 53 deletions rules/Php70/Rector/Assign/ListSwapArrayOrderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,20 @@
namespace Rector\Php70\Rector\Assign;

use PhpParser\Node;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\List_;
use Rector\PhpParser\Printer\BetterStandardPrinter;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\Php70\Rector\Assign\ListSwapArrayOrderRector\ListSwapArrayOrderRectorTest
* @deprecated This rule is deprecated, as it can turn valid code into broken code. The reverse assign order only matters for the same array variable, but the rule cannot reliably tell apart independent assigns, so wrapping with array_reverse() can change correct behavior.
*/
final class ListSwapArrayOrderRector extends AbstractRector implements MinPhpVersionInterface
final class ListSwapArrayOrderRector extends AbstractRector implements MinPhpVersionInterface, DeprecatedInterface
{
public function __construct(
private readonly BetterStandardPrinter $betterStandardPrinter
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand All @@ -49,50 +40,14 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkipAssign($node)) {
return null;
}

/** @var List_ $list */
$list = $node->var;

$printedVariables = [];
foreach ($list->items as $arrayItem) {
if (! $arrayItem instanceof ArrayItem) {
continue;
}

if ($arrayItem->value instanceof ArrayDimFetch && ! $arrayItem->value->dim instanceof Expr) {
$printedVariables[] = $this->betterStandardPrinter->print($arrayItem->value->var);
} else {
return null;
}
}

// relevant only in 1 variable type
$uniqueVariables = array_unique($printedVariables);
if (count($uniqueVariables) !== 1) {
return null;
}

// wrap with array_reverse, to reflect reverse assign order in left
$node->expr = $this->nodeFactory->createFuncCall('array_reverse', [$node->expr]);

return $node;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as it can turn valid code into broken code',
self::class
));
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::LIST_SWAP_ORDER;
}

private function shouldSkipAssign(Assign $assign): bool
{
if (! $assign->var instanceof List_) {
return true;
}

// already converted
return $assign->expr instanceof FuncCall && $this->isName($assign->expr, 'array_reverse');
}
}
2 changes: 1 addition & 1 deletion src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ private function unwrapFileNode(array $stmts): array
}

/**
* @param Node[] $nodes
* @param array<Node|null> $nodes Null in case of array destructuring
*/
private function containsNop(array $nodes): bool
{
Expand Down
Loading