Skip to content

fix(member_access): Fix multi line indentation in if clause#901

Open
klausi wants to merge 2 commits intocarthage-software:mainfrom
klausi:if_multiline_operator
Open

fix(member_access): Fix multi line indentation in if clause#901
klausi wants to merge 2 commits intocarthage-software:mainfrom
klausi:if_multiline_operator

Conversation

@klausi
Copy link
Contributor

@klausi klausi commented Jan 16, 2026

📌 What Does This PR Do?

Fixes an issue where chained method calls inside control structure conditions (like if statements) were being formatted with incorrect indentation (double indent).

🔍 Context & Motivation

When a method chain (e.g., $this->foo()->bar()) appeared inside an if condition, the formatter was applying two levels of indentation: one from the condition wrapper and one from the chain formatter itself. Additionally, heuristics for "short chains" were preventing proper inlining, causing readable code to break awkwardly across multiple lines.

This change ensures that chains inside conditions respect the parent indentation context and inline short chains appropriately, matching the expected output in the if_multiline_operator test case and general PHP coding standards.

This test code:

if (!$this->windows && !$this->getSession()
    ->getDriver() instanceof BrowserKitDriver) {
    if (!$this->getSession()->isStarted()) {
        $this->getSession()->start();
    }

    $this->windows = $this->getSession()->getWindowNames();
}

with settings

preserve-breaking-conditional-expression = true
preserve-breaking-member-access-chain = true

is wrongly formatted to:

if (!$this->windows && !$this->getSession()
        ->getDriver() instanceof BrowserKitDriver) {
    if (!$this->getSession()->isStarted()) {
        $this->getSession()->start();
    }

    $this->windows = $this->getSession()->getWindowNames();
}

🛠️ Summary of Changes

  • Bug Fix: Prevented double indentation for member access chains when they appear inside a condition context.
  • Bug Fix: Adjusted is_already_broken logic to allow short chains (length <= 3) to be formatted as chains when inside conditions.
  • Bug Fix: Updated print_member_access_chain to force inlining of the first access for short chains in conditions, ensuring $object->method() stays together on the first line where possible.

📂 Affected Areas

  • Formatter

🔗 Related Issues or PRs

📝 Notes for Reviewers

  • The logic specifically checks FormatterState.in_condition to apply these relaxations only where appropriate (inside if(...), while(...), etc.).
  • Verified that all formatter tests pass without regressions.
  • Not sure about the approach, let me know if there is a better way.

@klausi
Copy link
Contributor Author

klausi commented Jan 16, 2026

Added some more test cases, did not find proper solution yet.

@azjezz azjezz added c-formatter PHP code reformatting and style rules t-bug Something isn't working labels Feb 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c-formatter PHP code reformatting and style rules t-bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants