Skip to content

Commit 31423d4

Browse files
committed
minor #5193 DX: add AbstractPhpUnitFixer (kubawerlos)
This PR was merged into the 2.15 branch. Discussion ---------- DX: add AbstractPhpUnitFixer Follow up to: #5190 //cc @SpacePossum Commits ------- c285526 DX: add AbstractPhpUnitFixer
2 parents 1772f07 + c285526 commit 31423d4

23 files changed

+292
-565
lines changed

src/Fixer/AbstractPhpUnitFixer.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHP CS Fixer.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
8+
*
9+
* This source file is subject to the MIT license that is bundled
10+
* with this source code in the file LICENSE.
11+
*/
12+
13+
namespace PhpCsFixer\Fixer;
14+
15+
use PhpCsFixer\AbstractFixer;
16+
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
17+
use PhpCsFixer\Tokenizer\Tokens;
18+
19+
/**
20+
* @internal
21+
*/
22+
abstract class AbstractPhpUnitFixer extends AbstractFixer
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
final public function isCandidate(Tokens $tokens)
28+
{
29+
return $tokens->isAllTokenKindsFound([T_CLASS, T_STRING]);
30+
}
31+
32+
final protected function applyFix(\SplFileInfo $file, Tokens $tokens)
33+
{
34+
$phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
35+
36+
foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indices) {
37+
$this->applyPhpUnitClassFix($tokens, $indices[0], $indices[1]);
38+
}
39+
}
40+
41+
/**
42+
* @param int $startIndex
43+
* @param int $endIndex
44+
*/
45+
abstract protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex);
46+
47+
/**
48+
* @param int $index
49+
*
50+
* @return int
51+
*/
52+
final protected function getDocBlockIndex(Tokens $tokens, $index)
53+
{
54+
do {
55+
$index = $tokens->getPrevNonWhitespace($index);
56+
} while ($tokens[$index]->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE, T_FINAL, T_ABSTRACT, T_COMMENT]));
57+
58+
return $index;
59+
}
60+
61+
/**
62+
* @param int $index
63+
*
64+
* @return bool
65+
*/
66+
final protected function isPHPDoc(Tokens $tokens, $index)
67+
{
68+
return $tokens[$index]->isGivenKind(T_DOC_COMMENT);
69+
}
70+
}

src/Fixer/PhpUnit/PhpUnitConstructFixer.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,21 @@
1212

1313
namespace PhpCsFixer\Fixer\PhpUnit;
1414

15-
use PhpCsFixer\AbstractFixer;
15+
use PhpCsFixer\Fixer\AbstractPhpUnitFixer;
1616
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
1717
use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
1818
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverRootless;
1919
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
2020
use PhpCsFixer\FixerDefinition\CodeSample;
2121
use PhpCsFixer\FixerDefinition\FixerDefinition;
22-
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
2322
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
2423
use PhpCsFixer\Tokenizer\Token;
2524
use PhpCsFixer\Tokenizer\Tokens;
2625

