Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/run_all_sets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

Expand Down