Skip to content

Commit

Permalink
DX: add AbstractPhpUnitFixer
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos authored and SpacePossum committed Oct 22, 2020
1 parent 1772f07 commit c285526
Show file tree
Hide file tree
Showing 23 changed files with 292 additions and 565 deletions.
70 changes: 70 additions & 0 deletions src/Fixer/AbstractPhpUnitFixer.php
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);
}
}
31 changes: 9 additions & 22 deletions src/Fixer/PhpUnit/PhpUnitConstructFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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}
*/
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
26 changes: 10 additions & 16 deletions src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'],
Expand Down Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 4 additions & 25 deletions src/Fixer/PhpUnit/PhpUnitDedicateAssertInternalTypeFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,14 +75,6 @@ public function testMe()
);
}

/**
* {@inheritdoc}
*/
public function isCandidate(Tokens $tokens)
{
return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION, T_STRING]);
}

/**
* {@inheritdoc}
*/
Expand All @@ -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}
*/
Expand All @@ -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);
Expand Down
29 changes: 6 additions & 23 deletions src/Fixer/PhpUnit/PhpUnitExpectationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -130,14 +129,6 @@ public function getPriority()
return 0;
}

/**
* {@inheritdoc}
*/
public function isCandidate(Tokens $tokens)
{
return $tokens->isTokenKindFound(T_CLASS);
}

/**
* {@inheritdoc}
*/
Expand All @@ -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}
*/
Expand All @@ -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();

Expand Down
Loading

0 comments on commit c285526

Please sign in to comment.