55namespace Rector \Php70 \Rector \Assign ;
66
77use PhpParser \Node ;
8- use PhpParser \Node \ArrayItem ;
9- use PhpParser \Node \Expr ;
10- use PhpParser \Node \Expr \ArrayDimFetch ;
118use 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 ;
1511use Rector \Rector \AbstractRector ;
1612use Rector \ValueObject \PhpVersionFeature ;
1713use Rector \VersionBonding \Contract \MinPhpVersionInterface ;
1814use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
1915use 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