-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(rector) add rules to handle EventDispatcher versions
- Loading branch information
Showing
5 changed files
with
88 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
config/rector/src/SwapEventDispatcherEventNameParameters.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace lucatume\Rector; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use Rector\Core\Rector\AbstractRector; | ||
use PHPStan\Type\ObjectType; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
class SwapEventDispatcherEventNameParameters extends AbstractRector | ||
{ | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Swap the event and name parameters of the EventDispatcherInterface::dispatch method.', | ||
[ | ||
new CodeSample( | ||
'$eventDispatcher->dispatch($event, $eventName);', | ||
'$eventDispatcher->dispatch($eventName, $event);' | ||
) | ||
] | ||
); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$methodCallName = $this->getName($node->name); | ||
if ($methodCallName !== 'dispatch') { | ||
return null; | ||
} | ||
|
||
if (!$this->isObjectType( | ||
$node->var, | ||
new ObjectType('Symfony\Component\EventDispatcher\EventDispatcherInterface') | ||
)) { | ||
return null; | ||
} | ||
|
||
$args = $node->args; | ||
|
||
if (!($args[0] instanceof Node\Arg && $args[1] instanceof Node\Arg | ||
&& $args[0]->value?->name === 'event' | ||
&& $args[1]->value?->name === 'name')) { | ||
return null; | ||
} | ||
|
||
$node->args = [$args[1], $args[0]]; | ||
|
||
return $node; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters