-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1772f07
commit c285526
Showing
23 changed files
with
292 additions
and
565 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* Dariusz Rumiński <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace PhpCsFixer\Fixer; | ||
|
||
use PhpCsFixer\AbstractFixer; | ||
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
abstract class AbstractPhpUnitFixer extends AbstractFixer | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
final public function isCandidate(Tokens $tokens) | ||
{ | ||
return $tokens->isAllTokenKindsFound([T_CLASS, T_STRING]); | ||
} | ||
|
||
final protected function applyFix(\SplFileInfo $file, Tokens $tokens) | ||
{ | ||
$phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator(); | ||
|
||
foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indices) { | ||
$this->applyPhpUnitClassFix($tokens, $indices[0], $indices[1]); | ||
} | ||
} | ||
|
||
/** | ||
* @param int $startIndex | ||
* @param int $endIndex | ||
*/ | ||
abstract protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex); | ||
|
||
/** | ||
* @param int $index | ||
* | ||
* @return int | ||
*/ | ||
final protected function getDocBlockIndex(Tokens $tokens, $index) | ||
{ | ||
do { | ||
$index = $tokens->getPrevNonWhitespace($index); | ||
} while ($tokens[$index]->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE, T_FINAL, T_ABSTRACT, T_COMMENT])); | ||
|
||
return $index; | ||
} | ||
|
||
/** | ||
* @param int $index | ||
* | ||
* @return bool | ||
*/ | ||
final protected function isPHPDoc(Tokens $tokens, $index) | ||
{ | ||
return $tokens[$index]->isGivenKind(T_DOC_COMMENT); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,22 +12,21 @@ | |
|
||
namespace PhpCsFixer\Fixer\PhpUnit; | ||
|
||
use PhpCsFixer\AbstractFixer; | ||
use PhpCsFixer\Fixer\AbstractPhpUnitFixer; | ||
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface; | ||
use PhpCsFixer\FixerConfiguration\AllowedValueSubset; | ||
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverRootless; | ||
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder; | ||
use PhpCsFixer\FixerDefinition\CodeSample; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator; | ||
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer; | ||
use PhpCsFixer\Tokenizer\Token; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
/** | ||
* @author Dariusz Rumiński <[email protected]> | ||
*/ | ||
final class PhpUnitConstructFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface | ||
final class PhpUnitConstructFixer extends AbstractPhpUnitFixer implements ConfigurationDefinitionFixerInterface | ||
{ | ||
private static $assertionFixers = [ | ||
'assertSame' => 'fixAssertPositive', | ||
|
@@ -36,14 +35,6 @@ final class PhpUnitConstructFixer extends AbstractFixer implements Configuration | |
'assertNotSame' => 'fixAssertNegative', | ||
]; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isCandidate(Tokens $tokens) | ||
{ | ||
return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION, T_STRING]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -104,25 +95,21 @@ public function getPriority() | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function applyFix(\SplFileInfo $file, Tokens $tokens) | ||
protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex) | ||
{ | ||
// no assertions to be fixed - fast return | ||
if (empty($this->configuration['assertions'])) { | ||
return; | ||
} | ||
|
||
$phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator(); | ||
|
||
foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indexes) { | ||
foreach ($this->configuration['assertions'] as $assertionMethod) { | ||
$assertionFixer = self::$assertionFixers[$assertionMethod]; | ||
foreach ($this->configuration['assertions'] as $assertionMethod) { | ||
$assertionFixer = self::$assertionFixers[$assertionMethod]; | ||
|
||
for ($index = $indexes[0]; $index < $indexes[1]; ++$index) { | ||
$index = $this->{$assertionFixer}($tokens, $index, $assertionMethod); | ||
for ($index = $startIndex; $index < $endIndex; ++$index) { | ||
$index = $this->{$assertionFixer}($tokens, $index, $assertionMethod); | ||
|
||
if (null === $index) { | ||
break; | ||
} | ||
if (null === $index) { | ||
break; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
|
||
namespace PhpCsFixer\Fixer\PhpUnit; | ||
|
||
use PhpCsFixer\AbstractFixer; | ||
use PhpCsFixer\Fixer\AbstractPhpUnitFixer; | ||
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface; | ||
use PhpCsFixer\FixerConfiguration\AllowedValueSubset; | ||
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverRootless; | ||
|
@@ -27,7 +27,7 @@ | |
* @author SpacePossum | ||
* @author Dariusz Rumiński <[email protected]> | ||
*/ | ||
final class PhpUnitDedicateAssertFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface | ||
final class PhpUnitDedicateAssertFixer extends AbstractPhpUnitFixer implements ConfigurationDefinitionFixerInterface | ||
{ | ||
private static $fixMap = [ | ||
'array_key_exists' => ['assertArrayNotHasKey', 'assertArrayHasKey'], | ||
|
@@ -120,14 +120,6 @@ public function configure(array $configuration = null) | |
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isCandidate(Tokens $tokens) | ||
{ | ||
return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION, T_STRING]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -190,11 +182,9 @@ public function getPriority() | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function applyFix(\SplFileInfo $file, Tokens $tokens) | ||
protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex) | ||
{ | ||
//findPhpUnitClasses | ||
|
||
foreach ($this->getPreviousAssertCall($tokens) as $assertCall) { | ||
foreach ($this->getPreviousAssertCall($tokens, $startIndex, $endIndex) as $assertCall) { | ||
// test and fix for assertTrue/False to dedicated asserts | ||
if ('asserttrue' === $assertCall['loweredName'] || 'assertfalse' === $assertCall['loweredName']) { | ||
$this->fixAssertTrueFalse($tokens, $assertCall); | ||
|
@@ -397,11 +387,15 @@ private function fixAssertSameEquals(Tokens $tokens, array $assertCall) | |
]); | ||
} | ||
|
||
private function getPreviousAssertCall(Tokens $tokens) | ||
/** | ||
* @param int $startIndex | ||
* @param int $endIndex | ||
*/ | ||
private function getPreviousAssertCall(Tokens $tokens, $startIndex, $endIndex) | ||
{ | ||
$functionsAnalyzer = new FunctionsAnalyzer(); | ||
|
||
for ($index = $tokens->count(); $index > 0; --$index) { | ||
for ($index = $endIndex; $index > $startIndex; --$index) { | ||
$index = $tokens->getPrevTokenOfKind($index, [[T_STRING]]); | ||
if (null === $index) { | ||
return; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,21 +12,20 @@ | |
|
||
namespace PhpCsFixer\Fixer\PhpUnit; | ||
|
||
use PhpCsFixer\AbstractFixer; | ||
use PhpCsFixer\Fixer\AbstractPhpUnitFixer; | ||
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface; | ||
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver; | ||
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder; | ||
use PhpCsFixer\FixerDefinition\CodeSample; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator; | ||
use PhpCsFixer\Tokenizer\Token; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
use PhpCsFixer\Tokenizer\TokensAnalyzer; | ||
|
||
/** | ||
* @author Filippo Tessarotto <[email protected]> | ||
*/ | ||
final class PhpUnitDedicateAssertInternalTypeFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface | ||
final class PhpUnitDedicateAssertInternalTypeFixer extends AbstractPhpUnitFixer implements ConfigurationDefinitionFixerInterface | ||
{ | ||
/** | ||
* @var array | ||
|
@@ -76,14 +75,6 @@ public function testMe() | |
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isCandidate(Tokens $tokens) | ||
{ | ||
return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION, T_STRING]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -102,17 +93,6 @@ public function getPriority() | |
return -16; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function applyFix(\SplFileInfo $file, Tokens $tokens) | ||
{ | ||
$phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator(); | ||
foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indexes) { | ||
$this->updateAssertInternalTypeMethods($tokens, $indexes[0], $indexes[1]); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -128,10 +108,9 @@ protected function createConfigurationDefinition() | |
} | ||
|
||
/** | ||
* @param int $startIndex | ||
* @param int $endIndex | ||
* {@inheritdoc} | ||
*/ | ||
private function updateAssertInternalTypeMethods(Tokens $tokens, $startIndex, $endIndex) | ||
protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex) | ||
{ | ||
$anonymousClassIndexes = []; | ||
$tokenAnalyzer = new TokensAnalyzer($tokens); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,22 +12,21 @@ | |
|
||
namespace PhpCsFixer\Fixer\PhpUnit; | ||
|
||
use PhpCsFixer\AbstractFixer; | ||
use PhpCsFixer\Fixer\AbstractPhpUnitFixer; | ||
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface; | ||
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface; | ||
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver; | ||
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder; | ||
use PhpCsFixer\FixerDefinition\CodeSample; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator; | ||
use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer; | ||
use PhpCsFixer\Tokenizer\Token; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
/** | ||
* @author Dariusz Rumiński <[email protected]> | ||
*/ | ||
final class PhpUnitExpectationFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface, WhitespacesAwareFixerInterface | ||
final class PhpUnitExpectationFixer extends AbstractPhpUnitFixer implements ConfigurationDefinitionFixerInterface, WhitespacesAwareFixerInterface | ||
{ | ||
/** | ||
* @var array<string, string> | ||
|
@@ -130,14 +129,6 @@ public function getPriority() | |
return 0; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isCandidate(Tokens $tokens) | ||
{ | ||
return $tokens->isTokenKindFound(T_CLASS); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -146,17 +137,6 @@ public function isRisky() | |
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function applyFix(\SplFileInfo $file, Tokens $tokens) | ||
{ | ||
$phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator(); | ||
foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indexes) { | ||
$this->fixExpectation($tokens, $indexes[0], $indexes[1]); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -171,7 +151,10 @@ protected function createConfigurationDefinition() | |
]); | ||
} | ||
|
||
private function fixExpectation(Tokens $tokens, $startIndex, $endIndex) | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex) | ||
{ | ||
$argumentsAnalyzer = new ArgumentsAnalyzer(); | ||
|
||
|
Oops, something went wrong.