Skip to content

[RFC] Check types validity in the service graph at compile time - for currently loaded classes only #27744

@nicolas-grekas

Description

@nicolas-grekas

While wiring services, nothing prevents one from doing mistakes and break type hints, e.g. injecting a Foo object into an incompatible BarInterface-hinted argument.

When this happens, the error is discovered at runtime. A common practice to detect this before prod is to create a smoke test that instantiates all services and does nothing else. This should work even better with the new test container in 4.1. Maybe something we could add to the MakerBundle? Of course, this strategy doesn't work for lazy services / locators / iterators. Not sure about false-positives also.

We're not verifying these references at compile-time because we do not want to load all PHP classes, as doing so takes time. Also because doing the verification itself might slow down the compilation too much and hurt DX.

Yet, I think we could still have a compiler pass that does these checks. In order to make it affordable, it would have a mode where it would check references only for currently loaded classes/interfaces, using class|interface_exists() with false as 2nd argument, and maybe opcache_is_script_cached() also.

If the overhead of checking only the types (but not loading the code) is low enough, we could enable this pass by default in the dev env.

Worth a try?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DXDX = Developer eXperience (anything that improves the experience of using Symfony)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