Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 69a4fc5

Browse files
committed
feature #2684 BracesFixer - new options for braces position after control structures and anonymous constructs (aidantwoods, keradus)
This PR was squashed before being merged into the 2.4-dev branch (closes #2684). Discussion ---------- BracesFixer - new options for braces position after control structures and anonymous constructs Personally, I prefer my braces to be consistent across classes, functions, and control structures. This adds an extra option to apply those fixes which is not enabled by default. > Whether the opening brace should be placed on "next" or "same" line after control structures not covered by position_after_functions_and_oop_constructs. Commits ------- 8c6d50c BracesFixer - new options for braces position after control structures and anonymous constructs
2 parents ff2236b + 8c6d50c commit 69a4fc5

3 files changed

Lines changed: 1150 additions & 67 deletions

File tree

README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@ Choose from the list of available rules:
260260

261261
- ``allow_single_line_closure`` (``bool``): whether single line lambda notation
262262
should be allowed; defaults to ``false``
263+
- ``position_after_anonymous_constructs`` (``'next'``, ``'same'``): whether the
264+
opening brace should be placed on "next" or "same" line after anonymous
265+
constructs (anonymous classes and lambda functions); defaults to ``'same'``
266+
- ``position_after_control_structures`` (``'next'``, ``'same'``): whether the opening
267+
brace should be placed on "next" or "same" line after control
268+
structures; defaults to ``'same'``
263269
- ``position_after_functions_and_oop_constructs`` (``'next'``, ``'same'``): whether
264270
the opening brace should be placed on "next" or "same" line after
265271
classy constructs (non-anonymous classes, interfaces, traits, methods

src/Fixer/Basic/BracesFixer.php

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@ protected function createConfigurationDefinition()
164164
->setAllowedValues([self::LINE_NEXT, self::LINE_SAME])
165165
->setDefault(self::LINE_NEXT)
166166
->getOption(),
167+
(new FixerOptionBuilder('position_after_control_structures', 'whether the opening brace should be placed on "next" or "same" line after control structures.'))
168+
->setAllowedValues([self::LINE_NEXT, self::LINE_SAME])
169+
->setDefault(self::LINE_SAME)
170+
->getOption(),
171+
(new FixerOptionBuilder('position_after_anonymous_constructs', 'whether the opening brace should be placed on "next" or "same" line after anonymous constructs (anonymous classes and lambda functions).'))
172+
->setAllowedValues([self::LINE_NEXT, self::LINE_SAME])
173+
->setDefault(self::LINE_SAME)
174+
->getOption(),
167175
]);
168176
}
169177

@@ -240,7 +248,13 @@ private function fixControlContinuationBraces(Tokens $tokens)
240248
continue;
241249
}
242250

243-
$tokens->ensureWhitespaceAtIndex($index - 1, 1, ' ');
251+
$tokens->ensureWhitespaceAtIndex(
252+
$index - 1,
253+
1,
254+
self::LINE_NEXT === $this->configuration['position_after_control_structures'] ?
255+
$this->whitespacesConfig->getLineEnding().$this->detectIndent($tokens, $index)
256+
: ' '
257+
);
244258
}
245259
}
246260

