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

Issue:
Parse error when refactoring to willReturnCallback 
## Bug report

PHP CS Fixer 3.16.0 BoY42 by Fabien Potencier and Dariusz Ruminski.
PHP runtime: 8.2.3

### Description

```
Files that were not fixed due to errors reported during linting after fixing:
   1) <path>

[PhpCsFixer\Linter\LintingException]                                        
Parse error: syntax error, unexpected token ",", expecting ")" on line 19.
```
```diff
--- /Users/vudaltsov/PhpProjects/webinar/organization-api/tests/Unit/Infrastructure/Messaging/Internal/OrganizationMemberUnblockedListenerTest.php
+++ /Users/vudaltsov/PhpProjects/webinar/organization-api/tests/Unit/Infrastructure/Messaging/Internal/OrganizationMemberUnblockedListenerTest.php
@@ -1,17 +1,22 @@
 <?php
 
+declare(strict_types=1);
+
 use PHPUnit\Framework\TestCase;
 
-class SomeTest extends TestCase
+/**
+ * @internal
+ * @coversNothing
+ */
+final class OrganizationMemberUnblockedListenerTest extends TestCase
 {
     public function test(): void
     {
         $service = $this->createMock(\stdClass::class);
-        $service->method('some')->will(
-            $this->returnCallback(
-                function (mixed $event) {
-                }
-            )
+        $service->method('some')->willReturnCallback(
+                function (mixed $event): void {
+                },
+            ,
         );
     }
 }

```

### Runtime version

PHP CS Fixer 3.16.0 BoY42 by Fabien Potencier and Dariusz Ruminski.
PHP runtime: 8.2.3

### Configuration file

```php
$config = (new PhpCsFixer\Config())
    ->setFinder($finder)
    ->setRiskyAllowed(true)
    ->setRules([
        '@PHP80Migration:risky' => true,
        '@PHP82Migration' => true,
        '@PhpCsFixer' => true,
        '@PhpCsFixer:risky' => true,
        '@PHPUnit84Migration:risky' => true,
        'trailing_comma_in_multiline' => ['after_heredoc' => true, 'elements' => ['arrays', 'arguments', 'parameters']],
    ]);
```

### Code snippet that reproduces the problem

```
<?php

use PHPUnit\Framework\TestCase;

class SomeTest extends TestCase
{
    public function test(): void
    {
        $service = $this->createMock(\stdClass::class);
        $service->method('some')->will(
            $this->returnCallback(
                function (mixed $event) {
                }
            )
        );
    }
}
```



Discussion:

Patch:
diff --git a/src/Fixer/PhpUnit/PhpUnitMockShortWillReturnFixer.php b/src/Fixer/PhpUnit/PhpUnitMockShortWillReturnFixer.php
index ed253175217..2e04f23fe3b 100644
--- a/src/Fixer/PhpUnit/PhpUnitMockShortWillReturnFixer.php
+++ b/src/Fixer/PhpUnit/PhpUnitMockShortWillReturnFixer.php
@@ -126,6 +126,11 @@ protected function applyPhpUnitClassFix(Tokens $tokens, int $startIndex, int $en
             $tokens->clearTokenAndMergeSurroundingWhitespace($functionToRemoveIndex);
             $tokens->clearTokenAndMergeSurroundingWhitespace($openingBraceIndex);
             $tokens->clearTokenAndMergeSurroundingWhitespace($closingBraceIndex);
+
+            $commaAfterClosingBraceIndex = $tokens->getNextMeaningfulToken($closingBraceIndex);
+            if ($tokens[$commaAfterClosingBraceIndex]->equals(',')) {
+                $tokens->clearTokenAndMergeSurroundingWhitespace($commaAfterClosingBraceIndex);
+            }
         }
     }
 }


Test patch:
diff --git a/tests/Fixer/PhpUnit/PhpUnitMockShortWillReturnFixerTest.php b/tests/Fixer/PhpUnit/PhpUnitMockShortWillReturnFixerTest.php
index 7e77584474e..f700883a255 100644
--- a/tests/Fixer/PhpUnit/PhpUnitMockShortWillReturnFixerTest.php
+++ b/tests/Fixer/PhpUnit/PhpUnitMockShortWillReturnFixerTest.php
@@ -261,6 +261,20 @@ class FooTest extends TestCase {
     public function testFoo() {
         $someMock->method("someMethod")->will($this->returnValue( 10 , ));
     }
+}',
+            ],
+            'with trailing commas' => [
+                '<?php
+class FooTest extends TestCase {
+    public function testFoo() {
+        $someMock->method("someMethod")->willReturn( 10 ,   );
+    }
+}',
+                '<?php
+class FooTest extends TestCase {
+    public function testFoo() {
+        $someMock->method("someMethod")->will($this->returnValue( 10 , ) , );
+    }
 }',
             ],
         ];
diff --git a/tests/Fixtures/Integration/misc/php_unit_mock_short_will_return,trailing_comma_in_multiline.test b/tests/Fixtures/Integration/misc/php_unit_mock_short_will_return,trailing_comma_in_multiline.test
new file mode 100644
index 00000000000..fb1e6af4296
--- /dev/null
+++ b/tests/Fixtures/Integration/misc/php_unit_mock_short_will_return,trailing_comma_in_multiline.test
@@ -0,0 +1,33 @@
+--TEST--
+Integration of fixers: php_unit_mock_short_will_return,trailing_comma_in_multiline.
+--RULESET--
+{"php_unit_mock_short_will_return": true, "trailing_comma_in_multiline": {"elements": ["arguments"]}}
+--EXPECT--
+<?php
+class FooTest extends TestCase
+{
+    public function test(): void
+    {
+        $service->method('some')->willReturnCallback(
+            
+                function (mixed $event) {
+                },
+            
+        );
+    }
+}
+
+--INPUT--
+<?php
+class FooTest extends TestCase
+{
+    public function test(): void
+    {
+        $service->method('some')->will(
+            $this->returnCallback(
+                function (mixed $event) {
+                }
+            )
+        );
+    }
+}