diff --git a/crates/formatter/src/internal/format/member_access.rs b/crates/formatter/src/internal/format/member_access.rs index 8091a14d..bd5be6e8 100644 --- a/crates/formatter/src/internal/format/member_access.rs +++ b/crates/formatter/src/internal/format/member_access.rs @@ -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; } @@ -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, @@ -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))); } diff --git a/crates/formatter/tests/cases/if_multiline_operator/after.php b/crates/formatter/tests/cases/if_multiline_operator/after.php new file mode 100644 index 00000000..8ff0b72c --- /dev/null +++ b/crates/formatter/tests/cases/if_multiline_operator/after.php @@ -0,0 +1,47 @@ +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; +} diff --git a/crates/formatter/tests/cases/if_multiline_operator/before.php b/crates/formatter/tests/cases/if_multiline_operator/before.php new file mode 100644 index 00000000..d9d4b4c8 --- /dev/null +++ b/crates/formatter/tests/cases/if_multiline_operator/before.php @@ -0,0 +1,38 @@ +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; +} diff --git a/crates/formatter/tests/cases/if_multiline_operator/settings.inc b/crates/formatter/tests/cases/if_multiline_operator/settings.inc new file mode 100644 index 00000000..ac5c50ff --- /dev/null +++ b/crates/formatter/tests/cases/if_multiline_operator/settings.inc @@ -0,0 +1,5 @@ +FormatSettings { + preserve_breaking_conditional_expression: true, + preserve_breaking_member_access_chain: true, + ..Default::default() +} diff --git a/crates/formatter/tests/mod.rs b/crates/formatter/tests/mod.rs index b70e9c33..608f6265 100644 --- a/crates/formatter/tests/mod.rs +++ b/crates/formatter/tests/mod.rs @@ -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);