Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions crates/formatter/src/internal/format/member_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ impl<'arena> MemberAccessChain<'arena> {
return true;
}

if accesses_len <= 3 {
if accesses_len <= 3 && !f.in_condition {
// we must have at least 2 breaks for chains of length 3 or less
return false;
}
Expand Down Expand Up @@ -581,9 +581,10 @@ pub(super) fn print_member_access_chain<'arena>(
let mut last_element_end = member_access_chain.base.span().end;
// Handle the first access

if should_inline_first_access(f, member_access_chain)
&& let Some((_, first_chain_link)) = accesses_iter.next()
{
let should_inline = should_inline_first_access(f, member_access_chain)
|| (f.in_condition && member_access_chain.accesses.len() <= 3);

if should_inline && let Some((_, first_chain_link)) = accesses_iter.next() {
// Format the base object and first method call together
parts.push(format_access_operator(
f,
Expand Down Expand Up @@ -642,7 +643,11 @@ pub(super) fn print_member_access_chain<'arena>(
}

if must_break {
parts.push(Document::Indent(contents));
if f.in_condition {
parts.push(Document::Array(contents));
} else {
parts.push(Document::Indent(contents));
}
} else {
parts.push(Document::IndentIfBreak(IndentIfBreak::new(group_id, contents)));
}
Expand Down
47 changes: 47 additions & 0 deletions crates/formatter/tests/cases/if_multiline_operator/after.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

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

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

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

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

if (
$node->bundle() == 'organization_profile'
&& \Drupal::service('example_recruiter_verification.example_recruiter_verification_helper')
->isRecruiterVerificationEnabled()
) {
$a = $b;
}

if (
$node->bundle() == 'organization_profile'
&& \Drupal::service('example_recruiter_verification.example_recruiter_verification_helper')
->somePublicProperty
) {
$a = $b;
}

if (
$node->bundle() == 'organization_profile'
&& $object->method('example_recruiter_verification.example_recruiter_verification_helper')
->somePublicProperty
) {
$a = $b;
}
38 changes: 38 additions & 0 deletions crates/formatter/tests/cases/if_multiline_operator/before.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

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

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

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

if ($node->bundle() == 'organization_profile' && \Drupal::service('example_recruiter_verification.example_recruiter_verification_helper')
->isRecruiterVerificationEnabled()) {
$a = $b;
}

if ($node->bundle() == 'organization_profile' && \Drupal::service('example_recruiter_verification.example_recruiter_verification_helper')
->somePublicProperty) {
$a = $b;
}

if ($node->bundle() == 'organization_profile' && $object->method('example_recruiter_verification.example_recruiter_verification_helper')
->somePublicProperty) {
$a = $b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FormatSettings {
preserve_breaking_conditional_expression: true,
preserve_breaking_member_access_chain: true,
..Default::default()
}
1 change: 1 addition & 0 deletions crates/formatter/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ test_case!(idempotent_chain_with_array);
test_case!(match_idempotency);
test_case!(heredoc_indentation);
test_case!(heredoc_indentation_disabled);
test_case!(if_multiline_operator);

// A special test case for regressions in the Psl codebase
test_case!(psl_regressions);
Expand Down
Loading