[PHPUnit] SpecificMethodBoolNullRector added

This commit is contained in:
TomasVotruba 2017-12-18 01:16:36 +01:00
parent d3b8f891c7
commit 1817337631
5 changed files with 122 additions and 0 deletions

View File

@ -0,0 +1,78 @@
<?php declare(strict_types=1);
namespace Rector\Rector\Contrib\PHPUnit;
use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\Rector\AbstractRector;
/**
* Before:
* - $this->assertSame(null, $anything);
*
* After:
* - $this->assertNull($anything);
*/
final class SpecificMethodBoolNullRector extends AbstractRector
{
/**
* @var MethodCallAnalyzer
*/
private $methodCallAnalyzer;
/**
* @var string|null
*/
private $activeFuncCallName;
public function __construct(MethodCallAnalyzer $methodCallAnalyzer)
{
$this->methodCallAnalyzer = $methodCallAnalyzer;
}
public function isCandidate(Node $node): bool
{
$this->activeFuncCallName = null;
if (! $this->methodCallAnalyzer->isTypesAndMethods(
$node,
['PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase'],
['assertSame']
)) {
return false;
}
/** @var MethodCall $methodCallNode */
$methodCallNode = $node;
$firstArgumentValue = $methodCallNode->args[0]->value;
if (! $firstArgumentValue instanceof ConstFetch) {
return false;
}
$costName = $firstArgumentValue->name->toString();
if ($costName === 'null') {
return true;
}
return false;
}
/**
* @param MethodCall $methodCallNode
*/
public function refactor(Node $methodCallNode): ?Node
{
$methodCallNode->name = new Identifier('assertNull');
$methodArguments = $methodCallNode->args;
array_shift($methodArguments);
$methodCallNode->args = $methodCallNode;
return $methodCallNode;
}
}

View File

@ -2,6 +2,7 @@ rectors:
Rector\Rector\Contrib\PHPUnit\ExceptionAnnotationRector: ~
Rector\Rector\Contrib\PHPUnit\GetMockRector: ~
Rector\Rector\Contrib\PHPUnit\SpecificMethodRector: ~
Rector\Rector\Contrib\PHPUnit\SpecificMethodBoolNullRector: ~
# ref. https://github.com/sebastianbergmann/phpunit/compare/5.7.9...6.0.0
Rector\Rector\Dynamic\PseudoNamespaceToNamespaceRector:

View File

@ -0,0 +1,9 @@
<?php declare(strict_types=1);
final class MyTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertNull('second argument');
}
}

View File

@ -0,0 +1,25 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Contrib\PHPUnit\SpecificMethodBoolNullRector;
use Rector\Rector\Contrib\PHPUnit\SpecificMethodBoolNullRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class Test extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFileMatchesExpectedContent(
__DIR__ . '/Wrong/wrong.php.inc',
__DIR__ . '/Correct/correct.php.inc'
);
}
/**
* @return string[]
*/
protected function getRectorClasses(): array
{
return [SpecificMethodBoolNullRector::class];
}
}

View File

@ -0,0 +1,9 @@
<?php declare(strict_types=1);
final class MyTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertSame(null, 'second argument');
}
}