Problem/Motivation
Split from #3050720: [Meta] Implement strict typing in existing code.
PHP allows child classes to add return type hints in the parent class doesn't. This means we can add return type hints to interfaces in Drupal 11, contrib modules can add them whenever, and those modules will continue to work in both Drupal 10 and 11.
What we can't do, is notify contrib modules that they need to add the type hint to continue to work.
Symfony has partially solved this issue - it looks at @return phpdoc, and if there's no matching type hint, the debug classloader issues a deprecation message. However, this is overkill - it issues deprecations for classes even where the new version doesn't have a return type hint, so can be misleading (and noisy).
Steps to reproduce
Proposed resolution
We want something similar to Symfony's implementation, but explict instead of implicit. For example, could we add an attribute like [#ReturnTypeHintDeprecation]. To do this would require overriding Symfony's debug classloader though.
Comments
Comment #2
andypostThere's parent meta to track adding type-hints is this issue actionable?
Comment #4
catchYes I think it's actionable but we don't have a clear path forwards for it either.
Comment #5
andypostI think PHP 8.3 feature https://stitcher.io/blog/new-in-php-83##[override]-attribute-rfc is exactly about it
Comment #6
kingdutchI previously requested PHPStan functionality for this because it's not just problematic for implementers but also for consumers of the code that's changing. Unfortunately the request was rejected.
Comment #7
catchSince #3377356: Make DebugClassLoader ignored deprecations more accurate we should start getting the Symfony deprecation messages from the classloader. This is not perfect - it's catch-all and doesn't allow for custom messages etc., but it should start to warn modules that have deprecation testing enabled that we might add a type hint later, but we should do a bit of testing with it.
Comment #8
mondrakeOpened an issue to Symfony to see if we can get some tweaks to DebugClassloader to facilitate this task: [ErrorHandler] DebugClassloader: introduce an annotation to signal definite intent to add/change the return typehint of a method
Comment #9
mondrakeOpened #3486376: Extend Symfony DebugClassLoader to report missing cross-module @return types to approach this with a PHPStan custom rule.
Comment #10
mondrakeComment #12
mondrakeIs this outdated by #3486376: Extend Symfony DebugClassLoader to report missing cross-module @return types now?
Comment #13
smustgrave commentedPer #12 ?
Comment #14
mondrake