Skip to content

Format-preserving printer loses newline between attributes and method declaration for visibility mutation #1117

@maks-rafalko

Description

@maks-rafalko

Environment

Description

At Infection, we do a lot of nodes replacements during the Mutation Testing process.

For example, we replace public function ... with protected function ... class method visibility modifier.

When mutating the method visibility, the format-preserving printer incorrectly removes the newline between the last attribute and the method declaration.

Expected Behavior

The format-preserving printer should maintain the newline between attributes and the method signature:

#[SomeAttribute1]
#[SomeAttribute2]
public function &foo(int $param, $test = 1): bool

Actual Behavior

The newline is lost, causing the last attribute and method declaration to be on the same line:

#[SomeAttribute1]
#[SomeAttribute2]protected function &foo(int $param, $test = 1): bool
                ^ missing newline here

Mutation

$stmts[0]->stmts[0]->flags = ($stmts[0]->stmts[0]->flags & ~\PhpParser\Modifiers::PUBLIC) | \PhpParser\Modifiers::PROTECTED;

Related to

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions