From 6161674af1ec193fe4e62ac45385e2022c9b1537 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 23 Sep 2026 16:06:27 +0200 Subject: [PATCH] [symfony] allow 2nd dynamic arg in SingleArgEventDispatchRule --- src/Rules/Symfony/SingleArgEventDispatchRule.php | 12 +++++++++++- .../Fixture/SkipDynamicSecondArg.php | 13 +++++++++++++ .../SingleArgEventDispatchRuleTest.php | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/Rules/Symfony/SingleArgEventDispatchRule/Fixture/SkipDynamicSecondArg.php diff --git a/src/Rules/Symfony/SingleArgEventDispatchRule.php b/src/Rules/Symfony/SingleArgEventDispatchRule.php index e9ef4dfd2..d79811b6b 100644 --- a/src/Rules/Symfony/SingleArgEventDispatchRule.php +++ b/src/Rules/Symfony/SingleArgEventDispatchRule.php @@ -39,11 +39,21 @@ public function processNode(Node $node, Scope $scope): array return []; } + $args = $node->getArgs(); + // all good - if (count($node->getArgs()) === 1) { + if (count($args) === 1) { return []; } + // allow 2nd arg when dynamic, e.g. a variable event name + if (count($args) === 2) { + $secondArgType = $scope->getType($args[1]->value); + if ($secondArgType->getConstantStrings() === []) { + return []; + } + } + $callerType = $scope->getType($node->var); if (! $callerType instanceof ObjectType) { return []; diff --git a/tests/Rules/Symfony/SingleArgEventDispatchRule/Fixture/SkipDynamicSecondArg.php b/tests/Rules/Symfony/SingleArgEventDispatchRule/Fixture/SkipDynamicSecondArg.php new file mode 100644 index 000000000..14c789a61 --- /dev/null +++ b/tests/Rules/Symfony/SingleArgEventDispatchRule/Fixture/SkipDynamicSecondArg.php @@ -0,0 +1,13 @@ +dispatch($event, $name); + } +} diff --git a/tests/Rules/Symfony/SingleArgEventDispatchRule/SingleArgEventDispatchRuleTest.php b/tests/Rules/Symfony/SingleArgEventDispatchRule/SingleArgEventDispatchRuleTest.php index 40d173be8..69b2a1f90 100644 --- a/tests/Rules/Symfony/SingleArgEventDispatchRule/SingleArgEventDispatchRuleTest.php +++ b/tests/Rules/Symfony/SingleArgEventDispatchRule/SingleArgEventDispatchRuleTest.php @@ -30,6 +30,7 @@ public static function provideData(): Iterator yield [__DIR__ . '/Fixture/SkipSingleDispatch.php', []]; yield [__DIR__ . '/Fixture/SkipUnrelatedDispatch.php', []]; + yield [__DIR__ . '/Fixture/SkipDynamicSecondArg.php', []]; } protected function getRule(): Rule