phpstan-symfony
phpstan-symfony copied to clipboard
Symfony service constructor analysis
PHPStan already supports analysis for valid parameters count and type.
Method A::a() invoked with 0 parameters, 1 required.
Parameter #1 $a of method A::a() expects B, C given.
However it does not report invalid constructor parameters for Symfony services. We have access to the container via
parameters:
symfony:
container_xml_path: app/cache/dev/appDevDebugProjectContainer.xml
so we can analyze constructors as well. Do I miss something or this is a feature that is not implemented?
Do you have a code sample that you'd like to report an error on?
I did not update services.yaml after adding another constructor parameter in a class. That's all.
Isn't that reported by Symfony during container compilation?
Anyway, I'd say this falls outside of responsibilites of PHPStan, because there's no AST involved, there's nothing to hook the rule onto.
Isn't that reported by Symfony during container compilation?
It looks like not as the error has occurred on production.
Anyway, I'd say this falls outside of responsibilites of PHPStan, because there's no AST involved, there's nothing to hook the rule onto.
Ok, I won't question you because I don't really know much about AST. It just feels naturally to me that it could be done as we have all the information, i.e. a container file and a class, and we could compare what's inside PHP class constructor and inside services.yaml configuration file.
Isn't that reported by Symfony during container compilation?
Yep, it should be.
I don't think it's necessary to implement any checks here, especially because you should always recompile the container before running PHPStan on it.
Yep, it should be.
Maybe it's reported since the specific Symfony version? Do you know which one?
Actually you might be right. There might not be an error until you instantiate the service (or any other service that depends on it, even indirectly).
Feel free to explore this issue and maybe send a PR. I'll be happy to take a look at it.
One idea how to implement such rule - just hook onto a FileNode
and execute your logic on the first file that's analysed.
Sorry, accidental close.
Feel free to explore this issue and maybe send a PR. I'll be happy to take a look at it.
Hmmm, this feature look encouragingly. It would also be a nice first contribution to PHPStan but also a challenging one as I've never worked with AST (and PHPStan internally). Will give it a try in my spare time but at the same time I'll be ready to be disappointed.
Be sure to check out https://phpstan.org/developing-extensions/extension-types.