2726
/**
2827
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
2928
*/
30-
final class PhpUnitConstructFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface
29+
final class PhpUnitConstructFixer extends AbstractPhpUnitFixer implements ConfigurationDefinitionFixerInterface
3130
{
3231
private static $assertionFixers = [
3332
'assertSame' => 'fixAssertPositive',
@@ -36,14 +35,6 @@ final class PhpUnitConstructFixer extends AbstractFixer implements Configuration
3635
'assertNotSame' => 'fixAssertNegative',
3736
];
3837

39-
/**
40-
* {@inheritdoc}
41-
*/
42-
public function isCandidate(Tokens $tokens)
43-
{
44-
return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION, T_STRING]);
45-
}
46-
4738
/**
4839
* {@inheritdoc}
4940
*/
@@ -104,25 +95,21 @@ public function getPriority()
10495
/**
10596
* {@inheritdoc}
10697
*/
107-
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
98+
protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex)
10899
{
109100
// no assertions to be fixed - fast return
110101
if (empty($this->configuration['assertions'])) {
111102
return;
112103
}
113104

114-
$phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
115-
116-
foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indexes) {
117-
foreach ($this->configuration['assertions'] as $assertionMethod) {
118-
$assertionFixer = self::$assertionFixers[$assertionMethod];
105+
foreach ($this->configuration['assertions'] as $assertionMethod) {
106+
$assertionFixer = self::$assertionFixers[$assertionMethod];
119107

120-
for ($index = $indexes[0]; $index < $indexes[1]; ++$index) {
121-
$index = $this->{$assertionFixer}($tokens, $index, $assertionMethod);
108+
for ($index = $startIndex; $index < $endIndex; ++$index) {
109+
$index = $this->{$assertionFixer}($tokens, $index, $assertionMethod);
122110

123-
if (null === $index) {
124-
break;
125-
}
111+
if (null === $index) {
112+
break;
126113
}
127114
}
128115
}

src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace PhpCsFixer\Fixer\PhpUnit;
1414

15-
use PhpCsFixer\AbstractFixer;
15+
use PhpCsFixer\Fixer\AbstractPhpUnitFixer;
1616
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
1717
use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
1818
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverRootless;
@@ -27,7 +27,7 @@
2727
* @author SpacePossum
2828
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
2929
*/
30-
final class PhpUnitDedicateAssertFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface
30+
final class PhpUnitDedicateAssertFixer extends AbstractPhpUnitFixer implements ConfigurationDefinitionFixerInterface
3131
{
3232
private static $fixMap = [
3333
'array_key_exists' => ['assertArrayNotHasKey', 'assertArrayHasKey'],
@@ -120,14 +120,6 @@ public function configure(array $configuration = null)
120120
}
121121
}
122122

123-
/**
124-
* {@inheritdoc}
125-
*/
126-
public function isCandidate(Tokens $tokens)
127-
{
128-
return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION, T_STRING]);
129-
}
130-
131123
/**
132124
* {@inheritdoc}
133125
*/
@@ -190,11 +182,9 @@ public function getPriority()
190182
/**
191183
* {@inheritdoc}
192184
*/
193-
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
185+
protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex)
194186
{
195-
//findPhpUnitClasses
196-
197-
foreach ($this->getPreviousAssertCall($tokens) as $assertCall) {
187+
foreach ($this->getPreviousAssertCall($tokens, $startIndex, $endIndex) as $assertCall) {
198188
// test and fix for assertTrue/False to dedicated asserts
199189
if ('asserttrue' === $assertCall['loweredName'] || 'assertfalse' === $assertCall['loweredName']) {
200190
$this->fixAssertTrueFalse($tokens, $assertCall);
@@ -397,11 +387,15 @@ private function fixAssertSameEquals(Tokens $tokens, array $assertCall)
397387
]);
398388
}
399389

400-
private function getPreviousAssertCall(Tokens $tokens)
390+
/**
391+
* @param int $startIndex
392+
* @param int $endIndex
393+
*/
394+
private function getPreviousAssertCall(Tokens $tokens, $startIndex, $endIndex)
401395
{
402396
$functionsAnalyzer = new FunctionsAnalyzer();
403397

404-
for ($index = $tokens->count(); $index > 0; --$index) {
398+
for ($index = $endIndex; $index > $startIndex; --$index) {
405399
$index = $tokens->getPrevTokenOfKind($index, [[T_STRING]]);
406400
if (null === $index) {
407401
return;

src/Fixer/PhpUnit/PhpUnitDedicateAssertInternalTypeFixer.php

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,20 @@
1212

1313
namespace PhpCsFixer\Fixer\PhpUnit;
1414

15-
use PhpCsFixer\AbstractFixer;
15+
use PhpCsFixer\Fixer\AbstractPhpUnitFixer;
1616
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
1717
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
1818
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
1919
use PhpCsFixer\FixerDefinition\CodeSample;
2020
use PhpCsFixer\FixerDefinition\FixerDefinition;
21-
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
2221
use PhpCsFixer\Tokenizer\Token;
2322
use PhpCsFixer\Tokenizer\Tokens;
2423
use PhpCsFixer\Tokenizer\TokensAnalyzer;
2524

2625
/**
2726
* @author Filippo Tessarotto <zoeslam@gmail.com>
2827
*/
29-
final class PhpUnitDedicateAssertInternalTypeFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface
28+
final class PhpUnitDedicateAssertInternalTypeFixer extends AbstractPhpUnitFixer implements ConfigurationDefinitionFixerInterface
3029
{
3130
/**
3231
* @var array
@@ -76,14 +75,6 @@ public function testMe()
7675
);
7776
}
7877

79-
/**
80-
* {@inheritdoc}
81-
*/
82-
public function isCandidate(Tokens $tokens)
83-
{
84-
return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION, T_STRING]);
85-
}
86-
8778
/**
8879
* {@inheritdoc}
8980
*/
@@ -102,17 +93,6 @@ public function getPriority()
10293
return -16;
10394
}
10495

105-
/**
106-
* {@inheritdoc}
107-
*/
108-
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
109-
{
110-
$phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
111-
foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indexes) {
112-
$this->updateAssertInternalTypeMethods($tokens, $indexes[0], $indexes[1]);
113-
}
114-
}
115-
11696
/**
11797
* {@inheritdoc}
11898
*/
@@ -128,10 +108,9 @@ protected function createConfigurationDefinition()
128108
}
129109

130110
/**
131-
* @param int $startIndex
132-
* @param int $endIndex
111+
* {@inheritdoc}
133112
*/
134-
private function updateAssertInternalTypeMethods(Tokens $tokens, $startIndex, $endIndex)
113+
protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex)
135114
{
136115
$anonymousClassIndexes = [];
137116
$tokenAnalyzer = new TokensAnalyzer($tokens);

src/Fixer/PhpUnit/PhpUnitExpectationFixer.php

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,21 @@
1212

1313
namespace PhpCsFixer\Fixer\PhpUnit;
1414

15-
use PhpCsFixer\AbstractFixer;
15+
use PhpCsFixer\Fixer\AbstractPhpUnitFixer;
1616
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
1717
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
1818
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
1919
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
2020
use PhpCsFixer\FixerDefinition\CodeSample;
2121
use PhpCsFixer\FixerDefinition\FixerDefinition;
22-
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
2322
use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
2423
use PhpCsFixer\Tokenizer\Token;
2524
use PhpCsFixer\Tokenizer\Tokens;
2625

2726
/**
2827
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
2928
*/
30-
final class PhpUnitExpectationFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface, WhitespacesAwareFixerInterface
29+
final class PhpUnitExpectationFixer extends AbstractPhpUnitFixer implements ConfigurationDefinitionFixerInterface, WhitespacesAwareFixerInterface
3130
{
3231
/**
3332
* @var array<string, string>
@@ -130,14 +129,6 @@ public function getPriority()
130129
return 0;
131130
}
132131

133-
/**
134-
* {@inheritdoc}
135-
*/
136-
public function isCandidate(Tokens $tokens)
137-
{
138-
return $tokens->isTokenKindFound(T_CLASS);
139-
}
140-
141132
/**
142133
* {@inheritdoc}
143134
*/
@@ -146,17 +137,6 @@ public function isRisky()
146137
return true;
147138
}
148139

149-
/**
150-
* {@inheritdoc}
151-
*/
152-
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
153-
{
154-
$phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
155-
foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indexes) {
156-
$this->fixExpectation($tokens, $indexes[0], $indexes[1]);
157-
}
158-
}
159-
160140
/**
161141
* {@inheritdoc}
162142
*/
@@ -171,7 +151,10 @@ protected function createConfigurationDefinition()
171151
]);
172152
}
173153

174-
private function fixExpectation(Tokens $tokens, $startIndex, $endIndex)
154+
/**
155+
* {@inheritdoc}
156+
*/
157+
protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex)
175158
{
176159
$argumentsAnalyzer = new ArgumentsAnalyzer();
177160

0 commit comments

Comments
 (0)