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

Add extensible ClassReflection::getAllowedSubTypes()

Open jiripudil opened this issue 3 years ago • 6 comments

...to allow restricting inheritance hierarchies in userland – but also internally; I've rewritten the current DateTimeInterface and enums support into this extension point.

Closes phpstan/phpstan#7493

jiripudil avatar Jun 27 '22 06:06 jiripudil

I guess with this extension in place, we should be able to write a rule in phpstan-src that actually checks an extending class in regard to ClassReflection::getAllowedSubTypes()?

ondrejmirtes avatar Jun 27 '22 07:06 ondrejmirtes

And yeah, sorry, the build isn't going to be absolutely green because of some failures I have in https://github.com/phpstan/phpstan/actions/workflows/extension-tests-run.yml now.

ondrejmirtes avatar Jun 27 '22 07:06 ondrejmirtes

It seems most of the failures relate to the null-safe operator I've used in ObjectType. Am I not allowed to use it, even though composer.json requires PHP ^8.0?

jiripudil avatar Jun 27 '22 07:06 jiripudil

@jiripudil I think it's a question of configuring Rector to downgrade it correctly here https://github.com/phpstan/phpstan-src/blob/1.7.x/build/rector-downgrade.php. (DowngradeNullsafeToTernaryOperatorRector is missing there).

ondrejmirtes avatar Jun 27 '22 07:06 ondrejmirtes

Also, I've realized this probably won't play well with generics, be it generic parent, generic descendant, or both. I don't have a use case for that right now, but I can imagine someone might have. I'll give it some thought to see if it's possible to implement it later with the current API.

jiripudil avatar Jun 27 '22 07:06 jiripudil

I'd really welcome the rule that checks allowed extends/implements here :) Otherwise it's 👍

ondrejmirtes avatar Sep 22 '22 09:09 ondrejmirtes

Perfect! I especially like the code generalization in ObjectType and StaticType about enum subtraction :) Thank you very much.

ondrejmirtes avatar Oct 16 '22 05:10 ondrejmirtes

FYI I've just written a documentation about this feature: https://phpstan.org/developing-extensions/allowed-subtypes Feel free to add anything!

ondrejmirtes avatar Nov 02 '22 10:11 ondrejmirtes