Skip to content

Commit e221af8

Browse files
authored
Deprecate DataProviderNameFixer (#907)
1 parent 4fac22f commit e221af8

File tree

5 files changed

+45
-85
lines changed

5 files changed

+45
-85
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# CHANGELOG for PHP CS Fixer: custom fixers
22

33
## v3.15.0
4+
- Deprecate DataProviderNameFixer - use "php_unit_data_provider_name"
45
- Deprecate PhpdocParamOrderFixer
5-
- Update minimum PHP CS Fixer version to 3.17.0
6+
- Update minimum PHP CS Fixer version to 3.19.0
67

78
## v3.14.0
89
- Add EmptyFunctionBodyFixer

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-3444-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)
@@ -75,7 +75,8 @@ Constructor's empty braces must be single line.
7575
```
7676

7777
#### DataProviderNameFixer
78-
Data provider names used only once must match the name of the test.
78+
Data provider names must match the name of the test.
79+
DEPRECATED: use `php_unit_data_provider_name` instead.
7980
*Risky: when relying on name of data provider function.*
8081
Configuration options:
8182
- `prefix` (`string`): prefix that replaces "test"; defaults to `'provide'`

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

src/Fixer/DataProviderNameFixer.php

Lines changed: 24 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,28 @@
1212
namespace PhpCsFixerCustomFixers\Fixer;
1313

1414
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
15+
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
16+
use PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderNameFixer;
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\Preg;
23-
use PhpCsFixer\Tokenizer\Token;
2423
use 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
}

tests/Fixer/DataProviderNameFixerTest.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
namespace Tests\Fixer;
1313

1414
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
15+
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
1516
use PhpCsFixer\FixerConfiguration\FixerOptionInterface;
1617

1718
/**
1819
* @internal
1920
*
20-
* @property ConfigurableFixerInterface $fixer
21+
* @property ConfigurableFixerInterface&DeprecatedFixerInterface $fixer
2122
*
2223
* @covers \PhpCsFixerCustomFixers\Fixer\DataProviderNameFixer
2324
*/
@@ -40,6 +41,11 @@ public function testIsRisky(): void
4041
self::assertTrue($this->fixer->isRisky());
4142
}
4243

44+
public function testSuccessorName(): void
45+
{
46+
self::assertContains('php_unit_data_provider_name', $this->fixer->getSuccessorsNames());
47+
}
48+
4349
/**
4450
* @param null|array<string, string> $configuration
4551
*
@@ -177,16 +183,6 @@ public function notDataProvider() {}
177183

178184
yield 'multiple data providers for test' => [
179185
'<?php
180-
class FooTest extends TestCase {
181-
/**
182-
* @dataProvider provideFooCases
183-
* @dataProvider foo2DataProvider
184-
*/
185-
public function testFoo() {}
186-
public function provideFooCases() {}
187-
public function foo2DataProvider() {}
188-
}',
189-
'<?php
190186
class FooTest extends TestCase {
191187
/**
192188
* @dataProvider foo1DataProvider
@@ -235,13 +231,13 @@ function () { return true; };
235231
236232
/**
237233
* @dataProvider reusedDataProvider
238-
* @dataProvider provideFooCases
234+
* @dataProvider dataFooProvider
239235
*/
240236
public function testFoo() {}
241237
242238
/**
243239
* @dataProvider reusedDataProvider
244-
* @dataProvider provideBarCases
240+
* @dataProvider dataBarProvider
245241
*/
246242
public function testBar() {}
247243
@@ -254,8 +250,8 @@ public function provideBazCases() {}
254250
/** @dataProvider provideSomethingCases */
255251
public function testSomething() {}
256252
public function provideSomethingCases() {}
257-
public function provideFooCases() {}
258-
public function provideBarCases() {}
253+
public function dataFooProvider() {}
254+
public function dataBarProvider() {}
259255
}',
260256
'<?php
261257
class FooTest extends TestCase {
@@ -270,13 +266,13 @@ function () { return true; };
270266
271267
/**
272268
* @dataProvider reusedDataProvider
273-
* @dataProvider testFooProvider
269+
* @dataProvider dataFooProvider
274270
*/
275271
public function testFoo() {}
276272
277273
/**
278274
* @dataProvider reusedDataProvider
279-
* @dataProvider testBarProvider
275+
* @dataProvider dataBarProvider
280276
*/
281277
public function testBar() {}
282278
@@ -289,8 +285,8 @@ public function provideBazCases() {}
289285
/** @dataProvider someDataProvider */
290286
public function testSomething() {}
291287
public function someDataProvider() {}
292-
public function testFooProvider() {}
293-
public function testBarProvider() {}
288+
public function dataFooProvider() {}
289+
public function dataBarProvider() {}
294290
}',
295291
];
296292

0 commit comments

Comments
 (0)