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); + } +}