diff --git a/rules-tests/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector/Fixture/skip_as_nested_assign.php.inc b/rules-tests/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector/Fixture/skip_as_nested_assign.php.inc new file mode 100644 index 00000000000..2c5efa8170a --- /dev/null +++ b/rules-tests/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector/Fixture/skip_as_nested_assign.php.inc @@ -0,0 +1,13 @@ +var->setAttribute(AttributeKey::IS_ASSIGN_OP_VAR, true); + $this->markAssignOpVar($node->var); return null; } @@ -54,4 +56,19 @@ public function enterNode(Node $node): ?Node return null; } + + /** + * Marks the whole dim fetch chain, not just the outermost node: $array[$key][0] += 1 writes + * to $array[$key] as well, so a rule that rewrites the inner fetch would change a write + * into a read of a temporary value. + */ + private function markAssignOpVar(Expr $expr): void + { + $expr->setAttribute(AttributeKey::IS_ASSIGN_OP_VAR, true); + + while ($expr instanceof ArrayDimFetch) { + $expr = $expr->var; + $expr->setAttribute(AttributeKey::IS_ASSIGN_OP_VAR, true); + } + } }