18
18
use PhpCsFixer \DocBlock \DocBlock ;
19
19
use PhpCsFixer \Fixer \AbstractPhpUnitFixer ;
20
20
use PhpCsFixer \Fixer \AttributeNotation \OrderedAttributesFixer ;
21
+ use PhpCsFixer \Fixer \ConfigurableFixerInterface ;
22
+ use PhpCsFixer \Fixer \ConfigurableFixerTrait ;
23
+ use PhpCsFixer \FixerConfiguration \FixerConfigurationResolver ;
24
+ use PhpCsFixer \FixerConfiguration \FixerConfigurationResolverInterface ;
25
+ use PhpCsFixer \FixerConfiguration \FixerOptionBuilder ;
21
26
use PhpCsFixer \FixerDefinition \FixerDefinition ;
22
27
use PhpCsFixer \FixerDefinition \FixerDefinitionInterface ;
23
28
use PhpCsFixer \FixerDefinition \VersionSpecification ;
31
36
32
37
/**
33
38
* @author Kuba Werłos <[email protected] >
39
+ *
40
+ * @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
41
+ *
42
+ * @phpstan-type _AutogeneratedInputConfiguration array{
43
+ * keep_annotations?: bool
44
+ * }
45
+ * @phpstan-type _AutogeneratedComputedConfiguration array{
46
+ * keep_annotations: bool
47
+ * }
34
48
*/
35
- final class PhpUnitAttributesFixer extends AbstractPhpUnitFixer
49
+ final class PhpUnitAttributesFixer extends AbstractPhpUnitFixer implements ConfigurableFixerInterface
36
50
{
51
+ /** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
52
+ use ConfigurableFixerTrait;
53
+
37
54
/** @var array<string, string> */
38
55
private array $ fixingMap ;
39
56
@@ -45,29 +62,29 @@ public function __construct()
45
62
46
63
public function getDefinition (): FixerDefinitionInterface
47
64
{
65
+ $ codeSample = <<<'PHP'
66
+ <?php
67
+ /**
68
+ * @covers \VendorName\Foo
69
+ * @internal
70
+ */
71
+ final class FooTest extends TestCase {
72
+ /**
73
+ * @param int $expected
74
+ * @param int $actual
75
+ * @dataProvider giveMeSomeData
76
+ * @requires PHP 8.0
77
+ */
78
+ public function testSomething($expected, $actual) {}
79
+ }
80
+
81
+ PHP;
82
+
48
83
return new FixerDefinition (
49
84
'PHPUnit attributes must be used over their respective PHPDoc-based annotations. ' ,
50
85
[
51
- new VersionSpecificCodeSample (
52
- <<<'PHP'
53
- <?php
54
- /**
55
- * @covers \VendorName\Foo
56
- * @internal
57
- */
58
- final class FooTest extends TestCase {
59
- /**
60
- * @param int $expected
61
- * @param int $actual
62
- * @dataProvider giveMeSomeData
63
- * @requires PHP 8.0
64
- */
65
- public function testSomething($expected, $actual) {}
66
- }
67
-
68
- PHP,
69
- new VersionSpecification (8_00_00 ),
70
- ),
86
+ new VersionSpecificCodeSample ($ codeSample , new VersionSpecification (8_00_00 )),
87
+ new VersionSpecificCodeSample ($ codeSample , new VersionSpecification (8_00_00 ), ['keep_annotations ' => true ]),
71
88
],
72
89
);
73
90
}
@@ -87,6 +104,16 @@ public function getPriority(): int
87
104
return 8 ;
88
105
}
89
106
107
+ protected function createConfigurationDefinition (): FixerConfigurationResolverInterface
108
+ {
109
+ return new FixerConfigurationResolver ([
110
+ (new FixerOptionBuilder ('keep_annotations ' , 'Whether to keep annotations or not. This may be helpful for projects that support PHP before version 8 or PHPUnit before version 10. ' ))
111
+ ->setAllowedTypes (['bool ' ])
112
+ ->setDefault (false )
113
+ ->getOption (),
114
+ ]);
115
+ }
116
+
90
117
protected function applyPhpUnitClassFix (Tokens $ tokens , int $ startIndex , int $ endIndex ): void
91
118
{
92
119
$ classIndex = $ tokens ->getPrevTokenOfKind ($ startIndex , [[T_CLASS ]]);
@@ -136,7 +163,10 @@ protected function applyPhpUnitClassFix(Tokens $tokens, int $startIndex, int $en
136
163
}
137
164
138
165
$ tokens ->insertSlices ([$ index + 1 => $ tokensToInsert ]);
139
- $ annotation ->remove ();
166
+
167
+ if (!$ this ->configuration ['keep_annotations ' ]) {
168
+ $ annotation ->remove ();
169
+ }
140
170
}
141
171
142
172
if ('' === $ docBlock ->getContent ()) {
0 commit comments