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

Symfony service constructor analysis

Open javaDeveloperKid opened this issue 3 years ago • 12 comments

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?

javaDeveloperKid avatar Feb 08 '22 10:02 javaDeveloperKid

Do you have a code sample that you'd like to report an error on?

ondrejmirtes avatar Feb 08 '22 10:02 ondrejmirtes

I did not update services.yaml after adding another constructor parameter in a class. That's all.

javaDeveloperKid avatar Feb 08 '22 11:02 javaDeveloperKid

Isn't that reported by Symfony during container compilation?

ondrejmirtes avatar Feb 08 '22 11:02 ondrejmirtes

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.

ondrejmirtes avatar Feb 08 '22 11:02 ondrejmirtes

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.

javaDeveloperKid avatar Feb 08 '22 12:02 javaDeveloperKid

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.

lookyman avatar Feb 08 '22 12:02 lookyman

Yep, it should be.

Maybe it's reported since the specific Symfony version? Do you know which one?

javaDeveloperKid avatar Feb 08 '22 12:02 javaDeveloperKid

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.

lookyman avatar Feb 08 '22 21:02 lookyman

One idea how to implement such rule - just hook onto a FileNode and execute your logic on the first file that's analysed.

ondrejmirtes avatar Feb 09 '22 08:02 ondrejmirtes

Sorry, accidental close.

ondrejmirtes avatar Feb 09 '22 08:02 ondrejmirtes

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.

javaDeveloperKid avatar Feb 10 '22 08:02 javaDeveloperKid

Be sure to check out https://phpstan.org/developing-extensions/extension-types.

lookyman avatar Feb 10 '22 16:02 lookyman