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.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

catch created an issue. See original summary.

andypost’s picture

There's parent meta to track adding type-hints is this issue actionable?

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

catch’s picture

Yes I think it's actionable but we don't have a clear path forwards for it either.

andypost’s picture

I think PHP 8.3 feature https://stitcher.io/blog/new-in-php-83##[override]-attribute-rfc is exactly about it

kingdutch’s picture

I 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.

catch’s picture

Since #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.

mondrake’s picture

Opened 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

mondrake’s picture

mondrake’s picture

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

mondrake’s picture

Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Postponed (maintainer needs more info)

Per #12 ?

mondrake’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.