Skip to content

[DI][DX][RFC] Implement a ServiceSubscriberTrait #23898

Closed
@tgalopin

Description

@tgalopin
Q A
Bug report? no
Feature request? no
BC Break report? no
RFC? yes
Symfony version 3.4

While the Symfony DI component is a really awesome tool to help build high quality apps, I was thinking there could be ways to improve its DX in terms of autocompletion in IDEs.

When interacting with service locators, we are used to do things like $this->get('my_service'), or more recently $this->get(MyService::class). Without a Symfony plugin, IDEs are not able to autocomplete what comes after $this->get('my_service') as the type of the returned element is not known until runtime.

After some dicussions with @nicolas-grekas, we came up with the idea of a ServiceSubscriberTrait that could be used to improve DX on this level. Here is an example of usage (the class would be userland, the trait would be in Symfony):

https://gist.github.com/tgalopin/46e2be955762d6d68360dd455afeab70

The idea is to detect in getSubscribedServices all the current class methods and inject automatically the right services in the service locator using the typehints. This would mean developers could autocomplete usages of services by implementing tiny helper methods, relying on the trait to detect what they need based on these methods.

I would like to get opinions about this before trying to work on an implementation. WDYT?

Metadata

Metadata

Labels

DependencyInjectionRFCRFC = Request For Comments (proposals about features that you want to be discussed)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions