Skip to content

Commit aa22b8b

Browse files
gaetan-petitGaëtan Petit
authored andcommitted
Exclude method with AsMessageHandler attribute
1 parent d7cf348 commit aa22b8b

File tree

4 files changed

+238
-1
lines changed

4 files changed

+238
-1
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"symfony/doctrine-bridge": "^5.4 || ^6.0 || ^7.0",
3939
"symfony/event-dispatcher": "^5.4 || ^6.0 || ^7.0",
4040
"symfony/http-kernel": "^5.4 || ^6.0 || ^7.0",
41+
"symfony/messenger": "^7.3",
4142
"symfony/routing": "^5.4 || ^6.0 || ^7.0",
4243
"symfony/validator": "^5.4 || ^6.0 || ^7.0",
4344
"twig/twig": "^3.0"

composer.lock

Lines changed: 212 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Provider/SymfonyUsageProvider.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ protected function shouldMarkAsUsed(ReflectionMethod $method): ?string
363363
return 'Event listener method via #[AsEventListener] attribute';
364364
}
365365

366+
if ($this->isMethodWithMessageHandlerAttribute($method)) {
367+
return 'Message Handler method via #[AsMessageHandler] attribute';
368+
}
369+
366370
if ($this->isAutowiredWithRequiredAttribute($method)) {
367371
return 'Autowired with #[Required] (called by DIC)';
368372
}
@@ -518,6 +522,14 @@ protected function isMethodWithRouteAttribute(ReflectionMethod $method): bool
518522
|| $this->hasAttribute($method, 'Symfony\Component\Routing\Annotation\Route', $isInstanceOf);
519523
}
520524

525+
protected function isMethodWithMessageHandlerAttribute(ReflectionMethod $method): bool
526+
{
527+
$class = $method->getDeclaringClass();
528+
529+
return $this->hasAttribute($class, 'Symfony\Component\Messenger\Attribute\AsMessageHandler')
530+
|| $this->hasAttribute($method, 'Symfony\Component\Messenger\Attribute\AsMessageHandler');
531+
}
532+
521533
protected function isMethodWithCallbackConstraintAttribute(ReflectionMethod $method): bool
522534
{
523535
$attributes = $method->getDeclaringClass()->getAttributes('Symfony\Component\Validator\Constraints\Callback');

tests/Rule/data/providers/symfony.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpKernel\Bundle\Bundle;
1717
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
1818
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
19+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
1920
use Symfony\Component\Routing\Attribute\Route;
2021
use Symfony\Contracts\Service\Attribute\Required;
2122
use Symfony\Component\Validator\Constraints as Assert;
@@ -47,6 +48,18 @@ public function __construct() {
4748

4849
}
4950

51+
#[AsMessageHandler]
52+
class SomeMessageHandler {
53+
}
54+
55+
class SomeMessageHandlerClass
56+
{
57+
#[AsMessageHandler]
58+
public function onSomething(): void
59+
{
60+
}
61+
}
62+
5063
class FooBundle extends Bundle {
5164
public function __construct() {
5265
}

0 commit comments

Comments
 (0)