[PHPUnit] Remove active* helper properties

This commit is contained in:
Tomas Votruba 2018-10-31 17:31:33 +01:00
parent de9063548f
commit a2d68ad756
5 changed files with 29 additions and 127 deletions

View File

@ -1,10 +0,0 @@
## PHPUnit Rectors
All methods are changes by default. But **you can specify methods** you like:
````yaml
services:
Rector\PHPUnit\Rector\SpecificMethod\AssertTrueFalseToSpecificMethodRector:
$activeMethods:
- 'is_file'
```

View File

@ -79,10 +79,6 @@ parameters:
# example in description
- 'src/Rector/Annotation/AnnotationReplacerRector.php'
# empty arguments passing
PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\ValidDefaultValueSniff.NotAtEnd:
- 'packages/PHPUnit/src/Rector/SpecificMethod/AssertTrueFalseToSpecificMethodRector.php'
Symplify\CodingStandard\Sniffs\CleanCode\ForbiddenStaticFunctionSniff:
# required by Composer interface
- 'tests/Composer/AutoloadWrongCasesEventSubscriber.php'

View File

@ -6,18 +6,12 @@ use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use Rector\Builder\IdentifierRenamer;
use Rector\Rector\AbstractPHPUnitRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class AssertCompareToSpecificMethodRector extends AbstractPHPUnitRector
{
/**
* @var string
*/
private $activeFuncCallName;
/**
* @var string[][]|false[][]
*/
@ -28,16 +22,6 @@ final class AssertCompareToSpecificMethodRector extends AbstractPHPUnitRector
'get_class' => ['assertInstanceOf', 'assertNotInstanceOf'],
];
/**
* @var IdentifierRenamer
*/
private $identifierRenamer;
public function __construct(IdentifierRenamer $identifierRenamer)
{
$this->identifierRenamer = $identifierRenamer;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Turns vague php-only method in PHPUnit TestCase to more specific', [
@ -85,39 +69,37 @@ final class AssertCompareToSpecificMethodRector extends AbstractPHPUnitRector
return null;
}
if (! isset($node->args[1])) {
return null;
}
$secondArgumentValue = $node->args[1]->value;
if (! $secondArgumentValue instanceof FuncCall) {
return null;
}
$resolvedFuncCallName = $this->getName($secondArgumentValue);
if ($resolvedFuncCallName === null) {
if (! $this->isNames($secondArgumentValue, array_keys($this->defaultOldToNewMethods))) {
return null;
}
$this->activeFuncCallName = $resolvedFuncCallName;
if (! isset($this->defaultOldToNewMethods[$this->activeFuncCallName])) {
return null;
}
$this->renameMethod($node);
$this->renameMethod($node, $this->getName($secondArgumentValue));
$this->moveFunctionArgumentsUp($node);
return $node;
}
private function renameMethod(MethodCall $methodCallNode): void
private function renameMethod(MethodCall $methodCallNode, string $funcName): void
{
/** @var Identifier $identifierNode */
$identifierNode = $methodCallNode->name;
$oldMethodName = $identifierNode->toString();
[$trueMethodName, $falseMethodName] = $this->defaultOldToNewMethods[$this->activeFuncCallName];
[$trueMethodName, $falseMethodName] = $this->defaultOldToNewMethods[$funcName];
if (in_array($oldMethodName, ['assertSame', 'assertEquals'], true) && $trueMethodName) {
$this->identifierRenamer->renameNode($methodCallNode, $trueMethodName);
$methodCallNode->name = new Identifier($trueMethodName);
} elseif (in_array($oldMethodName, ['assertNotSame', 'assertNotEquals'], true) && $falseMethodName) {
$this->identifierRenamer->renameNode($methodCallNode, $falseMethodName);
$methodCallNode->name = new Identifier($falseMethodName);
}
}

View File

@ -3,28 +3,21 @@
namespace Rector\PHPUnit\Rector\SpecificMethod;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use Rector\Builder\IdentifierRenamer;
use Rector\Node\NodeFactory;
use Rector\Rector\AbstractPHPUnitRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class AssertTrueFalseToSpecificMethodRector extends AbstractPHPUnitRector
{
/**
* @var string|null
*/
private $activeFuncCallName;
/**
* @var string[][]|false[][]
*/
private $defaultOldToNewMethods = [
private $oldToNewMethods = [
'is_readable' => ['assertIsReadable', 'assertNotIsReadable'],
'array_key_exists' => ['assertArrayHasKey', 'assertArrayNotHasKey'],
'array_search' => ['assertContains', 'assertNotContains'],
@ -39,34 +32,6 @@ final class AssertTrueFalseToSpecificMethodRector extends AbstractPHPUnitRector
'is_a' => ['assertInstanceOf', 'assertNotInstanceOf'],
];
/**
* @var string[][]|false[][]
*/
private $activeOldToNewMethods = [];
/**
* @var IdentifierRenamer
*/
private $identifierRenamer;
/**
* @var NodeFactory
*/
private $nodeFactory;
/**
* @param string[][] $activeMethods
*/
public function __construct(
array $activeMethods = [],
IdentifierRenamer $identifierRenamer,
NodeFactory $nodeFactory
) {
$this->activeOldToNewMethods = $this->filterActiveOldToNewMethods($activeMethods);
$this->identifierRenamer = $identifierRenamer;
$this->nodeFactory = $nodeFactory;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition(
@ -104,66 +69,30 @@ final class AssertTrueFalseToSpecificMethodRector extends AbstractPHPUnitRector
if (! isset($node->args[0])) {
return null;
}
$firstArgumentValue = $node->args[0]->value;
$funcCallName = $this->resolveFunctionName($firstArgumentValue);
if ($funcCallName === null) {
if (! $this->isNames($firstArgumentValue, array_keys($this->oldToNewMethods))) {
return null;
}
$this->activeFuncCallName = $funcCallName;
if (! isset($this->activeOldToNewMethods[$funcCallName])) {
return null;
}
$this->renameMethod($node);
$this->renameMethod($node, $this->getName($firstArgumentValue));
$this->moveFunctionArgumentsUp($node);
return $node;
}
/**
* @param string[][]|false[][] $activeMethods
* @return string[][]|false[][]
*/
private function filterActiveOldToNewMethods(array $activeMethods = []): array
{
if ($activeMethods) {
return array_filter($this->defaultOldToNewMethods, function (string $method) use ($activeMethods): bool {
return in_array($method, $activeMethods, true);
}, ARRAY_FILTER_USE_KEY);
}
return $this->defaultOldToNewMethods;
}
private function resolveFunctionName(Node $node): ?string
{
if ($node instanceof FuncCall
&& $node->name instanceof Name
) {
/** @var Name $nameNode */
$nameNode = $node->name;
return $nameNode->toString();
}
if ($node instanceof Empty_) {
return 'empty';
}
return null;
}
private function renameMethod(MethodCall $methodCallNode): void
private function renameMethod(MethodCall $methodCallNode, string $funcName): void
{
/** @var Identifier $identifierNode */
$identifierNode = $methodCallNode->name;
$oldMethodName = $identifierNode->toString();
[$trueMethodName, $falseMethodName] = $this->activeOldToNewMethods[$this->activeFuncCallName];
[$trueMethodName, $falseMethodName] = $this->oldToNewMethods[$funcName];
if ($oldMethodName === 'assertTrue' && $trueMethodName) {
$this->identifierRenamer->renameNode($methodCallNode, $trueMethodName);
$methodCallNode->name = new Identifier($trueMethodName);
} elseif ($oldMethodName === 'assertFalse' && $falseMethodName) {
$this->identifierRenamer->renameNode($methodCallNode, $falseMethodName);
$methodCallNode->name = new Identifier($falseMethodName);
}
}
@ -191,7 +120,7 @@ final class AssertTrueFalseToSpecificMethodRector extends AbstractPHPUnitRector
}
if ($funcCallOrEmptyNode instanceof Empty_) {
$methodCallNode->args[0] = $this->nodeFactory->createArg($funcCallOrEmptyNode->expr);
$methodCallNode->args[0] = new Arg($funcCallOrEmptyNode->expr);
}
}

View File

@ -4,6 +4,7 @@ namespace Rector\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
@ -29,7 +30,7 @@ final class NameResolver
return $this->resolve($classConstNode->consts[0]);
};
$this->nameResolvers[Property::class] = function (Property $propertyNode) {
$this->nameResolvers[Property::class] = function (Property $propertyNode): ?string {
if (! count($propertyNode->props)) {
return null;
}
@ -37,7 +38,7 @@ final class NameResolver
return $this->resolve($propertyNode->props[0]);
};
$this->nameResolvers[Use_::class] = function (Use_ $useNode) {
$this->nameResolvers[Use_::class] = function (Use_ $useNode): ?string {
if (! count($useNode->uses)) {
return null;
}
@ -45,7 +46,7 @@ final class NameResolver
return $this->resolve($useNode->uses[0]);
};
$this->nameResolvers[Name::class] = function (Name $nameNode) {
$this->nameResolvers[Name::class] = function (Name $nameNode): string {
$resolvedName = $nameNode->getAttribute(Attribute::RESOLVED_NAME);
if ($resolvedName instanceof FullyQualified) {
return $resolvedName->toString();
@ -53,6 +54,10 @@ final class NameResolver
return $nameNode->toString();
};
$this->nameResolvers[Empty_::class] = function (): string {
return 'empty';
};
}
public function isName(Node $node, string $name): bool