-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Description
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?