Merge pull request #2045 from rectorphp/symfony43-dispatch-get-class

[Symfony] Make MakeDispatchFirstArgumentEventRector work with get_class
This commit is contained in:
Tomáš Votruba 2019-09-26 13:58:25 +02:00 committed by GitHub
commit a268b441ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 8 deletions

View File

@ -14,7 +14,7 @@ $setProvider = new SetProvider();
$file = 'src/Rector/AbstractRector.php';
$excludedSets = [
// required Kernel class to be set in parameters
'symfony-code-quality'
'symfony-code-quality',
];
$errors = [];

View File

@ -4,6 +4,7 @@ namespace Rector\Symfony\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
@ -83,18 +84,32 @@ PHP
return null;
}
if (! $this->isStringOrUnionStringOnlyType($node->args[0]->value)) {
return null;
$firstArgumentValue = $node->args[0]->value;
$secondArgumentValue = $node->args[1]->value;
if ($this->isStringOrUnionStringOnlyType($firstArgumentValue)) {
// swap arguments
[$node->args[0], $node->args[1]] = [$node->args[1], $node->args[0]];
if ($this->isEventNameSameAsEventObjectClass($node)) {
unset($node->args[1]);
}
return $node;
}
// swap arguments
[$node->args[0], $node->args[1]] = [$node->args[1], $node->args[0]];
if ($secondArgumentValue instanceof FuncCall) {
if ($this->isName($secondArgumentValue, 'get_class')) {
$getClassArgument = $secondArgumentValue->args[0]->value;
if ($this->isEventNameSameAsEventObjectClass($node)) {
unset($node->args[1]);
if ($this->areNodesEqual($firstArgumentValue, $getClassArgument)) {
unset($node->args[1]);
return $node;
}
}
}
return $node;
return null;
}
/**

View File

@ -0,0 +1,35 @@
<?php
namespace Rector\Symfony\Tests\Rector\MethodCall\MakeDispatchFirstArgumentEventRector\Fixture;
use Rector\Symfony\Tests\Rector\MethodCall\MakeDispatchFirstArgumentEventRector\Source\CustomEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
class EventGetClass
{
public function run(EventDispatcher $eventDispatcher)
{
$customEvent = new CustomEvent();
$eventDispatcher->dispatch($customEvent, get_class($customEvent));
}
}
?>
-----
<?php
namespace Rector\Symfony\Tests\Rector\MethodCall\MakeDispatchFirstArgumentEventRector\Fixture;
use Rector\Symfony\Tests\Rector\MethodCall\MakeDispatchFirstArgumentEventRector\Source\CustomEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
class EventGetClass
{
public function run(EventDispatcher $eventDispatcher)
{
$customEvent = new CustomEvent();
$eventDispatcher->dispatch($customEvent);
}
}
?>

View File

@ -22,6 +22,7 @@ final class MakeDispatchFirstArgumentEventRectorTest extends AbstractRectorTestC
{
yield [__DIR__ . '/Fixture/fixture.php.inc'];
yield [__DIR__ . '/Fixture/event_class_constant.php.inc'];
yield [__DIR__ . '/Fixture/get_class.php.inc'];
yield [__DIR__ . '/Fixture/keep_string_event_constant.php.inc'];
}