@@ -124,13 +124,13 @@ private function processElse(\PHP_CodeSniffer\Files\File $phpcsFile, int $elsePo
124124
125125 $ ifCode = $ this ->getScopeCode ($ phpcsFile , $ ifPointer );
126126 $ elseCode = $ this ->getScopeCode ($ phpcsFile , $ elsePointer );
127- $ negativeIfCondition = $ this ->getNegativeCondition ($ phpcsFile , $ tokens [$ ifPointer ]['parenthesis_opener ' ] + 1 , $ tokens [$ ifPointer ]['parenthesis_closer ' ] - 1 );
127+ $ negativeIfCondition = $ this ->getNegativeCondition ($ phpcsFile , $ tokens [$ ifPointer ]['parenthesis_opener ' ], $ tokens [$ ifPointer ]['parenthesis_closer ' ]);
128128 $ afterIfCode = $ this ->fixIndentation ($ ifCode , $ phpcsFile ->eolChar , $ this ->getIndentation ($ phpcsFile , $ ifPointer ));
129129
130130 $ phpcsFile ->fixer ->addContent (
131131 $ ifPointer ,
132132 sprintf (
133- 'if (%s) {%s}%s%s ' ,
133+ 'if %s {%s}%s%s ' ,
134134 $ negativeIfCondition ,
135135 $ elseCode ,
136136 $ phpcsFile ->eolChar ,
@@ -225,7 +225,7 @@ private function processIf(\PHP_CodeSniffer\Files\File $phpcsFile, int $ifPointe
225225 $ earlyExitCode = $ this ->getEarlyExitCode ($ tokens [$ scopePointer ]['code ' ]);
226226 $ earlyExitCodeIndentation = $ this ->prepareIndentation ($ ifIndentation );
227227
228- $ negativeIfCondition = $ this ->getNegativeCondition ($ phpcsFile , $ tokens [$ ifPointer ]['parenthesis_opener ' ] + 1 , $ tokens [$ ifPointer ]['parenthesis_closer ' ] - 1 );
228+ $ negativeIfCondition = $ this ->getNegativeCondition ($ phpcsFile , $ tokens [$ ifPointer ]['parenthesis_opener ' ], $ tokens [$ ifPointer ]['parenthesis_closer ' ]);
229229
230230 $ phpcsFile ->fixer ->beginChangeset ();
231231
@@ -238,7 +238,7 @@ private function processIf(\PHP_CodeSniffer\Files\File $phpcsFile, int $ifPointe
238238 $ phpcsFile ->fixer ->addContent (
239239 $ ifPointer ,
240240 sprintf (
241- 'if (%s) {%s%s%s;%s%s}%s%s ' ,
241+ 'if %s {%s%s%s;%s%s}%s%s ' ,
242242 $ negativeIfCondition ,
243243 $ phpcsFile ->eolChar ,
244244 $ earlyExitCodeIndentation ,
@@ -291,7 +291,7 @@ private function getEarlyExitCode($code): string
291291 return 'return ' ;
292292 }
293293
294- private function getNegativeCondition (\PHP_CodeSniffer \Files \File $ phpcsFile , int $ conditionBoundaryStartPointer , int $ conditionBoundaryEndPointer , bool $ nested = false ): string
294+ private function getNegativeConditionPart (\PHP_CodeSniffer \Files \File $ phpcsFile , int $ conditionBoundaryStartPointer , int $ conditionBoundaryEndPointer , bool $ nested ): string
295295 {
296296 $ tokens = $ phpcsFile ->getTokens ();
297297
@@ -314,7 +314,7 @@ private function getNegativeCondition(\PHP_CodeSniffer\Files\File $phpcsFile, in
314314 }
315315 }
316316
317- if (! $ nested && count ($ booleanPointers ) > 0 ) {
317+ if (count ($ booleanPointers ) > 0 ) {
318318 return $ this ->getNegativeLogicalCondition ($ phpcsFile , $ conditionBoundaryStartPointer , $ conditionBoundaryEndPointer );
319319 }
320320
@@ -405,30 +405,38 @@ private function getNegativeLogicalCondition(\PHP_CodeSniffer\Files\File $phpcsF
405405 continue ;
406406 }
407407
408- $ negativeCondition .= $ this ->getNegativeNestedCondition ($ phpcsFile , $ nestedConditionStartPointer , $ actualPointer - 1 );
408+ $ negativeCondition .= $ this ->getNegativeCondition ($ phpcsFile , $ nestedConditionStartPointer , $ actualPointer - 1 , true );
409409 $ negativeCondition .= $ booleanOperatorReplacements [$ tokens [$ actualPointer ]['code ' ]];
410410
411411 $ nestedConditionStartPointer = $ actualPointer + 1 ;
412412 $ actualPointer ++;
413413
414414 } while (true );
415415
416- $ negativeCondition .= $ this ->getNegativeNestedCondition ($ phpcsFile , $ nestedConditionStartPointer , $ conditionBoundaryEndPointer );
416+ $ negativeCondition .= $ this ->getNegativeCondition ($ phpcsFile , $ nestedConditionStartPointer , $ conditionBoundaryEndPointer, true );
417417
418418 return $ negativeCondition ;
419419 }
420420
421- private function getNegativeNestedCondition (\PHP_CodeSniffer \Files \File $ phpcsFile , int $ conditionBoundaryStartPointer , int $ conditionBoundaryEndPointer ): string
421+ private function getNegativeCondition (\PHP_CodeSniffer \Files \File $ phpcsFile , int $ conditionBoundaryStartPointer , int $ conditionBoundaryEndPointer, bool $ nested = false ): string
422422 {
423423 /** @var int $conditionStartPointer */
424424 $ conditionStartPointer = TokenHelper::findNextEffective ($ phpcsFile , $ conditionBoundaryStartPointer );
425425 /** @var int $conditionEndPointer */
426426 $ conditionEndPointer = TokenHelper::findPreviousEffective ($ phpcsFile , $ conditionBoundaryEndPointer );
427427
428+ $ tokens = $ phpcsFile ->getTokens ();
429+ if ($ tokens [$ conditionStartPointer ]['code ' ] === T_OPEN_PARENTHESIS && $ tokens [$ conditionStartPointer ]['parenthesis_closer ' ] === $ conditionEndPointer ) {
430+ /** @var int $conditionStartPointer */
431+ $ conditionStartPointer = TokenHelper::findNextEffective ($ phpcsFile , $ conditionStartPointer + 1 );
432+ /** @var int $conditionEndPointer */
433+ $ conditionEndPointer = TokenHelper::findPreviousEffective ($ phpcsFile , $ conditionEndPointer - 1 );
434+ }
435+
428436 return sprintf (
429437 '%s%s%s ' ,
430438 $ conditionBoundaryStartPointer !== $ conditionStartPointer ? TokenHelper::getContent ($ phpcsFile , $ conditionBoundaryStartPointer , $ conditionStartPointer - 1 ) : '' ,
431- $ this ->getNegativeCondition ($ phpcsFile , $ conditionStartPointer , $ conditionEndPointer , true ),
439+ $ this ->getNegativeConditionPart ($ phpcsFile , $ conditionStartPointer , $ conditionEndPointer , $ nested ),
432440 $ conditionBoundaryEndPointer !== $ conditionEndPointer ? TokenHelper::getContent ($ phpcsFile , $ conditionEndPointer + 1 , $ conditionBoundaryEndPointer ) : ''
433441 );
434442 }
0 commit comments