phpstan-symfony icon indicating copy to clipboard operation
phpstan-symfony copied to clipboard

Support Symfony #[AutowireLocator] attribute

Open RafaelKr opened this issue 11 months ago • 5 comments

I implemented initial support for the #[AutowireLocator] attribute. See https://symfony.com/blog/new-in-symfony-6-4-autowirelocator-and-autowireiterator-attributes

The simple syntax is supported now:

#[AutowireLocator([FooHandler::class, BarHandler::class])]

It also supports when classes are specified via string:

#[AutowireLocator(['App\FooHandler'])]

The advanced syntax described in the blog post is ~~not~~ also supported ~~yet~~ now:

#[AutowireLocator([
    'foo' => FooHandler::class,
    'bar' => new SubscribedService(type: 'string', attributes: new Autowire('%some.parameter%')),
    'optionalBaz' => '?'.BazHandler::class,
])]
private ContainerInterface $handlers,

~~Maybe someone else wants to figure this out. Unfortunately I have no more time left to do it in the coming days.~~

Edit 2: I just found out how I can both simplify this a lot and implement the advanced syntax by just creating an instance of the AutowireLocator attribute. And I'll move this to AutowireLoaderServiceMapFactory so it will be also available for PHPStan\Rules\Symfony\ContainerInterfaceUnknownServiceRule.

closes #411

RafaelKr avatar Dec 30 '24 17:12 RafaelKr

~~I tried to fix all errors but I'm not sure how to fix those 2 errors left.~~

Edit: Got it.

RafaelKr avatar Dec 30 '24 18:12 RafaelKr

I'll add some tests later, besides from that it's ready for review.

Btw: I tried to rename the branch of my Repo to feat/autowirelocator, but it wasn't possible without losing the connection to this PR, the PR showed the branch was deleted in that case.

RafaelKr avatar Dec 31 '24 16:12 RafaelKr

I also added tests now, but I have no idea how to resolve the failing lint and test checks. If someone can resolve those we're ready to merge from my side :rocket:

RafaelKr avatar Jan 03 '25 20:01 RafaelKr

I also noticed it only works when we're using Constructor property promotion, so when specifying private ContainerInterface $locator in the constructor.

It won't work when $locator is specified as a class member and then assigned in the constructor method (as here e.g. https://github.com/phpstan/phpstan-symfony/blob/c7b7e7f520893621558bfbfdb2694d4364565c1d/tests/Rules/Symfony/ExampleServiceSubscriber.php).

Is this a problem in practice?

RafaelKr avatar Jan 03 '25 20:01 RafaelKr

Hi, I can't review this when the build isn't green.

ondrejmirtes avatar Jan 19 '25 13:01 ondrejmirtes