@@ -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