From afb27b1900997d2d5059c2dc6a98919628c3d5af Mon Sep 17 00:00:00 2001 From: Gabriel Caruso Date: Tue, 19 Dec 2017 20:18:10 -0200 Subject: [PATCH] Make easier to add new assertions to SpecificMethodCountRector --- .../PHPUnit/SpecificMethodCountRector.php | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Rector/Contrib/PHPUnit/SpecificMethodCountRector.php b/src/Rector/Contrib/PHPUnit/SpecificMethodCountRector.php index 1bdd92d6782..7d4b14f3029 100644 --- a/src/Rector/Contrib/PHPUnit/SpecificMethodCountRector.php +++ b/src/Rector/Contrib/PHPUnit/SpecificMethodCountRector.php @@ -33,16 +33,21 @@ use Rector\Rector\AbstractRector; */ final class SpecificMethodCountRector extends AbstractRector { + /** + * @var string[] + */ + private $renameMethodsMap = [ + 'assertSame' => 'assertCount', + 'assertNotSame' => 'assertNotCount', + 'assertEquals' => 'assertCount', + 'assertNotEquals' => 'assertNotCount', + ]; + /** * @var MethodCallAnalyzer */ private $methodCallAnalyzer; - /** - * @var string|null - */ - private $activeFuncCallName; - public function __construct(MethodCallAnalyzer $methodCallAnalyzer) { $this->methodCallAnalyzer = $methodCallAnalyzer; @@ -50,8 +55,6 @@ final class SpecificMethodCountRector extends AbstractRector public function isCandidate(Node $node): bool { - $this->activeFuncCallName = null; - if (! $this->methodCallAnalyzer->isTypesAndMethods( $node, ['PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase'], @@ -76,7 +79,9 @@ final class SpecificMethodCountRector extends AbstractRector $coutableMethod = $secondArgumentValue->name->toString(); - return $coutableMethod === 'count' || $coutableMethod === 'sizeof'; + return in_array($coutableMethod, [ + 'count', 'sizeof', + ]); } /** @@ -86,13 +91,7 @@ final class SpecificMethodCountRector extends AbstractRector { $oldMethodName = $methodCallNode->name->toString(); - if ($oldMethodName === 'assertEquals' || $oldMethodName === 'assertSame') { - /** @var string $trueMethodName */ - $methodCallNode->name = new Identifier('assertCount'); - } elseif ($oldMethodName === 'assertNotEquals' || $oldMethodName === 'assertNotSame') { - /** @var string $falseMethodName */ - $methodCallNode->name = new Identifier('assertNotCount'); - } + $methodCallNode->name = new Identifier($this->renameMethodsMap[$oldMethodName]); /** @var FuncCall $secondArgument */ $secondArgument = $methodCallNode->args[1]->value;