Skip to content

Commit 0414fbe

Browse files
authored
Deprecate DataProviderStaticFixer (#885)
1 parent d3111f1 commit 0414fbe

File tree

5 files changed

+28
-46
lines changed

5 files changed

+28
-46
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ jobs:
8888
jq -r --indent 4 'del(.autoload."psr-4")' composer.json > composer.tmp
8989
jq -r --indent 4 '.autoload += {"files": ["bootstrap.php"]}' composer.tmp > composer.json
9090
- if: ${{ matrix.use-shim-package }}
91-
run: |
92-
composer remove friendsofphp/php-cs-fixer --no-update
93-
composer require php-cs-fixer/shim:* --no-update
91+
run: sed -i 's#friendsofphp/php-cs-fixer#php-cs-fixer/shim#g' composer.json
9492
- if: ${{ matrix.with-php-cs-fixer-from-master }}
9593
run: composer require friendsofphp/php-cs-fixer:dev-master --no-update
9694
- run: composer update --no-progress ${{ matrix.composer-flags }}

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## v3.14.0
44
- Add EmptyFunctionBodyFixer
5+
- Deprecate DataProviderStaticFixer - use "php_unit_data_provider_static"
6+
- Update minimum PHP CS Fixer version to 3.16.0
57

68
## v3.13.0
79
- MultilinePromotedPropertiesFixer - add option "keep_blank_lines"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ The types returned by data providers must be `iterable`.
110110

111111
#### DataProviderStaticFixer
112112
Data providers must be static.
113+
DEPRECATED: use `php_unit_data_provider_static` instead.
113114
*Risky: when `force` is set to `true`.*
114115
Configuration options:
115116
- `force` (`bool`): whether to make static data providers having dynamic class calls; defaults to `false`

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"php": "^7.4 || ^8.0",
1414
"ext-filter": "*",
1515
"ext-tokenizer": "*",
16-
"friendsofphp/php-cs-fixer": "^3.6.0"
16+
"friendsofphp/php-cs-fixer": "^3.16.0"
1717
},
1818
"require-dev": {
1919
"phpunit/phpunit": "^9.6.4 || ^10.0.14"

src/Fixer/DataProviderStaticFixer.php

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,33 @@
1212
namespace PhpCsFixerCustomFixers\Fixer;
1313

1414
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
15+
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
16+
use PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderStaticFixer;
1517
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
1618
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
1719
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
1820
use PhpCsFixer\FixerDefinition\CodeSample;
1921
use PhpCsFixer\FixerDefinition\FixerDefinition;
2022
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
21-
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
22-
use PhpCsFixer\Tokenizer\Token;
2323
use 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

Comments
 (0)