mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-05 10:50:48 +00:00
[Symfony] add VarDumperTestTraitMethodArgsRector
This commit is contained in:
parent
d30ba52112
commit
b0d3984ae0
37
src/NodeAnalyzer/MethodCallAnalyzer.php
Normal file
37
src/NodeAnalyzer/MethodCallAnalyzer.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
|
||||
final class MethodCallAnalyzer
|
||||
{
|
||||
public function isStaticMethodCallTypeAndMethod(Node $node, string $type, string $method): bool
|
||||
{
|
||||
if (! $this->isStaticMethodCallType($node, $type)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var StaticCall $node */
|
||||
$methodName = (string) $node->name;
|
||||
if ($methodName !== $method) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function isStaticMethodCallType(Node $node, string $type): bool
|
||||
{
|
||||
if (! $node instanceof StaticCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($node->class->toString() !== $type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Contrib\Symfony;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ConstFetch;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\Deprecation\SetNames;
|
||||
use Rector\NodeAnalyzer\MethodCallAnalyzer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
/**
|
||||
* Converts all:
|
||||
* VarDumperTestTrait::assertDumpEquals($dump, $data, $mesage = '');
|
||||
* VarDumperTestTrait::assertDumpMatchesFormat($dump, $format, $mesage = '');
|
||||
*
|
||||
* into:
|
||||
* VarDumperTestTrait::assertDumpEquals($dump, $data, $context = null, $mesage = '');
|
||||
* VarDumperTestTrait::assertDumpMatchesFormat($dump, $format, $context = null, $mesage = '');
|
||||
*/
|
||||
final class VarDumperTestTraitMethodArgsRector extends AbstractRector
|
||||
{
|
||||
const TRAIT_NAME = 'VarDumperTestTrait';
|
||||
/**
|
||||
* @var MethodCallAnalyzer
|
||||
*/
|
||||
private $methodCallAnalyzer;
|
||||
|
||||
public function __construct(MethodCallAnalyzer $methodCallAnalyzer)
|
||||
{
|
||||
$this->methodCallAnalyzer = $methodCallAnalyzer;
|
||||
}
|
||||
|
||||
public function getSetName(): string
|
||||
{
|
||||
return SetNames::SYMFONY;
|
||||
}
|
||||
|
||||
public function sinceVersion(): float
|
||||
{
|
||||
return 4.0;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $this->methodCallAnalyzer->isStaticMethodCallTypeAndMethod($node,self::TRAIT_NAME,'assertDumpEquals')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var StaticCall $node */
|
||||
if (count($node->args) <= 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param StaticCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$methodArguments = $node->args;
|
||||
|
||||
if ($methodArguments[2]->value instanceof String_) {
|
||||
$methodArguments[3] = $methodArguments[2];
|
||||
$methodArguments[2] = $this->createNullConstant();
|
||||
|
||||
$node->args = $methodArguments;
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function createNullConstant(): ConstFetch
|
||||
{
|
||||
return new ConstFetch(new Name('null'));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
VarDumperTestTrait::assertDumpEquals($dump, $data, null, 'Some message');
|
|
@ -0,0 +1,25 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Contrib\Symfony\VarDumperTestTraitMethodArgsRector;
|
||||
|
||||
use Rector\Rector\Contrib\Symfony\VarDumperTestTraitMethodArgsRector;
|
||||
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 [VarDumperTestTraitMethodArgsRector::class];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
VarDumperTestTrait::assertDumpEquals($dump, $data, 'Some message');
|
Loading…
Reference in New Issue
Block a user