Skip to content

Commit fd5a1cf

Browse files
committed
BracesFixer - fix invalid code generation on alternative syntax
1 parent a4df718 commit fd5a1cf

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

src/Fixer/Basic/BracesFixer.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,8 @@ private function fixMissingControlBraces(Tokens $tokens)
613613
}
614614

615615
$parenthesisEndIndex = $this->findParenthesisEnd($tokens, $index);
616-
$tokenAfterParenthesis = $tokens[$tokens->getNextMeaningfulToken($parenthesisEndIndex)];
616+
$nextAfterParenthesisEndIndex = $tokens->getNextMeaningfulToken($parenthesisEndIndex);
617+
$tokenAfterParenthesis = $tokens[$nextAfterParenthesisEndIndex];
617618

618619
// if Token after parenthesis is { then we do not need to insert brace, but to fix whitespace before it
619620
if ($tokenAfterParenthesis->equals('{') && self::LINE_SAME === $this->configuration['position_after_control_structures']) {
@@ -629,6 +630,19 @@ private function fixMissingControlBraces(Tokens $tokens)
629630
continue;
630631
}
631632

633+
// do not add for short 'if' followed by alternative loop,
634+
// for example: if ($a) while ($b): ? > X < ?php endwhile; ? >
635+
if ($tokenAfterParenthesis->isGivenKind([T_FOR, T_FOREACH, T_SWITCH, T_WHILE])) {
636+
$tokenAfterParenthesisBlockEnd = $tokens->findBlockEnd( // go to ')'
637+
Tokens::BLOCK_TYPE_PARENTHESIS_BRACE,
638+
$tokens->getNextMeaningfulToken($nextAfterParenthesisEndIndex)
639+
);
640+
641+
if ($tokens[$tokens->getNextMeaningfulToken($tokenAfterParenthesisBlockEnd)]->equals(':')) {
642+
continue;
643+
}
644+
}
645+
632646
$statementEndIndex = $this->findStatementEnd($tokens, $parenthesisEndIndex);
633647

634648
// insert closing brace

tests/Fixer/Basic/BracesFixerTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5263,4 +5263,83 @@ function example()
52635263
}'
52645264
);
52655265
}
5266+
5267+
/**
5268+
* @param string $expected
5269+
* @param null|string $input
5270+
*
5271+
* @dataProvider provideFixAlternativeSyntaxCases
5272+
*/
5273+
public function testFixAlternativeSyntax($expected, $input = null)
5274+
{
5275+
$this->doTest($expected, $input);
5276+
}
5277+
5278+
public function provideFixAlternativeSyntaxCases()
5279+
{
5280+
yield [
5281+
'<?php if (foo()) {
5282+
while (bar()) {
5283+
}
5284+
}',
5285+
'<?php if (foo()) while (bar()) {}',
5286+
];
5287+
5288+
yield [
5289+
'<?php if ($a) {
5290+
foreach ($b as $c) {
5291+
}
5292+
}',
5293+
'<?php if ($a) foreach ($b as $c) {}',
5294+
];
5295+
5296+
yield [
5297+
'<?php if ($a) foreach ($b as $c): ?> X <?php endforeach; ?>',
5298+
];
5299+
5300+
yield [
5301+
'<?php if ($a) while ($b): ?> X <?php endwhile; ?>',
5302+
];
5303+
5304+
yield [
5305+
'<?php if ($a) for (;;): ?> X <?php endfor; ?>',
5306+
];
5307+
5308+
yield [
5309+
'<?php if ($a) switch ($a): case 1: ?> X <?php endswitch; ?>',
5310+
];
5311+
5312+
yield [
5313+
'<?php if ($a): elseif ($b): for (;;): ?> X <?php endfor; endif; ?>',
5314+
];
5315+
5316+
yield [
5317+
'<?php switch ($a): case 1: for (;;): ?> X <?php endfor; endswitch; ?>,',
5318+
];
5319+
5320+
yield [
5321+
'<?php
5322+
if ($a) foreach ($b as $c): ?>
5323+
<?php if ($a) for (;;): ?>
5324+
<?php if ($a) foreach ($b as $c): ?>
5325+
<?php if ($a) for (;;): ?>
5326+
<?php if ($a) while ($b): ?>
5327+
<?php if ($a) while ($b): ?>
5328+
<?php if ($a) foreach ($b as $c): ?>
5329+
<?php if ($a) for (;;): ?>
5330+
<?php if ($a) while ($b): ?>
5331+
<?php if ($a) while ($b): ?>
5332+
5333+
<?php endwhile; ?>
5334+
<?php endwhile; ?>
5335+
<?php endfor; ?>
5336+
<?php endforeach; ?>
5337+
<?php endwhile; ?>
5338+
<?php endwhile; ?>
5339+
<?php endfor; ?>
5340+
<?php endforeach; ?>
5341+
<?php endfor; ?>
5342+
<?php endforeach; ?>',
5343+
];
5344+
}
52665345
}

0 commit comments

Comments
 (0)