Skip to content

Commit

Permalink
Fix incorrect formatting in alternative syntax constructs
Browse files Browse the repository at this point in the history
- Fix issue where assignments in alternative syntax constructs are not
  aligned when `align-data` is enabled
- Fix issue where alignment is incorrectly applied to default value
  assignments in parameter lists with no leading newline when
  `align-data` is enabled
- Fix issue where alternative syntax constructs may not be indented
  correctly when `align-lists` is enabled
- Fix issue where blank lines may not be added before `yield` statements
  in alternative syntax constructs when `blank-before-return` is enabled
- Fix issue where blank lines before and after comments are incorrectly
  suppressed in alternative syntax constructs
  • Loading branch information
lkrms committed Jan 8, 2025
1 parent 3c9e20a commit 8e2bee8
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 24 deletions.
4 changes: 2 additions & 2 deletions docs/Rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions src/Rule/AlignData.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ public function boot(): void
* When they appear in the same scope, a callback is registered to align
* consecutive:
*
* - assignment operators
* - assignment operators (except as noted below)
* - `=>` delimiters in array syntax (except as noted below)
* - `=>` delimiters in `match` expressions
*
* If the open bracket of an array is not followed by a newline and neither
* `AlignLists` nor `StrictLists` are enabled, its `=>` delimiters are
* ignored.
* If the open bracket of an array or parameter list is not followed by a
* newline and neither `AlignLists` nor `StrictLists` are enabled, its `=>`
* delimiters or assignment operators are ignored.
*
* @prettyphp-callback Assignment operators are aligned unless the
* formatter's `MaxAssignmentPadding` property is not `null` and would be
Expand Down Expand Up @@ -112,7 +112,13 @@ public function processBlock(array $lines): void
(
!$token->Parent
|| $token->Parent->Flags & TokenFlag::STRUCTURAL_BRACE
|| $token->Parent->isParameterList()
|| $token->Parent->id === T_COLON
|| (
$token->Parent->isParameterList() && (
$this->ListRuleEnabled
|| $token->Parent->hasNewlineBeforeNextCode()
)
)
)
// Ignore assignment operators after the first:
// - in the statement
Expand Down
22 changes: 18 additions & 4 deletions src/Rule/AlignLists.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,30 @@ static function () use (
if ($parent->CloseBracket) {
// `]` after `2` -> `]` after `5`
while (($adjacent = $to->lastSiblingBeforeNewline()) !== $to && !(
$adjacent->id === \T_OPEN_BRACE
&& $adjacent->Flags & TokenFlag::STRUCTURAL_BRACE
(
(
$adjacent->id === \T_OPEN_BRACE
&& $adjacent->Flags & TokenFlag::STRUCTURAL_BRACE
) || (
$adjacent->id === T_COLON
&& $adjacent->CloseBracket
)
)
&& $adjacent->Depth <= $parent->Depth
)) {
$to = $adjacent;
}
// `]` after `5` -> `+` -> `)` after `11`
while (($adjacent = $to->adjacentBeforeNewline()) && !(
$adjacent->id === \T_OPEN_BRACE
&& $adjacent->Flags & TokenFlag::STRUCTURAL_BRACE
(
(
$adjacent->id === \T_OPEN_BRACE
&& $adjacent->Flags & TokenFlag::STRUCTURAL_BRACE
) || (
$adjacent->id === T_COLON
&& $adjacent->CloseBracket
)
)
&& $adjacent->Depth <= $parent->Depth
)) {
$to = TokenUtil::getOperatorEndExpression($adjacent);
Expand Down
5 changes: 4 additions & 1 deletion src/Rule/BlankBeforeReturn.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ public function processTokens(array $tokens): void
if (
$token->Statement !== $token || (
$token->Parent
&& !($token->Parent->Flags & TokenFlag::STRUCTURAL_BRACE)
&& !(
$token->Parent->Flags & TokenFlag::STRUCTURAL_BRACE
|| $token->Parent->id === T_COLON
)
)
) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/HangingIndentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function processTokens(array $tokens): void
)
) || (
$token->id === \T_COLON
&& $token->isColonAltSyntaxDelimiter()
&& $token->CloseBracket
) || $token->id === \T_OPEN_UNENCLOSED;
$flags = 0;
if ($token->hasNewlineBeforeNextCode()) {
Expand Down
10 changes: 7 additions & 3 deletions src/Rule/PreserveNewlines.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,10 @@ private function maybePreserveNewlineAfter(
&& $prevCode->Parent === $parent
&& $prevCode->EndStatement !== $prevCode
) || (
$parent
&& !($parent->Flags & TokenFlag::STRUCTURAL_BRACE)
$parent && !(
$parent->Flags & TokenFlag::STRUCTURAL_BRACE
|| $parent->id === T_COLON
)
)
)
) || (
Expand All @@ -258,8 +260,10 @@ private function maybePreserveNewlineAfter(
&& $prevCode->EndStatement !== $prevCode
) || (
$next->Parent
&& !($next->Parent->Flags & TokenFlag::STRUCTURAL_BRACE)
&& !(
$next->Parent->Flags & TokenFlag::STRUCTURAL_BRACE
|| $next->Parent->id === T_COLON
) && !(
$next->Parent->id === \T_OPEN_BRACE
&& $next->Parent->isMatchOpenBrace()
&& ($prevCode = $next->PrevCode)
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Preset/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function processTokens(array $tokens): void
}

if ($token->id === \T_COLON) {
if (!$token->isColonAltSyntaxDelimiter()) {
if (!$token->CloseBracket) {
continue;
}
$token->applyWhitespace(Space::SPACE_BEFORE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ while ( have_posts() ) :
$images = twentyten_get_gallery_images();
if ( $images ) :
$total_images = count( $images );
$image = reset( $images );
$image = reset( $images );
?>
<div class="gallery-thumb">
<a class="size-thumbnail" href="<?php the_permalink(); ?>"><?php echo wp_get_attachment_image( $image, 'thumbnail' ); ?></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ function page_attributes_meta_box( $post ) {
* @param WP_Post $post The current post.
*/
$dropdown_args = apply_filters( 'page_attributes_dropdown_pages_args', $dropdown_args, $post );
$pages = wp_dropdown_pages( $dropdown_args );
$pages = wp_dropdown_pages( $dropdown_args );
if ( ! empty( $pages ) ) :
?>
<p class="post-attributes-label-wrapper parent-id-label-wrapper"><label class="post-attributes-label" for="parent_id"><?php _e( 'Parent' ); ?></label></p>
Expand Down
Loading

0 comments on commit 8e2bee8

Please sign in to comment.