@@ -362,11 +376,13 @@ function ($item) {
362376
!($nestToken->equals('}') && $tokens[$nestIndex - 1]->equalsAny(['"', "'", [T_CONSTANT_ENCAPSED_STRING]]))
363377
) {
364378
if (
365-
$nextNonWhitespaceNestToken->isGivenKind($this->getControlContinuationTokens())
379+
self::LINE_NEXT !== $this->configuration['position_after_control_structures']
380+
&& $nextNonWhitespaceNestToken->isGivenKind($this->getControlContinuationTokens())
366381
|| $nextNonWhitespaceNestToken->isGivenKind(T_CLOSE_TAG)
367382
|| (
368-
$nextNonWhitespaceNestToken->isGivenKind(T_WHILE) &&
369-
$tokensAnalyzer->isWhilePartOfDoWhile($nextNonWhitespaceNestIndex)
383+
self::LINE_NEXT !== $this->configuration['position_after_control_structures']
384+
&& $nextNonWhitespaceNestToken->isGivenKind(T_WHILE)
385+
&& $tokensAnalyzer->isWhilePartOfDoWhile($nextNonWhitespaceNestIndex)
370386
)
371387
) {
372388
$whitespace = ' ';
@@ -436,19 +452,43 @@ function ($item) {
436452
}
437453

438454
$tokens->ensureWhitespaceAtIndex($startBraceIndex - 1, 1, $ensuredWhitespace);
439-
} elseif ($token->isGivenKind(T_FUNCTION) && !$tokensAnalyzer->isLambda($index)) {
455+
} elseif (
456+
$token->isGivenKind(T_FUNCTION) && !$tokensAnalyzer->isLambda($index)
457+
|| (
458+
self::LINE_NEXT === $this->configuration['position_after_control_structures'] && $token->isGivenKind($controlTokens)
459+
|| (
460+
self::LINE_NEXT === $this->configuration['position_after_anonymous_constructs']
461+
&& (
462+
$token->isGivenKind(T_FUNCTION) && $tokensAnalyzer->isLambda($index)
463+
|| $token->isGivenKind(T_CLASS) && $tokensAnalyzer->isAnonymousClass($index)
464+
)
465+
)
466+
)
467+
) {
468+
$isAnonymousClass = $token->isGivenKind($classyTokens) && $tokensAnalyzer->isAnonymousClass($index);
469+
440470
$closingParenthesisIndex = $tokens->getPrevTokenOfKind($startBraceIndex, [')']);
441-
if (null === $closingParenthesisIndex) {
471+
if (null === $closingParenthesisIndex && !$isAnonymousClass) {
442472
continue;
443473
}
444474

445-
$prevToken = $tokens[$closingParenthesisIndex - 1];
446-
if ($prevToken->isWhitespace() && false !== strpos($prevToken->getContent(), "\n")) {
475+
if (!$isAnonymousClass) {
476+
$prevToken = $tokens[$closingParenthesisIndex - 1];
477+
}
478+
479+
if (!$isAnonymousClass && $prevToken->isWhitespace() && false !== strpos($prevToken->getContent(), "\n")) {
447480
if (!$tokens[$startBraceIndex - 2]->isComment()) {
448481
$tokens->ensureWhitespaceAtIndex($startBraceIndex - 1, 1, ' ');
449482
}
450483
} else {
451-
if (self::LINE_SAME === $this->configuration['position_after_functions_and_oop_constructs'] && !$tokens[$tokens->getPrevNonWhitespace($startBraceIndex)]->isComment()) {
484+
if (
485+
self::LINE_SAME === $this->configuration['position_after_functions_and_oop_constructs']
486+
&& (
487+
$token->isGivenKind([T_FUNCTION]) && !$tokensAnalyzer->isLambda($index)
488+
|| $token->isGivenKind($classyTokens) && !$tokensAnalyzer->isAnonymousClass($index)
489+
)
490+
&& !$tokens[$tokens->getPrevNonWhitespace($startBraceIndex)]->isComment()
491+
) {
452492
$ensuredWhitespace = ' ';
453493
} else {
454494
$ensuredWhitespace = $this->whitespacesConfig->getLineEnding().$indent;
@@ -480,7 +520,7 @@ private function fixMissingControlBraces(Tokens $tokens)
480520
$tokenAfterParenthesis = $tokens[$tokens->getNextMeaningfulToken($parenthesisEndIndex)];
481521

482522
// if Token after parenthesis is { then we do not need to insert brace, but to fix whitespace before it
483-
if ($tokenAfterParenthesis->equals('{')) {
523+
if ($tokenAfterParenthesis->equals('{') && self::LINE_SAME === $this->configuration['position_after_control_structures']) {
484524
$tokens->ensureWhitespaceAtIndex($parenthesisEndIndex + 1, 0, ' ');
485525

486526
continue;
@@ -523,7 +563,13 @@ private function fixSpaceAroundToken(Tokens $tokens)
523563
$nextNonWhitespaceIndex = $tokens->getNextNonWhitespace($index);
524564

525565
if (!$tokens[$nextNonWhitespaceIndex]->equals(':')) {
526-
$tokens->ensureWhitespaceAtIndex($index + 1, 0, ' ');
566+
$tokens->ensureWhitespaceAtIndex(
567+
$index + 1,
568+
0,
569+
self::LINE_NEXT === $this->configuration['position_after_control_structures'] && !$tokens[$nextNonWhitespaceIndex]->equals('(') ?
570+
$this->whitespacesConfig->getLineEnding().$this->detectIndent($tokens, $index)
571+
: ' '
572+
);
527573
}
528574

529575
$prevToken = $tokens[$index - 1];

0 commit comments

Comments
 (0)