Skip to content

BracesFixer - handle comment for content outside of given block #3359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
37 changes: 36 additions & 1 deletion src/Fixer/Basic/BracesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,42 @@ function ($item) {
$whitespace = $nextWhitespace.$this->whitespacesConfig->getLineEnding().$indent;

if (!$nextNonWhitespaceNestToken->equals('}')) {
$whitespace .= $this->whitespacesConfig->getIndent();
$determineIsIndentableBlockContent = function ($contentIndex) use ($tokens) {
if (!$tokens[$contentIndex]->isComment()) {
return true;
}

if (!$tokens[$tokens->getPrevMeaningfulToken($contentIndex)]->equals(';')) {
return true;
}

$nextIndex = $tokens->getNextMeaningfulToken($contentIndex);

if (!$tokens[$nextIndex]->equals('}')) {
return true;
}

$nextNextIndex = $tokens->getNextMeaningfulToken($nextIndex);

if (null === $nextNextIndex) {
return true;
}

if ($tokens[$nextNextIndex]->equalsAny(array(
array(T_ELSE),
array(T_ELSEIF),
',',
))) {
return false;
}

return true;
};

// add extra indent only if current content is not a comment for content outside of current block
if ($determineIsIndentableBlockContent($nestIndex + 2)) {
$whitespace .= $this->whitespacesConfig->getIndent();
}
}
}

Expand Down
85 changes: 53 additions & 32 deletions tests/Fixer/Basic/BracesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ public function provideFixMissingBracesAndIndentCases()
$a = "a";
} elseif (3) {
$b = "b";
// comment
// comment
} else {
$c = "c";
}
Expand Down Expand Up @@ -1685,7 +1685,7 @@ public function __construct(
$a = "a";
} elseif (3) {
$b = "b";
// comment
// comment
} else {
$c = "c";
}
Expand Down Expand Up @@ -2778,6 +2778,27 @@ public function provideFixCommentBeforeBraceCases()
};',
self::$configurationOopPositionSameLine,
),
array(
'<?php
// 2.5+ API
if (isNewApi()) {
echo "new API";
// 2.4- API
} elseif (isOldApi()) {
echo "old API";
// 2.4- API
} else {
echo "unknown API";
// sth
}

return $this->guess($class, $property, function (Constraint $constraint) use ($guesser) {
return $guesser->guessRequiredForConstraint($constraint);
// Fallback to false...
// ... due to sth...
}, false);
',
),
);
}

Expand Down Expand Up @@ -4071,6 +4092,36 @@ public function provideFixWithAllowOnelineLambdaCases()
);
}

/**
* @param string $expected
* @param null|string $input
*
* @dataProvider provideDoWhileLoopInsideAnIfWithoutBracketsCases
*/
public function testDoWhileLoopInsideAnIfWithoutBrackets($expected, $input = null)
{
$this->doTest($expected, $input);
}

public function provideDoWhileLoopInsideAnIfWithoutBracketsCases()
{
return array(
array(
'<?php
if (true) {
do {
echo 1;
} while (false);
}',
'<?php
if (true)
do {
echo 1;
} while (false);',
),
);
}

/**
* @param string $expected
* @param null|string $input
Expand Down Expand Up @@ -4123,34 +4174,4 @@ public function provideMessyWhitespacesCases()
),
);
}

public function provideDoWhileLoopInsideAnIfWithoutBracketsCases()
{
return array(
array(
'<?php
if (true) {
do {
echo 1;
} while (false);
}',
'<?php
if (true)
do {
echo 1;
} while (false);',
),
);
}

/**
* @param string $expected
* @param null|string $input
*
* @dataProvider provideDoWhileLoopInsideAnIfWithoutBracketsCases
*/
public function testDoWhileLoopInsideAnIfWithoutBrackets($expected, $input = null)
{
$this->doTest($expected, $input);
}
}