mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 16:30:51 +00:00
Merge pull request #18 from TomasVotruba/symfony-var-dumper-test-trait-new-args
[Symfony] var dumper test trait new args
This commit is contained in:
commit
d756d0ccaa
43
src/NodeAnalyzer/MethodCallAnalyzer.php
Normal file
43
src/NodeAnalyzer/MethodCallAnalyzer.php
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
|
||||
final class MethodCallAnalyzer
|
||||
{
|
||||
/**
|
||||
* @param string[] $methodNames
|
||||
*/
|
||||
public function isStaticMethodCallTypeAndMethods(Node $node, string $type, array $methodNames): bool
|
||||
{
|
||||
if (! $this->isStaticMethodCallType($node, $type)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var StaticCall $node */
|
||||
$currentMethodName = (string) $node->name;
|
||||
|
||||
foreach ($methodNames as $methodName) {
|
||||
if ($currentMethodName === $methodName) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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,87 @@
|
|||
<?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
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private 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->isStaticMethodCallTypeAndMethods($node,self::TRAIT_NAME, ['assertDumpEquals', 'assertDumpMatchesFormat'])) {
|
||||
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,5 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
VarDumperTestTrait::assertDumpEquals($dump, $data, null, 'Some message');
|
||||
|
||||
VarDumperTestTrait::assertDumpMatchesFormat($dump, $format, 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,5 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
VarDumperTestTrait::assertDumpEquals($dump, $data, 'Some message');
|
||||
|
||||
VarDumperTestTrait::assertDumpMatchesFormat($dump, $format, 'Some message');
|
Loading…
Reference in New Issue
Block a user