Add extensible ClassReflection::getAllowedSubTypes()
...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
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()?
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.
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 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).
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.
I'd really welcome the rule that checks allowed extends/implements here :) Otherwise it's 👍
Perfect! I especially like the code generalization in ObjectType and StaticType about enum subtraction :) Thank you very much.
FYI I've just written a documentation about this feature: https://phpstan.org/developing-extensions/allowed-subtypes Feel free to add anything!