mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-15 21:38:24 +01:00
Merge pull request #2045 from rectorphp/symfony43-dispatch-get-class
[Symfony] Make MakeDispatchFirstArgumentEventRector work with get_class
This commit is contained in:
commit
a268b441ae
@ -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 = [];
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -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'];
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user