1212namespace PhpCsFixerCustomFixers \Fixer ;
1313
1414use PhpCsFixer \Fixer \ConfigurableFixerInterface ;
15+ use PhpCsFixer \Fixer \DeprecatedFixerInterface ;
16+ use PhpCsFixer \Fixer \PhpUnit \PhpUnitDataProviderNameFixer ;
1517use PhpCsFixer \FixerConfiguration \FixerConfigurationResolver ;
1618use PhpCsFixer \FixerConfiguration \FixerConfigurationResolverInterface ;
1719use PhpCsFixer \FixerConfiguration \FixerOptionBuilder ;
1820use PhpCsFixer \FixerDefinition \CodeSample ;
1921use PhpCsFixer \FixerDefinition \FixerDefinition ;
2022use PhpCsFixer \FixerDefinition \FixerDefinitionInterface ;
21- use PhpCsFixer \Indicator \PhpUnitTestCaseIndicator ;
22- use PhpCsFixer \Preg ;
23- use PhpCsFixer \Tokenizer \Token ;
2423use PhpCsFixer \Tokenizer \Tokens ;
25- use PhpCsFixerCustomFixers \Analyzer \DataProviderAnalyzer ;
2624
27- final class DataProviderNameFixer extends AbstractFixer implements ConfigurableFixerInterface
25+ /**
26+ * @deprecated
27+ */
28+ final class DataProviderNameFixer extends AbstractFixer implements ConfigurableFixerInterface, DeprecatedFixerInterface
2829{
30+ private PhpUnitDataProviderNameFixer $ phpUnitDataProviderNameFixer ;
31+
32+ public function __construct ()
33+ {
34+ $ this ->phpUnitDataProviderNameFixer = new PhpUnitDataProviderNameFixer ();
35+ }
36+
2937 /** @var string */
3038 private $ prefix = 'provide ' ;
3139
@@ -35,7 +43,7 @@ final class DataProviderNameFixer extends AbstractFixer implements ConfigurableF
3543 public function getDefinition (): FixerDefinitionInterface
3644 {
3745 return new FixerDefinition (
38- ' Data provider names used only once must match the name of the test. ' ,
46+ $ this -> phpUnitDataProviderNameFixer -> getDefinition ()-> getSummary () ,
3947 [
4048 new CodeSample (
4149 '<?php
@@ -73,80 +81,34 @@ public function getConfigurationDefinition(): FixerConfigurationResolverInterfac
7381 */
7482 public function configure (array $ configuration ): void
7583 {
76- if (\array_key_exists ('prefix ' , $ configuration )) {
77- $ this ->prefix = $ configuration ['prefix ' ];
78- }
79-
80- if (\array_key_exists ('suffix ' , $ configuration )) {
81- $ this ->suffix = $ configuration ['suffix ' ];
82- }
84+ $ this ->phpUnitDataProviderNameFixer ->configure ($ configuration );
8385 }
8486
8587 public function getPriority (): int
8688 {
87- return 0 ;
89+ return $ this -> phpUnitDataProviderNameFixer -> getPriority () ;
8890 }
8991
9092 public function isCandidate (Tokens $ tokens ): bool
9193 {
92- return $ tokens -> isAllTokenKindsFound ([\ T_CLASS , \ T_DOC_COMMENT , \ T_EXTENDS , \ T_FUNCTION , \ T_STRING ] );
94+ return $ this -> phpUnitDataProviderNameFixer -> isCandidate ( $ tokens );
9395 }
9496
9597 public function isRisky (): bool
9698 {
97- return true ;
99+ return $ this -> phpUnitDataProviderNameFixer -> isRisky () ;
98100 }
99101
100102 public function fix (\SplFileInfo $ file , Tokens $ tokens ): void
101103 {
102- $ phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator ();
103-
104- /** @var array<int> $indices */
105- foreach ($ phpUnitTestCaseIndicator ->findPhpUnitClasses ($ tokens ) as $ indices ) {
106- $ this ->fixNames ($ tokens , $ indices [0 ], $ indices [1 ]);
107- }
108- }
109-
110- private function fixNames (Tokens $ tokens , int $ startIndex , int $ endIndex ): void
111- {
112- $ dataProviderAnalyzer = new DataProviderAnalyzer ();
113- foreach ($ dataProviderAnalyzer ->getDataProviders ($ tokens , $ startIndex , $ endIndex ) as $ dataProviderAnalysis ) {
114- if (\count ($ dataProviderAnalysis ->getUsageIndices ()) > 1 ) {
115- continue ;
116- }
117-
118- $ usageIndex = $ dataProviderAnalysis ->getUsageIndices ()[0 ];
119-
120- $ testNameIndex = $ tokens ->getNextTokenOfKind ($ usageIndex , [[\T_STRING ]]);
121- \assert (\is_int ($ testNameIndex ));
122-
123- $ dataProviderNewName = $ this ->getProviderNameForTestName ($ tokens [$ testNameIndex ]->getContent ());
124- if ($ tokens ->findSequence ([[\T_FUNCTION ], [\T_STRING , $ dataProviderNewName ]], $ startIndex , $ endIndex ) !== null ) {
125- continue ;
126- }
127-
128- $ tokens [$ dataProviderAnalysis ->getNameIndex ()] = new Token ([\T_STRING , $ dataProviderNewName ]);
129-
130- $ newCommentContent = Preg::replace (
131- \sprintf ('/(@dataProvider\s+)%s/ ' , $ dataProviderAnalysis ->getName ()),
132- \sprintf ('$1%s ' , $ dataProviderNewName ),
133- $ tokens [$ usageIndex ]->getContent (),
134- );
135-
136- $ tokens [$ usageIndex ] = new Token ([\T_DOC_COMMENT , $ newCommentContent ]);
137- }
104+ $ this ->phpUnitDataProviderNameFixer ->fix ($ file , $ tokens );
138105 }
139106
140- private function getProviderNameForTestName (string $ name ): string
107+ /**
108+ * @return array<string>
109+ */
110+ public function getSuccessorsNames (): array
141111 {
142- $ name = Preg::replace ('/^test_*/i ' , '' , $ name );
143-
144- if ($ this ->prefix === '' ) {
145- $ name = \lcfirst ($ name );
146- } elseif (\substr ($ this ->prefix , -1 ) !== '_ ' ) {
147- $ name = \ucfirst ($ name );
148- }
149-
150- return $ this ->prefix . $ name . $ this ->suffix ;
112+ return [$ this ->phpUnitDataProviderNameFixer ->getName ()];
151113 }
152114}
0 commit comments