Skip to content
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ $ vendor/bin/phpstan
- constructors, calls, factory methods
- [`phpstan/phpstan-symfony`](https://github.com/phpstan/phpstan-symfony) with `containerXmlPath` must be used
- `#[AsEventListener]` attribute
- `#[AsMessageHandler]` attribute
- `#[AsController]` attribute
- `#[AsCommand]` attribute
- `#[Required]` attribute
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"symfony/doctrine-bridge": "^5.4 || ^6.0 || ^7.0",
"symfony/event-dispatcher": "^5.4 || ^6.0 || ^7.0",
"symfony/http-kernel": "^5.4 || ^6.0 || ^7.0",
"symfony/messenger": "^5.4 || ^6.0 || ^7.0",
"symfony/routing": "^5.4 || ^6.0 || ^7.0",
"symfony/validator": "^5.4 || ^6.0 || ^7.0",
"twig/twig": "^3.0"
Expand Down
213 changes: 212 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/Provider/SymfonyUsageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ protected function shouldMarkAsUsed(ReflectionMethod $method): ?string
return 'Event listener method via #[AsEventListener] attribute';
}

if ($this->isMethodWithMessageHandlerAttribute($method)) {
return 'Message Handler method via #[AsMessageHandler] attribute';
}

if ($this->isAutowiredWithRequiredAttribute($method)) {
return 'Autowired with #[Required] (called by DIC)';
}
Expand Down Expand Up @@ -518,6 +522,14 @@ protected function isMethodWithRouteAttribute(ReflectionMethod $method): bool
|| $this->hasAttribute($method, 'Symfony\Component\Routing\Annotation\Route', $isInstanceOf);
}

protected function isMethodWithMessageHandlerAttribute(ReflectionMethod $method): bool
{
$class = $method->getDeclaringClass();

return $this->hasAttribute($class, 'Symfony\Component\Messenger\Attribute\AsMessageHandler')
Copy link
Member

@janedbal janedbal Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think this is correct. When used on class, I believe it only calls a single method (by default __invoke and optionally the one stated in the attribute)

Currently, you mark all methods as used in a class marked with this attribute.

We need test for such behaviour.

|| $this->hasAttribute($method, 'Symfony\Component\Messenger\Attribute\AsMessageHandler');
}

protected function isMethodWithCallbackConstraintAttribute(ReflectionMethod $method): bool
{
$attributes = $method->getDeclaringClass()->getAttributes('Symfony\Component\Validator\Constraints\Callback');
Expand Down
13 changes: 13 additions & 0 deletions tests/Rule/data/providers/symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\Component\Validator\Constraints as Assert;
Expand Down Expand Up @@ -47,6 +48,18 @@ public function __construct() {

}

#[AsMessageHandler]
class SomeMessageHandler {
}

class SomeMessageHandlerClass
{
#[AsMessageHandler]
public function onSomething(): void
{
}
}

class FooBundle extends Bundle {
public function __construct() {
}
Expand Down