From 41e9bc9072d1be9eee3f4a25f2c79fc51bb40632 Mon Sep 17 00:00:00 2001 From: Maks Rafalko Date: Tue, 14 Oct 2025 21:21:49 +0200 Subject: [PATCH] Replacing class method visibility doesn't work without previous node's attributes --- .../classMethodModifier.test | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 test/code/formatPreservation/classMethodModifier.test diff --git a/test/code/formatPreservation/classMethodModifier.test b/test/code/formatPreservation/classMethodModifier.test new file mode 100644 index 0000000000..3e1159b649 --- /dev/null +++ b/test/code/formatPreservation/classMethodModifier.test @@ -0,0 +1,77 @@ +Replace class method modifier `public` with `protected` without keeping node attributes +----- + $values + * @return list + */ + public function makeAList(array $values): array + { + // some comment + + $strings = ['1']; + + $ints = array_map(function ($value): int { + return (int) $value; + }, $strings); + + $nonEmptyArray = ['1']; + + $nonEmptyArrayFromMethod = $this->returnNonEmptyArray(); + + $inlineNonEmpty = ['1']; + + return array_values($values); + } +} +----- +$node = $stmts[0]->stmts[0]->stmts[0]; +$stmts[0]->stmts[0]->stmts[0] = new \PhpParser\Node\Stmt\ClassMethod( + $node->name, + [ + 'flags' => ($node->flags & ~\PhpParser\Modifiers::PUBLIC) | \PhpParser\Modifiers::PROTECTED, + 'byRef' => $node->returnsByRef(), + 'params' => $node->getParams(), + 'returnType' => $node->getReturnType(), + 'stmts' => $node->getStmts(), + 'attrGroups' => $node->getAttrGroups(), + ], + // $node->getAttributes(), <---- does not work without previous node's attributes +); +----- + $values + * @return list + */ + protected function makeAList(array $values): array + { + // some comment + + $strings = ['1']; + + $ints = array_map(function ($value): int { + return (int) $value; + }, $strings); + + $nonEmptyArray = ['1']; + + $nonEmptyArrayFromMethod = $this->returnNonEmptyArray(); + + $inlineNonEmpty = ['1']; + + return array_values($values); + } +}