Skip to content

Commit

Permalink
feat: StatementIndentationFixer - support comment for continuous cont…
Browse files Browse the repository at this point in the history
…rol statement (#7384)
  • Loading branch information
keradus committed Oct 27, 2023
1 parent 3a60ff1 commit 753d0af
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 18 deletions.
27 changes: 26 additions & 1 deletion src/Fixer/Whitespace/StatementIndentationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,32 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
(null !== $firstNonWhitespaceTokenIndex && $firstNonWhitespaceTokenIndex < $endIndex)
|| (null !== $nextNewlineIndex && $nextNewlineIndex < $endIndex)
) {
$indent = true;
if (
// do we touch whitespace directly before comment...
$tokens[$firstNonWhitespaceTokenIndex]->isGivenKind(T_COMMENT)
// ...and afterwards, there is only comment or `}`
&& $tokens[$tokens->getNextMeaningfulToken($firstNonWhitespaceTokenIndex)]->equals('}')
) {
if (
// ... and the comment was only content in docblock
$tokens[$tokens->getPrevMeaningfulToken($firstNonWhitespaceTokenIndex)]->equals('{')
) {
$indent = true;
} else {
// or it was dedicated comment for next control loop
// ^^ we need to check if there is a control group afterwards, and in that case don't make extra indent level
$nextIndex = $tokens->getNextMeaningfulToken($firstNonWhitespaceTokenIndex);
$nextNextIndex = $tokens->getNextMeaningfulToken($nextIndex);

if (null !== $nextNextIndex && $tokens[$nextNextIndex]->isGivenKind([T_ELSE, T_ELSEIF])) {
$indent = false;
} else {
$indent = true;
}
}
} else {
$indent = true;
}
}
}

Expand Down
39 changes: 31 additions & 8 deletions tests/Fixer/Basic/BracesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ public static function provideFixMissingBracesAndIndentCases(): iterable
$a = "a";
} elseif (3) {
$b = "b";
// comment
// comment
} else {
$c = "c";
}
Expand Down Expand Up @@ -1898,7 +1898,7 @@ public function __construct(
$a = "a";
} elseif (3) {
$b = "b";
// comment
// comment
} else {
$c = "c";
}
Expand All @@ -1908,17 +1908,40 @@ public function __construct(
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];

yield 'multiline comment in block' => [
'<?php
if (1) {
$b = "a";
// multiline comment line 1
// multiline comment line 2
} elseif (2) {
// empty
} else {
$c = "b";
}',
'<?php
if (1) {
$b = "a";
// multiline comment line 1
// multiline comment line 2
} elseif (2) {
// empty
} else {
$c = "b";
}',
];

yield [
'<?php
if (1) {
if (2) {
$a = "a";
} elseif (3) {
$b = "b";
// comment line 1
// comment line 2
// comment line 3
// comment line 4
// comment line 1
// comment line 2
// comment line 3
// comment line 4
} else {
$c = "c";
}
Expand Down Expand Up @@ -3222,10 +3245,10 @@ public static function provideFixCommentBeforeBraceCases(): iterable
// 2.5+ API
if (isNewApi()) {
echo "new API";
// 2.4- API
// 2.4- API
} elseif (isOldApi()) {
echo "old API";
// 2.4- API
// other API
} else {
echo "unknown API";
// sth
Expand Down
123 changes: 114 additions & 9 deletions tests/Fixer/Whitespace/StatementIndentationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -776,15 +776,6 @@ public function foo(
;',
];

yield 'if with only a comment and followed by else' => [
'<?php
if (true) {
// foo
} else {
// bar
}',
];

yield 'multiple anonymous functions as function arguments' => [
'<?php
foo(function () {
Expand Down Expand Up @@ -924,8 +915,122 @@ public function foo() {
);',
];

yield 'if with only a comment and followed by else' => [
'<?php
if (true) {
// foo
} else {
// bar
}',
'<?php
if (true) {
// foo
} else {
// bar
}',
];

yield 'comment before else blocks' => [
'<?php
// foo
if ($foo) {
echo "foo";
// bar
} else {
$aaa = 1;
}',
'<?php
// foo
if ($foo) {
echo "foo";
// bar
} else {
$aaa = 1;
}',
];

yield 'multiline comment in block - describing next block' => [
'<?php
if (1) {
$b = "a";
// multiline comment line 1
// multiline comment line 2
// multiline comment line 3
} else {
$c = "b";
}',
'<?php
if (1) {
$b = "a";
// multiline comment line 1
// multiline comment line 2
// multiline comment line 3
} else {
$c = "b";
}',
];

yield 'multiline comment in block - the only content in block' => [
'<?php
if (1) {
// multiline comment line 1
// multiline comment line 2
// multiline comment line 3
} else {
$c = "b";
}',
'<?php
if (1) {
// multiline comment line 1
// multiline comment line 2
// multiline comment line 3
} else {
$c = "b";
}',
];

yield 'comment before elseif blocks' => [
'<?php
// foo
if ($foo) {
echo "foo";
// bar
} elseif(1) {
echo "bar";
} elseif(2) {
// do nothing
} elseif(3) {
$aaa = 1;
// end comment in final block
}',
'<?php
// foo
if ($foo) {
echo "foo";
// bar
} elseif(1) {
echo "bar";
} elseif(2) {
// do nothing
} elseif(3) {
$aaa = 1;
// end comment in final block
}',
];

yield 'comments at the end of if/elseif/else blocks' => [
'<?php
if ($foo) {
echo "foo";
// foo
} elseif ($bar) {
echo "bar";
// bar
} else {
echo "baz";
// baz
}',
'<?php
if ($foo) {
echo "foo";
// foo
Expand Down

0 comments on commit 753d0af

Please sign in to comment.