Skip to content

Support converting EventSubscriberInterface to AsEventListener attributes - #1094

Merged
TomasVotruba merged 2 commits into
mainfrom
tv-as-event-listener-attribute-composer-based
Oct 27, 2026
Merged

TomasVotruba merged 2 commits into
mainfrom
tv-as-event-listener-attribute-composer-based

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Supersedes #1081 by @rebuy-de (rebased on main) and registers the new rule in the composer-based set.

  • rule EventSubscriberToAttributeRector converts EventSubscriberInterface implementations to #[AsEventListener] attributes
  • declares ComposerPackageConstraint('symfony/event-dispatcher', '>=5.4') - the attribute is allowed on methods since Symfony 5.4
  • registered in config/sets/symfony/composer-based.php under the event-dispatcher 5.4 bond

Original work by @rebuy-de (Martin Parsiegla), closes #1081.

ECS, PHPStan and the rule tests pass locally.

@TomasVotruba
TomasVotruba force-pushed the tv-as-event-listener-attribute-composer-based branch from be010ee to 5164842 Compare October 27, 2026 20:22
Spea and others added 2 commits October 27, 2026 21:32
…tributes

With this new rector, one can convert an implementation of an `EventSubscriberInterface` to attributes by using `AsEventListener`.

This rector will convert something like this:

```php
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class MyEventSubscriber implements EventSubscriberInterface
{
    public function methodToBeCalled()
    {
    }

    public static function getSubscribedEvents(): array
    {
        return ['some_event' => ['methodToBeCalled', 10]];
    }
}
```

to this

```php
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

class MyEventSubscriber
{
    #[AsEventListener(event: 'some_event', priority: 10)]
    public function methodToBeCalled()
    {
    }
}
```
@TomasVotruba
TomasVotruba force-pushed the tv-as-event-listener-attribute-composer-based branch from 5164842 to e0c03ee Compare October 27, 2026 20:32
@TomasVotruba
TomasVotruba merged commit 0e166e5 into main Oct 27, 2026
7 of 8 checks passed
@TomasVotruba
TomasVotruba deleted the tv-as-event-listener-attribute-composer-based branch October 27, 2026 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants