From 46412c73ce587b892ac850566a64a356bdc720ba Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 26 Sep 2019 13:52:17 +0200 Subject: [PATCH] [Symfony] Make MakeDispatchFirstArgumentEventRector work with get_class --- bin/run_all_sets.php | 2 +- .../MakeDispatchFirstArgumentEventRector.php | 29 +++++++++++---- .../Fixture/get_class.php.inc | 35 +++++++++++++++++++ ...keDispatchFirstArgumentEventRectorTest.php | 1 + 4 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 packages/Symfony/tests/Rector/MethodCall/MakeDispatchFirstArgumentEventRector/Fixture/get_class.php.inc diff --git a/bin/run_all_sets.php b/bin/run_all_sets.php index 3d8547b7ffe5..253bfc727a12 100644 --- a/bin/run_all_sets.php +++ b/bin/run_all_sets.php @@ -14,7 +14,7 @@ $file = 'src/Rector/AbstractRector.php'; $excludedSets = [ // required Kernel class to be set in parameters - 'symfony-code-quality' + 'symfony-code-quality', ]; $errors = []; diff --git a/packages/Symfony/src/Rector/MethodCall/MakeDispatchFirstArgumentEventRector.php b/packages/Symfony/src/Rector/MethodCall/MakeDispatchFirstArgumentEventRector.php index f0a61dec644a..c9295a20d615 100644 --- a/packages/Symfony/src/Rector/MethodCall/MakeDispatchFirstArgumentEventRector.php +++ b/packages/Symfony/src/Rector/MethodCall/MakeDispatchFirstArgumentEventRector.php @@ -4,6 +4,7 @@ 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 @@ public function refactor(Node $node): ?Node 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; } /** diff --git a/packages/Symfony/tests/Rector/MethodCall/MakeDispatchFirstArgumentEventRector/Fixture/get_class.php.inc b/packages/Symfony/tests/Rector/MethodCall/MakeDispatchFirstArgumentEventRector/Fixture/get_class.php.inc new file mode 100644 index 000000000000..1c8d70402035 --- /dev/null +++ b/packages/Symfony/tests/Rector/MethodCall/MakeDispatchFirstArgumentEventRector/Fixture/get_class.php.inc @@ -0,0 +1,35 @@ +dispatch($customEvent, get_class($customEvent)); + } +} + +?> +----- +dispatch($customEvent); + } +} + +?> diff --git a/packages/Symfony/tests/Rector/MethodCall/MakeDispatchFirstArgumentEventRector/MakeDispatchFirstArgumentEventRectorTest.php b/packages/Symfony/tests/Rector/MethodCall/MakeDispatchFirstArgumentEventRector/MakeDispatchFirstArgumentEventRectorTest.php index 70aa693a6aae..bcde31ce155d 100644 --- a/packages/Symfony/tests/Rector/MethodCall/MakeDispatchFirstArgumentEventRector/MakeDispatchFirstArgumentEventRectorTest.php +++ b/packages/Symfony/tests/Rector/MethodCall/MakeDispatchFirstArgumentEventRector/MakeDispatchFirstArgumentEventRectorTest.php @@ -22,6 +22,7 @@ public function provideDataForTest(): iterable { 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']; }