rector/vendor/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/AssertSameBoolNullToSpecificMethodRector.php
Tomas Votruba a2178d82dc Updated Rector to commit 9e9a314b23fa9f217c236412591c4cb9ad94edf5
9e9a314b23 [NodeTypeResolver] Remove unused AttributeKey::INSIDE_ARRAY_ITEM on ContextNodeVisitor (#4496)
2023-07-12 22:16:31 +00:00

92 lines
3.7 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use Rector\Core\Rector\AbstractRector;
use Rector\PHPUnit\NodeAnalyzer\ArgumentMover;
use Rector\PHPUnit\NodeAnalyzer\IdentifierManipulator;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\PHPUnit\ValueObject\ConstantWithAssertMethods;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertSameBoolNullToSpecificMethodRector\AssertSameBoolNullToSpecificMethodRectorTest
*/
final class AssertSameBoolNullToSpecificMethodRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\PHPUnit\NodeAnalyzer\IdentifierManipulator
*/
private $identifierManipulator;
/**
* @readonly
* @var \Rector\PHPUnit\NodeAnalyzer\ArgumentMover
*/
private $argumentMover;
/**
* @readonly
* @var \Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer
*/
private $testsNodeAnalyzer;
/**
* @var ConstantWithAssertMethods[]
*/
private $constantWithAssertMethods = [];
public function __construct(IdentifierManipulator $identifierManipulator, ArgumentMover $argumentMover, TestsNodeAnalyzer $testsNodeAnalyzer)
{
$this->identifierManipulator = $identifierManipulator;
$this->argumentMover = $argumentMover;
$this->testsNodeAnalyzer = $testsNodeAnalyzer;
$this->constantWithAssertMethods = [new ConstantWithAssertMethods('null', 'assertNull', 'assertNotNull'), new ConstantWithAssertMethods('true', 'assertTrue', 'assertNotTrue'), new ConstantWithAssertMethods('false', 'assertFalse', 'assertNotFalse')];
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Turns same bool and null comparisons to their method name alternatives in PHPUnit TestCase', [new CodeSample('$this->assertSame(null, $anything);', '$this->assertNull($anything);'), new CodeSample('$this->assertNotSame(false, $anything);', '$this->assertNotFalse($anything);')]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [MethodCall::class, StaticCall::class];
}
/**
* @param MethodCall|StaticCall $node
*/
public function refactor(Node $node) : ?Node
{
if (!$this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, ['assertSame', 'assertNotSame'])) {
return null;
}
if ($node->isFirstClassCallable()) {
return null;
}
$firstArgumentValue = $node->getArgs()[0]->value;
if (!$firstArgumentValue instanceof ConstFetch) {
return null;
}
foreach ($this->constantWithAssertMethods as $constantWithAssertMethod) {
if (!$this->isName($firstArgumentValue, $constantWithAssertMethod->getConstant())) {
continue;
}
$this->renameMethod($node, $constantWithAssertMethod);
$this->argumentMover->removeFirstArg($node);
return $node;
}
return null;
}
/**
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $node
*/
private function renameMethod($node, ConstantWithAssertMethods $constantWithAssertMethods) : void
{
$this->identifierManipulator->renameNodeWithMap($node, ['assertSame' => $constantWithAssertMethods->getAssetMethodName(), 'assertNotSame' => $constantWithAssertMethods->getNotAssertMethodName()]);
}
}