Skip to content

Commit 5f06d9d

Browse files
authored
Deprecate DataProviderReturnTypeFixer (#912)
1 parent c3df089 commit 5f06d9d

File tree

5 files changed

+41
-73
lines changed

5 files changed

+41
-73
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG for PHP CS Fixer: custom fixers
22

3+
## v3.16.0
4+
- Deprecate DataProviderReturnTypeFixer - use "php_unit_data_provider_return_type"
5+
- Update minimum PHP CS Fixer version to 3.22.0
6+
37
## v3.15.0
48
- Deprecate DataProviderNameFixer - use "php_unit_data_provider_name"
59
- Deprecate PhpdocParamOrderFixer - use "phpdoc_param_order"

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
44
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
55
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
6-
![Tests](https://img.shields.io/badge/tests-3446-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-3445-brightgreen.svg)
77
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
88

99
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)
@@ -95,7 +95,8 @@ Configuration options:
9595
```
9696

9797
#### DataProviderReturnTypeFixer
98-
The types returned by data providers must be `iterable`.
98+
The return type of PHPUnit data provider must be `iterable`.
99+
DEPRECATED: use `php_unit_data_provider_return_type` instead.
99100
*Risky: when relying on signature of data provider.*
100101
```diff
101102
<?php

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.19"
16+
"friendsofphp/php-cs-fixer": "^3.22"
1717
},
1818
"require-dev": {
1919
"phpunit/phpunit": "^9.6.4 || ^10.0.14"

src/Fixer/DataProviderReturnTypeFixer.php

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,29 @@
1111

1212
namespace PhpCsFixerCustomFixers\Fixer;
1313

14+
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
15+
use PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderReturnTypeFixer;
1416
use PhpCsFixer\FixerDefinition\CodeSample;
1517
use PhpCsFixer\FixerDefinition\FixerDefinition;
1618
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
17-
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
18-
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
19-
use PhpCsFixer\Tokenizer\CT;
20-
use PhpCsFixer\Tokenizer\Token;
2119
use PhpCsFixer\Tokenizer\Tokens;
22-
use PhpCsFixerCustomFixers\Analyzer\DataProviderAnalyzer;
2320

24-
final class DataProviderReturnTypeFixer extends AbstractFixer
21+
/**
22+
* @deprecated
23+
*/
24+
final class DataProviderReturnTypeFixer extends AbstractFixer implements DeprecatedFixerInterface
2525
{
26+
private PhpUnitDataProviderReturnTypeFixer $phpUnitDataProviderReturnTypeFixer;
27+
28+
public function __construct()
29+
{
30+
$this->phpUnitDataProviderReturnTypeFixer = new PhpUnitDataProviderReturnTypeFixer();
31+
}
32+
2633
public function getDefinition(): FixerDefinitionInterface
2734
{
2835
return new FixerDefinition(
29-
'The types returned by data providers must be `iterable`.',
36+
$this->phpUnitDataProviderReturnTypeFixer->getDefinition()->getSummary(),
3037
[
3138
new CodeSample(
3239
'<?php
@@ -50,57 +57,29 @@ public function provideSomethingCases(): array {}
5057
*/
5158
public function getPriority(): int
5259
{
53-
return 0;
60+
return $this->phpUnitDataProviderReturnTypeFixer->getPriority();
5461
}
5562

5663
public function isCandidate(Tokens $tokens): bool
5764
{
58-
return $tokens->isAllTokenKindsFound([\T_CLASS, \T_DOC_COMMENT, \T_EXTENDS, \T_FUNCTION, \T_STRING]);
65+
return $this->phpUnitDataProviderReturnTypeFixer->isCandidate($tokens);
5966
}
6067

6168
public function isRisky(): bool
6269
{
63-
return true;
70+
return $this->phpUnitDataProviderReturnTypeFixer->isRisky();
6471
}
6572

6673
public function fix(\SplFileInfo $file, Tokens $tokens): void
6774
{
68-
$phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
69-
70-
/** @var array<int> $indices */
71-
foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indices) {
72-
$this->fixReturnTypes($tokens, $indices[0], $indices[1]);
73-
}
75+
$this->phpUnitDataProviderReturnTypeFixer->fix($file, $tokens);
7476
}
7577

76-
private function fixReturnTypes(Tokens $tokens, int $startIndex, int $endIndex): void
78+
/**
79+
* @return array<string>
80+
*/
81+
public function getSuccessorsNames(): array
7782
{
78-
$dataProviderAnalyzer = new DataProviderAnalyzer();
79-
$functionsAnalyzer = new FunctionsAnalyzer();
80-
81-
foreach (\array_reverse($dataProviderAnalyzer->getDataProviders($tokens, $startIndex, $endIndex)) as $dataProviderAnalysis) {
82-
$typeAnalysis = $functionsAnalyzer->getFunctionReturnType($tokens, $dataProviderAnalysis->getNameIndex());
83-
84-
if ($typeAnalysis === null) {
85-
$argumentsStart = $tokens->getNextTokenOfKind($dataProviderAnalysis->getNameIndex(), ['(']);
86-
\assert(\is_int($argumentsStart));
87-
$argumentsEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $argumentsStart);
88-
$tokens->insertAt(
89-
$argumentsEnd + 1,
90-
[
91-
new Token([CT::T_TYPE_COLON, ':']),
92-
new Token([\T_WHITESPACE, ' ']),
93-
new Token([\T_STRING, 'iterable']),
94-
],
95-
);
96-
continue;
97-
}
98-
99-
if ($typeAnalysis->getName() !== 'iterable') {
100-
$startIndex = $tokens->getNextMeaningfulToken($typeAnalysis->getStartIndex() - 1);
101-
\assert(\is_int($startIndex));
102-
$tokens->overrideRange($startIndex, $typeAnalysis->getEndIndex(), [new Token([\T_STRING, 'iterable'])]);
103-
}
104-
}
83+
return [$this->phpUnitDataProviderReturnTypeFixer->getName()];
10584
}
10685
}

tests/Fixer/DataProviderReturnTypeFixerTest.php

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@
1111

1212
namespace Tests\Fixer;
1313

14+
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
15+
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
16+
1417
/**
1518
* @internal
1619
*
20+
* @property ConfigurableFixerInterface&DeprecatedFixerInterface $fixer
21+
*
1722
* @covers \PhpCsFixerCustomFixers\Fixer\DataProviderReturnTypeFixer
1823
*/
1924
final class DataProviderReturnTypeFixerTest extends AbstractFixerTestCase
@@ -38,6 +43,11 @@ public function testIsRisky(): void
3843
self::assertTrue($this->fixer->isRisky());
3944
}
4045

46+
public function testSuccessorName(): void
47+
{
48+
self::assertContains('php_unit_data_provider_return_type', $this->fixer->getSuccessorsNames());
49+
}
50+
4151
/**
4252
* @dataProvider provideFixCases
4353
*/
@@ -207,32 +217,6 @@ public function provideFooCases() {}
207217
}
208218
}
209219

210-
/**
211-
* @requires PHP ^7.4
212-
*
213-
* @dataProvider provideFix7Cases
214-
*/
215-
public function testFix7(string $expected, ?string $input = null): void
216-
{
217-
$this->doTest($expected, $input);
218-
}
219-
220-
/**
221-
* @return iterable<array{string, string}>
222-
*/
223-
public static function provideFix7Cases(): iterable
224-
{
225-
yield 'data provider with return type namespaced class starting with iterable' => self::mapToTemplate(
226-
': iterable',
227-
': iterable \ Foo',
228-
);
229-
230-
yield 'data provider with return type namespaced class and comments' => self::mapToTemplate(
231-
': iterable',
232-
': Foo/* Some info */\/* More info */Bar',
233-
);
234-
}
235-
236220
/**
237221
* @return array{string, string}
238222
*/

0 commit comments

Comments
 (0)