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

Issue:
php_unit_test_annotation set to prefix erroneously adds 'test' to single line doc blocks with the @depends tag
## Versions used

 * php-cs-fixer:  3.8.0 BerSzcz against war! 
 * PHP runtime: 8.1.6

## What did I do

Executed `vendor/bin/php-cs-fixer fix -v --allow-risky=yes tests/Unit/ItemTest.php`.

## What happened

The string `test` was appended to single-line-docblocks with the `@depends` tag.

```diff
      ---------- begin diff ----------
--- /var/www/tests/Unit/ItemTest.php
+++ /var/www/tests/Unit/ItemTest.php
@@ -120,7 +120,7 @@
         <irrelevant code>
     }

-    /** @depends testSaveStoppedWhenPostponed */
+    /** @depends testSaveStoppedWhenPostponed test*/
     public function testSaveResumedAfterPostpone()
     {
         <irrelevant code>

      ----------- end diff -----------
```

## What I expected to happen

Nothing. Considering that the rule `php_unit_test_annotation` is imported from `@PhpCsFixer:risky` and wasn't changed (so it's still on the default `prefix`), it shouldn't act here at all.

## Notes

The following PhpDoc blocks were not affected by the rule. Not sure if there are other single-line comments this would affect, as I only tested `@covers`

```php
    /**
     * @depends testCanSave
     */

    /** @covers Edit::save */
```

Probably not an urgent issue, since the workaround is to simply expand the PhpDoc block

## Used Ruleset

```php
[
    '@PhpCsFixer'                            => true,
    '@PhpCsFixer:risky'                      => true,
    'multiline_whitespace_before_semicolons' => ['strategy' => 'no_multi_line'],
    'yoda_style'                             => [
        'equal'                => null,
        'identical'            => null,
        'less_and_greater'     => null,
        'always_move_variable' => false,
    ],
    'phpdoc_to_comment'                      => [
        'ignored_tags' => ['noinspection', 'var'],
    ],
    'self_accessor'                          => false,
    'binary_operator_spaces'                 => [
        'operators' => [
            '=>' => 'align',
        ],
    ],
    'strict_comparison'                      => false,
    'phpdoc_no_alias_tag'                    => ['replacements' => ['type' => 'var', 'link' => 'see']],
]
```



Discussion:
@Bbansjkl is this issue still valid? Can you check it with most recent version of Fixer?
@Wirone Sorry, I can't check it since I no longer have a setup for PHP development.



Patch:
diff --git a/src/Fixer/PhpUnit/PhpUnitTestAnnotationFixer.php b/src/Fixer/PhpUnit/PhpUnitTestAnnotationFixer.php
index 0329643bb36..c97fa287d9c 100644
--- a/src/Fixer/PhpUnit/PhpUnitTestAnnotationFixer.php
+++ b/src/Fixer/PhpUnit/PhpUnitTestAnnotationFixer.php
@@ -380,13 +380,13 @@ private function addTestPrefixToDependsAnnotation(Line $line): Line
      */
     private function findWhereDependsFunctionNameStarts(array $line): int
     {
-        $counter = \count($line);
+        $index = stripos(implode('', $line), '@depends') + 8;
 
-        do {
-            --$counter;
-        } while (' ' !== $line[$counter]);
+        while (' ' === $line[$index]) {
+            ++$index;
+        }
 
-        return $counter + 1;
+        return $index;
     }
 
     /**


Test patch:
diff --git a/tests/Fixer/PhpUnit/PhpUnitTestAnnotationFixerTest.php b/tests/Fixer/PhpUnit/PhpUnitTestAnnotationFixerTest.php
index ba5241a289b..334cdb47b1c 100644
--- a/tests/Fixer/PhpUnit/PhpUnitTestAnnotationFixerTest.php
+++ b/tests/Fixer/PhpUnit/PhpUnitTestAnnotationFixerTest.php
@@ -976,6 +976,18 @@ public function test_test_foo() {}
 }',
                 ['style' => 'annotation'],
             ],
+            'do not touch single line @depends annotation when already correct' => [
+                '<?php class FooTest extends \PHPUnit\Framework\TestCase
+                {
+                    public function testOne() {}
+
+                    /** @depends testOne */
+                    public function testTwo() {}
+
+                    /**    @depends    testTwo    */
+                    public function testThree() {}
+                }',
+            ],
         ];
     }