1212namespace PhpCsFixerCustomFixers \Fixer ;
1313
1414use PhpCsFixer \Fixer \ConfigurableFixerInterface ;
15+ use PhpCsFixer \Fixer \DeprecatedFixerInterface ;
16+ use PhpCsFixer \Fixer \PhpUnit \PhpUnitDataProviderStaticFixer ;
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 \Tokenizer \Token ;
2323use PhpCsFixer \Tokenizer \Tokens ;
24- use PhpCsFixer \Tokenizer \TokensAnalyzer ;
25- use PhpCsFixerCustomFixers \Analyzer \DataProviderAnalyzer ;
2624
27- final class DataProviderStaticFixer extends AbstractFixer implements ConfigurableFixerInterface
25+ /**
26+ * @deprecated
27+ */
28+ final class DataProviderStaticFixer extends AbstractFixer implements ConfigurableFixerInterface, DeprecatedFixerInterface
2829{
29- /** @var bool */
30- private $ force = false ;
30+ private bool $ force = false ;
31+ private PhpUnitDataProviderStaticFixer $ phpUnitDataProviderStaticFixer ;
32+
33+ public function __construct ()
34+ {
35+ $ this ->phpUnitDataProviderStaticFixer = new PhpUnitDataProviderStaticFixer ();
36+ }
3137
3238 public function getDefinition (): FixerDefinitionInterface
3339 {
3440 return new FixerDefinition (
35- ' Data providers must be static. ' ,
41+ $ this -> phpUnitDataProviderStaticFixer -> getDefinition ()-> getSummary () ,
3642 [
3743 new CodeSample (
3844 '<?php
@@ -69,16 +75,17 @@ public function configure(array $configuration): void
6975 if (\array_key_exists ('force ' , $ configuration )) {
7076 $ this ->force = $ configuration ['force ' ];
7177 }
78+ $ this ->phpUnitDataProviderStaticFixer ->configure (['force ' => $ this ->force ]);
7279 }
7380
7481 public function getPriority (): int
7582 {
76- return 0 ;
83+ return $ this -> phpUnitDataProviderStaticFixer -> getPriority () ;
7784 }
7885
7986 public function isCandidate (Tokens $ tokens ): bool
8087 {
81- return $ tokens -> isAllTokenKindsFound ([\ T_CLASS , \ T_DOC_COMMENT , \ T_EXTENDS , \ T_FUNCTION , \ T_STRING ] );
88+ return $ this -> phpUnitDataProviderStaticFixer -> isCandidate ( $ tokens );
8289 }
8390
8491 public function isRisky (): bool
@@ -88,40 +95,14 @@ public function isRisky(): bool
8895
8996 public function fix (\SplFileInfo $ file , Tokens $ tokens ): void
9097 {
91- $ phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator ();
92-
93- /** @var array<int> $indices */
94- foreach ($ phpUnitTestCaseIndicator ->findPhpUnitClasses ($ tokens ) as $ indices ) {
95- $ this ->fixStatic ($ tokens , $ indices [0 ], $ indices [1 ]);
96- }
98+ $ this ->phpUnitDataProviderStaticFixer ->fix ($ file , $ tokens );
9799 }
98100
99- private function fixStatic (Tokens $ tokens , int $ startIndex , int $ endIndex ): void
101+ /**
102+ * @return array<string>
103+ */
104+ public function getSuccessorsNames (): array
100105 {
101- $ dataProviderAnalyzer = new DataProviderAnalyzer ();
102- $ tokensAnalyzer = new TokensAnalyzer ($ tokens );
103-
104- foreach (\array_reverse ($ dataProviderAnalyzer ->getDataProviders ($ tokens , $ startIndex , $ endIndex )) as $ dataProviderAnalysis ) {
105- $ methodStartIndex = $ tokens ->getNextTokenOfKind ($ dataProviderAnalysis ->getNameIndex (), ['{ ' ]);
106- if ($ methodStartIndex !== null ) {
107- $ methodEndIndex = $ tokens ->findBlockEnd (Tokens::BLOCK_TYPE_CURLY_BRACE , $ methodStartIndex );
108-
109- if (!$ this ->force && $ tokens ->findSequence ([[\T_VARIABLE , '$this ' ]], $ methodStartIndex , $ methodEndIndex ) !== null ) {
110- continue ;
111- }
112- }
113- $ functionIndex = $ tokens ->getPrevTokenOfKind ($ dataProviderAnalysis ->getNameIndex (), [[\T_FUNCTION ]]);
114- \assert (\is_int ($ functionIndex ));
115-
116- $ methodAttributes = $ tokensAnalyzer ->getMethodAttributes ($ functionIndex );
117- if ($ methodAttributes ['static ' ] !== false ) {
118- continue ;
119- }
120-
121- $ tokens ->insertAt (
122- $ functionIndex ,
123- [new Token ([\T_STATIC , 'static ' ]), new Token ([\T_WHITESPACE , ' ' ])],
124- );
125- }
106+ return [$ this ->phpUnitDataProviderStaticFixer ->getName ()];
126107 }
127108}
0 commit comments