Skip to content

Commit a3d990e

Browse files
authored
DataProviderStaticFixer - allow forcing static method when having dynamic call (#741)
1 parent 8e766ba commit a3d990e

File tree

4 files changed

+87
-11
lines changed

4 files changed

+87
-11
lines changed

CHANGELOG.md

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

3+
## v3.8.0
4+
- DataProviderStaticFixer - add option "force"
5+
36
## v3.7.0
47
- Add NoTrailingCommaInSinglelineFixer
5-
-
8+
69
## v3.6.0
710
- Add IssetToArrayKeyExistsFixer
811
- Add PhpdocVarAnnotationToAssertFixer

README.md

Lines changed: 4 additions & 1 deletion
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-3370-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-3373-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)
@@ -105,6 +105,9 @@ The types returned by data providers must be `iterable`.
105105

106106
#### DataProviderStaticFixer
107107
Data providers must be static.
108+
*Risky: when `force` is set to `true`.*
109+
Configuration options:
110+
- `force` (`bool`): whether to make static data providers having dynamic class calls; defaults to `false`
108111
```diff
109112
<?php
110113
class FooTest extends TestCase {

src/Fixer/DataProviderStaticFixer.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
namespace PhpCsFixerCustomFixers\Fixer;
1313

14+
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
15+
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
16+
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
17+
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
1418
use PhpCsFixer\FixerDefinition\CodeSample;
1519
use PhpCsFixer\FixerDefinition\FixerDefinition;
1620
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
@@ -20,8 +24,11 @@
2024
use PhpCsFixer\Tokenizer\TokensAnalyzer;
2125
use PhpCsFixerCustomFixers\Analyzer\DataProviderAnalyzer;
2226

23-
final class DataProviderStaticFixer extends AbstractFixer
27+
final class DataProviderStaticFixer extends AbstractFixer implements ConfigurableFixerInterface
2428
{
29+
/** @var bool */
30+
private $force = false;
31+
2532
public function getDefinition(): FixerDefinitionInterface
2633
{
2734
return new FixerDefinition(
@@ -38,10 +45,32 @@ public function provideSomethingCases() {}
3845
}
3946
'
4047
),
41-
]
48+
],
49+
null,
50+
'when `force` is set to `true`'
4251
);
4352
}
4453

54+
public function getConfigurationDefinition(): FixerConfigurationResolverInterface
55+
{
56+
return new FixerConfigurationResolver([
57+
(new FixerOptionBuilder('force', 'whether to make static data providers having dynamic class calls'))
58+
->setAllowedTypes(['bool'])
59+
->setDefault($this->force)
60+
->getOption(),
61+
]);
62+
}
63+
64+
/**
65+
* @param array<string, bool> $configuration
66+
*/
67+
public function configure(array $configuration): void
68+
{
69+
if (\array_key_exists('force', $configuration)) {
70+
$this->force = $configuration['force'];
71+
}
72+
}
73+
4574
public function getPriority(): int
4675
{
4776
return 0;
@@ -54,7 +83,7 @@ public function isCandidate(Tokens $tokens): bool
5483

5584
public function isRisky(): bool
5685
{
57-
return false;
86+
return true;
5887
}
5988

6089
public function fix(\SplFileInfo $file, Tokens $tokens): void
@@ -77,7 +106,7 @@ private function fixStatic(Tokens $tokens, int $startIndex, int $endIndex): void
77106
if ($methodStartIndex !== null) {
78107
$methodEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $methodStartIndex);
79108

80-
if ($tokens->findSequence([[\T_VARIABLE, '$this']], $methodStartIndex, $methodEndIndex) !== null) {
109+
if (!$this->force && $tokens->findSequence([[\T_VARIABLE, '$this']], $methodStartIndex, $methodEndIndex) !== null) {
81110
continue;
82111
}
83112
}

tests/Fixer/DataProviderStaticFixerTest.php

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,63 @@ final class DataProviderStaticFixerTest extends AbstractFixerTestCase
2020
{
2121
public function testIsRisky(): void
2222
{
23-
self::assertFalse($this->fixer->isRisky());
23+
self::assertTrue($this->fixer->isRisky());
24+
}
25+
26+
public function testConfiguration(): void
27+
{
28+
$options = $this->fixer->getConfigurationDefinition()->getOptions();
29+
self::assertArrayHasKey(0, $options);
30+
self::assertSame('force', $options[0]->getName());
2431
}
2532

2633
/**
34+
* @param null|array<string, bool> $configuration
2735
* @dataProvider provideFixCases
2836
*/
29-
public function testFix(string $expected, ?string $input = null): void
37+
public function testFix(string $expected, ?string $input = null, ?array $configuration = null): void
3038
{
31-
$this->doTest($expected, $input);
39+
$this->doTest($expected, $input, $configuration);
3240
}
3341

3442
/**
35-
* @return iterable<array{0: string, 1?: string}>
43+
* @return iterable<array{0: string, 1?: null|string, 2?: array<string, bool>}>
3644
*/
3745
public static function provideFixCases(): iterable
3846
{
39-
yield 'do not fix when containing dynamic calls' => [
47+
yield 'do not fix when containing dynamic calls by default' => [
48+
'<?php
49+
class FooTest extends TestCase {
50+
/**
51+
* @dataProvider provideFoo1Cases
52+
*/
53+
public function testFoo1() {}
54+
public function provideFoo1Cases() { $this->init(); }
55+
}',
56+
];
57+
58+
yield 'do not fix when containing dynamic calls and with `force` disabled' => [
59+
'<?php
60+
class FooTest extends TestCase {
61+
/**
62+
* @dataProvider provideFoo1Cases
63+
*/
64+
public function testFoo1() {}
65+
public function provideFoo1Cases() { $this->init(); }
66+
}',
67+
null,
68+
['force' => false],
69+
];
70+
71+
yield 'fix when containing dynamic calls and with `force` enabled' => [
72+
'<?php
73+
class FooTest extends TestCase {
74+
/**
75+
* @dataProvider provideFoo1Cases
76+
*/
77+
public function testFoo1() {}
78+
public static function provideFoo1Cases() { $this->init(); }
79+
}',
4080
'<?php
4181
class FooTest extends TestCase {
4282
/**
@@ -45,6 +85,7 @@ class FooTest extends TestCase {
4585
public function testFoo1() {}
4686
public function provideFoo1Cases() { $this->init(); }
4787
}',
88+
['force' => true],
4889
];
4990

5091
yield 'fix single' => [

0 commit comments

Comments
 (0)