Skip to content

Commit f5ec53a

Browse files
authored
[Php70] Deprecate ListSwapArrayOrderRector, can turn valid code into broken code (#8477)
1 parent d20c8cf commit f5ec53a

11 files changed

Lines changed: 9 additions & 187 deletions

File tree

‎config/set/php-version-based.php‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
3535
use Rector\Php56\Rector\FuncCall\PowToExpRector;
3636
use Rector\Php70\Rector\Assign\ListSplitStringRector;
37-
use Rector\Php70\Rector\Assign\ListSwapArrayOrderRector;
3837
use Rector\Php70\Rector\Break_\BreakNotInLoopOrSwitchToReturnRector;
3938
use Rector\Php70\Rector\ClassMethod\Php4ConstructorRector;
4039
use Rector\Php70\Rector\FuncCall\CallUserMethodRector;
@@ -195,7 +194,6 @@
195194
MultiDirnameRector::class,
196195
ListSplitStringRector::class,
197196
EmptyListRector::class,
198-
ListSwapArrayOrderRector::class,
199197
CallUserMethodRector::class,
200198
EregToPregMatchRector::class,
201199
ReduceMultipleDefaultSwitchRector::class,

‎config/set/php70.php‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Rector\Config\RectorConfig;
66
use Rector\Php70\Rector\Assign\ListSplitStringRector;
7-
use Rector\Php70\Rector\Assign\ListSwapArrayOrderRector;
87
use Rector\Php70\Rector\Break_\BreakNotInLoopOrSwitchToReturnRector;
98
use Rector\Php70\Rector\ClassMethod\Php4ConstructorRector;
109
use Rector\Php70\Rector\FuncCall\CallUserMethodRector;
@@ -31,7 +30,6 @@
3130
MultiDirnameRector::class,
3231
ListSplitStringRector::class,
3332
EmptyListRector::class,
34-
ListSwapArrayOrderRector::class,
3533
CallUserMethodRector::class,
3634
EregToPregMatchRector::class,
3735
ReduceMultipleDefaultSwitchRector::class,

‎rules-tests/Php70/Rector/Assign/ListSwapArrayOrderRector/Fixture/fixture.php.inc‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎rules-tests/Php70/Rector/Assign/ListSwapArrayOrderRector/Fixture/missing_list_item.php.inc‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎rules-tests/Php70/Rector/Assign/ListSwapArrayOrderRector/Fixture/skip.php.inc‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎rules-tests/Php70/Rector/Assign/ListSwapArrayOrderRector/Fixture/skip_already_array_reverse.php.inc‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎rules-tests/Php70/Rector/Assign/ListSwapArrayOrderRector/Fixture/skip_array_dim.php.inc‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎rules-tests/Php70/Rector/Assign/ListSwapArrayOrderRector/ListSwapArrayOrderRectorTest.php‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

‎rules-tests/Php70/Rector/Assign/ListSwapArrayOrderRector/config/configured_rule.php‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎rules/Php70/Rector/Assign/ListSwapArrayOrderRector.php‎

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,20 @@
55
namespace Rector\Php70\Rector\Assign;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\ArrayItem;
9-
use PhpParser\Node\Expr;
10-
use PhpParser\Node\Expr\ArrayDimFetch;
118
use PhpParser\Node\Expr\Assign;
12-
use PhpParser\Node\Expr\FuncCall;
13-
use PhpParser\Node\Expr\List_;
14-
use Rector\PhpParser\Printer\BetterStandardPrinter;
9+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
10+
use Rector\Exception\ShouldNotHappenException;
1511
use Rector\Rector\AbstractRector;
1612
use Rector\ValueObject\PhpVersionFeature;
1713
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
1814
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1915
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2016

2117
/**
22-
* @see \Rector\Tests\Php70\Rector\Assign\ListSwapArrayOrderRector\ListSwapArrayOrderRectorTest
18+
* @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.
2319
*/
24-
final class ListSwapArrayOrderRector extends AbstractRector implements MinPhpVersionInterface
20+
final class ListSwapArrayOrderRector extends AbstractRector implements MinPhpVersionInterface, DeprecatedInterface
2521
{
26-
public function __construct(
27-
private readonly BetterStandardPrinter $betterStandardPrinter
28-
) {
29-
}
30-
3122
public function getRuleDefinition(): RuleDefinition
3223
{
3324
return new RuleDefinition(
@@ -49,50 +40,14 @@ public function getNodeTypes(): array
4940
*/
5041
public function refactor(Node $node): ?Node
5142
{
52-
if ($this->shouldSkipAssign($node)) {
53-
return null;
54-
}
55-
56-
/** @var List_ $list */
57-
$list = $node->var;
58-
59-
$printedVariables = [];
60-
foreach ($list->items as $arrayItem) {
61-
if (! $arrayItem instanceof ArrayItem) {
62-
continue;
63-
}
64-
65-
if ($arrayItem->value instanceof ArrayDimFetch && ! $arrayItem->value->dim instanceof Expr) {
66-
$printedVariables[] = $this->betterStandardPrinter->print($arrayItem->value->var);
67-
} else {
68-
return null;
69-
}
70-
}
71-
72-
// relevant only in 1 variable type
73-
$uniqueVariables = array_unique($printedVariables);
74-
if (count($uniqueVariables) !== 1) {
75-
return null;
76-
}
77-
78-
// wrap with array_reverse, to reflect reverse assign order in left
79-
$node->expr = $this->nodeFactory->createFuncCall('array_reverse', [$node->expr]);
80-
81-
return $node;
43+
throw new ShouldNotHappenException(sprintf(
44+
'"%s" rule is deprecated, as it can turn valid code into broken code',
45+
self::class
46+
));
8247
}
8348

8449
public function provideMinPhpVersion(): int
8550
{
8651
return PhpVersionFeature::LIST_SWAP_ORDER;
8752
}
88-
89-
private function shouldSkipAssign(Assign $assign): bool
90-
{
91-
if (! $assign->var instanceof List_) {
92-
return true;
93-
}
94-
95-
// already converted
96-
return $assign->expr instanceof FuncCall && $this->isName($assign->expr, 'array_reverse');
97-
}
9853
}

0 commit comments

Comments
 (0)