mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-22 18:54:39 +01:00
[PHPUnit] SpecificMethodBoolNullRector added
This commit is contained in:
parent
d3b8f891c7
commit
1817337631
78
src/Rector/Contrib/PHPUnit/SpecificMethodBoolNullRector.php
Normal file
78
src/Rector/Contrib/PHPUnit/SpecificMethodBoolNullRector.php
Normal 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;
|
||||
}
|
||||
}
|
@ -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:
|
||||
|
@ -0,0 +1,9 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
final class MyTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$this->assertNull('second argument');
|
||||
}
|
||||
}
|
@ -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];
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
final class MyTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$this->assertSame(null, 'second argument');
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user