Skip to content

[printer] Keep blank lines between statements when a node list is emptied - #8886

Merged
TomasVotruba merged 2 commits into
mainfrom
preserve-blank-lines-on-emptied-implements-list
Oct 29, 2026
Merged

TomasVotruba merged 2 commits into
mainfrom
preserve-blank-lines-on-emptied-implements-list

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Problem

When a rule empties a node list - most commonly removing the last implements interface from a class - php-parser's format-preserving printer gives up on that node (PrettyPrinterAbstract::pArray() has // TODO: Support removing single node) and falls back to a full reprint of the parent. That full reprint drops the blank lines between the class statements.

Example - removing the interface collapses the spacing between methods:

class SomeClass implements SomeInterface
{
    public function first()
    {
    }

    public function second()
    {
    }
}

became:

class SomeClass
{
    public function first()
    {
    }
    public function second()
    {
    }
}

This surfaced in EventSubscriberToAttributeRector (rector-symfony), which drops EventSubscriberInterface while converting to #[AsEventListener].

Fix

BetterStandardPrinter::pArray() now handles the "whole list removed" case php-parser bails on: it cuts just the emptied clause (keyword + types) from the original tokens and keeps the rest format-preserved, so the surrounding statements keep their original blank lines. Covered keywords: implements (class, enum) and interface extends.

Guards: only fires when the new list is empty and the original was not, the keyword token is actually present, and no PHP tag sits in the removed range.

Test

testKeepBlankLinesBetweenMethodsWhenImplementsRemoved parses a class, empties implements, prints format-preserving and asserts the blank line between methods survives. It fails on main and passes with this change.

@TomasVotruba
TomasVotruba marked this pull request as draft October 29, 2026 08:27
@TomasVotruba
TomasVotruba marked this pull request as ready for review October 29, 2026 08:37
@TomasVotruba TomasVotruba changed the title Keep blank lines between statements when a node list is emptied [printer] Keep blank lines between statements when a node list is emptied Oct 29, 2026
@TomasVotruba
TomasVotruba merged commit 4e317b2 into main Oct 29, 2026
57 of 59 checks passed
@TomasVotruba
TomasVotruba deleted the preserve-blank-lines-on-emptied-implements-list branch October 29, 2026 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants