Skip to content

Commit 6115bff

Browse files
authored
Get all data from proxied fixers (#1044)
1 parent 84bbc96 commit 6115bff

11 files changed

+52
-203
lines changed

.dev-tools/src/Readme/ReadmeCommand.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,19 @@ private function fixers(): string
198198
$fixer->configure(['force' => true]);
199199
}
200200
if ($fixer->isRisky()) {
201+
$riskyDescription = $fixer->getDefinition()->getRiskyDescription();
202+
$starts = [
203+
'Risky when' => 'when',
204+
'Fixer could be risky if' => 'when',
205+
];
206+
foreach ($starts as $from => $to) {
207+
if (\str_starts_with($riskyDescription, $from)) {
208+
$riskyDescription = $to . \substr($riskyDescription, \strlen($from));
209+
}
210+
}
201211
$output .= \sprintf(
202212
"\n *Risky: %s.*",
203-
$fixer->getDefinition()->getRiskyDescription(),
213+
\lcfirst(\rtrim($riskyDescription, '.')),
204214
);
205215
}
206216
if ($fixer instanceof DataProviderStaticFixer) {

README.md

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ Constructor's empty braces must be on a single line.
9494
#### DataProviderNameFixer
9595
Data provider names must match the name of the test.
9696
DEPRECATED: use `php_unit_data_provider_name` instead.
97-
*Risky: when relying on name of data provider function.*
97+
*Risky: when one is calling data provider by name as function.*
9898
Configuration options:
9999
- `prefix` (`string`): prefix that replaces "test"; defaults to `'provide'`
100-
- `suffix` (`string`): suffix to be added at the end"; defaults to `'Cases'`
100+
- `suffix` (`string`): suffix to be present at the end; defaults to `'Cases'`
101101
```diff
102102
<?php
103103
class FooTest extends TestCase {
@@ -114,7 +114,7 @@ Configuration options:
114114
#### DataProviderReturnTypeFixer
115115
The return type of PHPUnit data provider must be `iterable`.
116116
DEPRECATED: use `php_unit_data_provider_return_type` instead.
117-
*Risky: when relying on signature of data provider.*
117+
*Risky: when relying on signature of the data provider.*
118118
```diff
119119
<?php
120120
class FooTest extends TestCase {
@@ -130,9 +130,9 @@ The return type of PHPUnit data provider must be `iterable`.
130130
#### DataProviderStaticFixer
131131
Data providers must be static.
132132
DEPRECATED: use `php_unit_data_provider_static` instead.
133-
*Risky: when `force` is set to `true`.*
133+
*Risky: when one is calling data provider function dynamically.*
134134
Configuration options:
135-
- `force` (`bool`): whether to make static data providers having dynamic class calls; defaults to `false`
135+
- `force` (`bool`): whether to make the data providers static even if they have a dynamic class call (may introduce fatal error "using $this when not in object context", and you may have to adjust the code manually by converting dynamic calls to static ones); defaults to `false`
136136
```diff
137137
<?php
138138
class FooTest extends TestCase {
@@ -178,12 +178,12 @@ Value from `foreach` must not be used if possible.
178178
```
179179

180180
#### InternalClassCasingFixer
181-
Classes defined internally by an extension or the core must be referenced with the correct case.
181+
When referencing an internal class it must be written using the correct casing.
182182
DEPRECATED: use `class_reference_name_casing` instead.
183183
```diff
184184
<?php
185-
-$foo = new STDClass();
186-
+$foo = new stdClass();
185+
-throw new \exception();
186+
+throw new \Exception();
187187
```
188188

189189
#### IssetToArrayKeyExistsFixer
@@ -513,15 +513,16 @@ Assertions and attributes for PHP and PHPUnit versions must have explicit versio
513513
```
514514

515515
#### PhpdocArrayStyleFixer
516-
Generic array style should be used in PHPDoc.
516+
PHPDoc `array<T>` type must be used instead of `T[]`.
517517
DEPRECATED: use `phpdoc_array_type` instead.
518518
```diff
519519
<?php
520520
/**
521-
- * @return int[]
522-
+ * @return array<int>
521+
- * @param int[] $x
522+
- * @param string[][] $y
523+
+ * @param array<int> $x
524+
+ * @param array<array<string>> $y
523525
*/
524-
function foo() { return [1, 2]; }
525526
```
526527

527528
#### PhpdocNoIncorrectVarAnnotationFixer
@@ -566,12 +567,14 @@ Orders all `@param` annotations in DocBlocks according to method signature.
566567
```diff
567568
<?php
568569
/**
569-
+ * @param int $a
570-
* @param int $b
571-
- * @param int $a
572-
* @param int $c
570+
* Annotations in wrong order
571+
*
572+
* @param int $a
573+
+ * @param array $b
574+
* @param Foo $c
575+
- * @param array $b
573576
*/
574-
function foo($a, $b, $c) {}
577+
function m($a, array $b, Foo $c) {}
575578
```
576579

577580
#### PhpdocParamTypeFixer
@@ -630,15 +633,16 @@ Configuration options:
630633
```
631634

632635
#### PhpdocTypeListFixer
633-
PHPDoc type `list` must be used instead of `array` without a key type.
636+
PHPDoc `list` type must be used instead of `array` without a key.
634637
DEPRECATED: use `phpdoc_list_type` instead.
635638
```diff
636639
<?php
637640
/**
638-
- * @param array<string>
639-
+ * @param list<string>
641+
- * @param array<int> $x
642+
- * @param array<array<string>> $y
643+
+ * @param list<int> $x
644+
+ * @param list<list<string>> $y
640645
*/
641-
function foo($x) {}
642646
```
643647

644648
#### PhpdocTypesCommaSpacesFixer

src/Fixer/DataProviderNameFixer.php

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,20 @@
1414
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
1515
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
1616
use PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderNameFixer;
17-
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
1817
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
19-
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
20-
use PhpCsFixer\FixerDefinition\CodeSample;
21-
use PhpCsFixer\FixerDefinition\FixerDefinition;
2218
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
2319
use PhpCsFixer\Tokenizer\Tokens;
2420

2521
/**
2622
* @deprecated
2723
*
28-
* @implements ConfigurableFixerInterface<_InputConfig, _Config>
29-
*
30-
* @phpstan-type _InputConfig array{prefix?: string, suffix?: string}
31-
* @phpstan-type _Config array{prefix: string, suffix: string}
24+
* @implements ConfigurableFixerInterface<array{prefix?: string, suffix?: string}, array{prefix: string, suffix: string}>
3225
*
3326
* @no-named-arguments
3427
*/
3528
final class DataProviderNameFixer extends AbstractFixer implements ConfigurableFixerInterface, DeprecatedFixerInterface
3629
{
3730
private PhpUnitDataProviderNameFixer $phpUnitDataProviderNameFixer;
38-
private string $prefix = 'provide';
39-
private string $suffix = 'Cases';
4031

4132
public function __construct()
4233
{
@@ -45,38 +36,12 @@ public function __construct()
4536

4637
public function getDefinition(): FixerDefinitionInterface
4738
{
48-
return new FixerDefinition(
49-
$this->phpUnitDataProviderNameFixer->getDefinition()->getSummary(),
50-
[
51-
new CodeSample(
52-
'<?php
53-
class FooTest extends TestCase {
54-
/**
55-
* @dataProvider dataProvider
56-
*/
57-
public function testSomething($expected, $actual) {}
58-
public function dataProvider() {}
59-
}
60-
',
61-
),
62-
],
63-
'',
64-
'when relying on name of data provider function',
65-
);
39+
return $this->phpUnitDataProviderNameFixer->getDefinition();
6640
}
6741

6842
public function getConfigurationDefinition(): FixerConfigurationResolverInterface
6943
{
70-
return new FixerConfigurationResolver([
71-
(new FixerOptionBuilder('prefix', 'prefix that replaces "test"'))
72-
->setAllowedTypes(['string'])
73-
->setDefault($this->prefix)
74-
->getOption(),
75-
(new FixerOptionBuilder('suffix', 'suffix to be added at the end"'))
76-
->setAllowedTypes(['string'])
77-
->setDefault($this->suffix)
78-
->getOption(),
79-
]);
44+
return $this->phpUnitDataProviderNameFixer->getConfigurationDefinition();
8045
}
8146

8247
public function configure(array $configuration): void
@@ -104,9 +69,6 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
10469
$this->phpUnitDataProviderNameFixer->fix($file, $tokens);
10570
}
10671

107-
/**
108-
* @return list<string>
109-
*/
11072
public function getSuccessorsNames(): array
11173
{
11274
return [$this->phpUnitDataProviderNameFixer->getName()];

src/Fixer/DataProviderReturnTypeFixer.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
1515
use PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderReturnTypeFixer;
16-
use PhpCsFixer\FixerDefinition\CodeSample;
17-
use PhpCsFixer\FixerDefinition\FixerDefinition;
1816
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
1917
use PhpCsFixer\Tokenizer\Tokens;
2018

@@ -34,24 +32,7 @@ public function __construct()
3432

3533
public function getDefinition(): FixerDefinitionInterface
3634
{
37-
return new FixerDefinition(
38-
$this->phpUnitDataProviderReturnTypeFixer->getDefinition()->getSummary(),
39-
[
40-
new CodeSample(
41-
'<?php
42-
class FooTest extends TestCase {
43-
/**
44-
* @dataProvider provideSomethingCases
45-
*/
46-
public function testSomething($expected, $actual) {}
47-
public function provideSomethingCases(): array {}
48-
}
49-
',
50-
),
51-
],
52-
'',
53-
'when relying on signature of data provider',
54-
);
35+
return $this->phpUnitDataProviderReturnTypeFixer->getDefinition();
5536
}
5637

5738
/**
@@ -77,9 +58,6 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
7758
$this->phpUnitDataProviderReturnTypeFixer->fix($file, $tokens);
7859
}
7960

80-
/**
81-
* @return list<string>
82-
*/
8361
public function getSuccessorsNames(): array
8462
{
8563
return [$this->phpUnitDataProviderReturnTypeFixer->getName()];

src/Fixer/DataProviderStaticFixer.php

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,19 @@
1414
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
1515
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
1616
use PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderStaticFixer;
17-
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
1817
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
19-
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
20-
use PhpCsFixer\FixerDefinition\CodeSample;
21-
use PhpCsFixer\FixerDefinition\FixerDefinition;
2218
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
2319
use PhpCsFixer\Tokenizer\Tokens;
2420

2521
/**
2622
* @deprecated
2723
*
28-
* @implements ConfigurableFixerInterface<_InputConfig, _Config>
29-
*
30-
* @phpstan-type _InputConfig array{force?: bool}
31-
* @phpstan-type _Config array{force: bool}
24+
* @implements ConfigurableFixerInterface<array{force?: bool}, array{force: bool}>
3225
*
3326
* @no-named-arguments
3427
*/
3528
final class DataProviderStaticFixer extends AbstractFixer implements ConfigurableFixerInterface, DeprecatedFixerInterface
3629
{
37-
private bool $force = false;
3830
private PhpUnitDataProviderStaticFixer $phpUnitDataProviderStaticFixer;
3931

4032
public function __construct()
@@ -44,45 +36,17 @@ public function __construct()
4436

4537
public function getDefinition(): FixerDefinitionInterface
4638
{
47-
return new FixerDefinition(
48-
$this->phpUnitDataProviderStaticFixer->getDefinition()->getSummary(),
49-
[
50-
new CodeSample(
51-
'<?php
52-
class FooTest extends TestCase {
53-
/**
54-
* @dataProvider provideSomethingCases
55-
*/
56-
public function testSomething($expected, $actual) {}
57-
public function provideSomethingCases() {}
58-
}
59-
',
60-
),
61-
],
62-
'',
63-
'when `force` is set to `true`',
64-
);
39+
return $this->phpUnitDataProviderStaticFixer->getDefinition();
6540
}
6641

6742
public function getConfigurationDefinition(): FixerConfigurationResolverInterface
6843
{
69-
return new FixerConfigurationResolver([
70-
(new FixerOptionBuilder('force', 'whether to make static data providers having dynamic class calls'))
71-
->setAllowedTypes(['bool'])
72-
->setDefault($this->force)
73-
->getOption(),
74-
]);
44+
return $this->phpUnitDataProviderStaticFixer->getConfigurationDefinition();
7545
}
7646

77-
/**
78-
* @param array<string, bool> $configuration
79-
*/
8047
public function configure(array $configuration): void
8148
{
82-
if (\array_key_exists('force', $configuration)) {
83-
$this->force = $configuration['force'];
84-
}
85-
$this->phpUnitDataProviderStaticFixer->configure(['force' => $this->force]);
49+
$this->phpUnitDataProviderStaticFixer->configure($configuration);
8650
}
8751

8852
public function getPriority(): int
@@ -105,9 +69,6 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
10569
$this->phpUnitDataProviderStaticFixer->fix($file, $tokens);
10670
}
10771

108-
/**
109-
* @return list<string>
110-
*/
11172
public function getSuccessorsNames(): array
11273
{
11374
return [$this->phpUnitDataProviderStaticFixer->getName()];

src/Fixer/InternalClassCasingFixer.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
use PhpCsFixer\Fixer\Casing\ClassReferenceNameCasingFixer;
1515
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
16-
use PhpCsFixer\FixerDefinition\CodeSample;
17-
use PhpCsFixer\FixerDefinition\FixerDefinition;
1816
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
1917
use PhpCsFixer\Tokenizer\Tokens;
2018

@@ -34,11 +32,7 @@ public function __construct()
3432

3533
public function getDefinition(): FixerDefinitionInterface
3634
{
37-
return new FixerDefinition(
38-
'Classes defined internally by an extension or the core must be referenced with the correct case.',
39-
[new CodeSample("<?php\n\$foo = new STDClass();\n")],
40-
'',
41-
);
35+
return $this->classReferenceNameCasingFixer->getDefinition();
4236
}
4337

4438
public function getPriority(): int
@@ -61,9 +55,6 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
6155
$this->classReferenceNameCasingFixer->fix($file, $tokens);
6256
}
6357

64-
/**
65-
* @return list<string>
66-
*/
6758
public function getSuccessorsNames(): array
6859
{
6960
return [$this->classReferenceNameCasingFixer->getName()];

0 commit comments

Comments
 (0)