Repository: FriendsOfPHP/PHP-CS-Fixer (php)

Issue:
fixer takes 2 passes to apply all changes
## Bug report
I am gradually getting down to individual problems that I am seeing, so am reporting one small thing in one issue.

The first pass does:
https://github.com/owncloud/core/pull/40211/commits/f791cf322db2c6f5b4ae602a991fed11ef54c3c6#diff-cc01c446c3b8b5bd999b6e8565e380964d4d832f83450b46015667c4efb68a6f

```
		if ($notification === 'first') {
			$notificationId = \reset($lastNotifications);
		} else { /* if ($notification === 'last')*/ 
			$notificationId = \end($lastNotifications);
		}
```
moves the `{` of the else from the very end of the line (after the comment) to be after the `else` (with the comment after the `{`

But it leaves a space at the end of the line.

The 2nd pass removes the space at the end-of-line:
https://github.com/owncloud/core/pull/40211/commits/8f4a7ae580d32f14f1eefe59d08144b743f789de#diff-cc01c446c3b8b5bd999b6e8565e380964d4d832f83450b46015667c4efb68a6f

A 3rd pass is happy with the code - it does not suggest any more changes.

It would be nice if all the fixes were applied in a single run. Possibly the order of fixers is important, or it may always be necessary, in general, to re-run all fixers again after any fixer has made a change - because fixer A might have "done its thing" but left something in a state that some other fixer B now is not happy with.

### Code snippet that reproduces the problem

  ```php
  <?php
		if ($notification === 'first') {
			$notificationId = \reset($lastNotifications);
		} else /* if ($notification === 'last')*/ {
			$notificationId = \end($lastNotifications);
		}
  ```


Pull request description:
bug: Fix trailing whitespace after moving brace
Fixes #6488

Patch:
diff --git a/src/Fixer/Basic/CurlyBracesPositionFixer.php b/src/Fixer/Basic/CurlyBracesPositionFixer.php
index a3a469b59..60f849536 100644
--- a/src/Fixer/Basic/CurlyBracesPositionFixer.php
+++ b/src/Fixer/Basic/CurlyBracesPositionFixer.php
@@ -305,15 +305,15 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
                 $delta = $openBraceIndex < $moveBraceToIndex ? 1 : -1;
 
                 if ($tokens[$openBraceIndex + $delta]->isWhitespace()) {
-                    if (1 === $delta) {
-                        $tokens->clearAt($openBraceIndex - 1);
-                    } elseif ($tokens[$openBraceIndex - 1]->isWhitespace() && Preg::match('/\R/', $tokens[$openBraceIndex - 1]->getContent())) {
+                    if (-1 === $delta && Preg::match('/\R/', $tokens[$openBraceIndex - 1]->getContent())) {
                         $content = Preg::replace('/^(\h*?\R)?\h*/', '', $tokens[$openBraceIndex + 1]->getContent());
                         if ('' !== $content) {
                             $tokens[$openBraceIndex + 1] = new Token([T_WHITESPACE, $content]);
                         } else {
                             $tokens->clearAt($openBraceIndex + 1);
                         }
+                    } else {
+                        $tokens->clearAt($openBraceIndex - 1);
                     }
                 }
 


Test patch:
diff --git a/tests/Fixer/Basic/CurlyBracesPositionFixerTest.php b/tests/Fixer/Basic/CurlyBracesPositionFixerTest.php
index 89623557a..91beefbdc 100644
--- a/tests/Fixer/Basic/CurlyBracesPositionFixerTest.php
+++ b/tests/Fixer/Basic/CurlyBracesPositionFixerTest.php
@@ -644,6 +644,28 @@ function foo(
                 } elseif ($foo){
                 }',
         ];
+
+        yield 'open brace preceded by comment and whitespace' => [
+            '<?php
+                if (true) { /* foo */
+                    foo();
+                }',
+            '<?php
+                if (true) /* foo */ {
+                    foo();
+                }',
+        ];
+
+        yield 'open brace surrounded by comment and whitespace' => [
+            '<?php
+                if (true) { /* foo */ /* bar */
+                    foo();
+                }',
+            '<?php
+                if (true) /* foo */ { /* bar */
+                    foo();
+                }',
+        ];
     }
 
     /**