Problem/Motivation

In PHP 8.1 a tentative return type was added to the following methods:

  • JsonSerializable::jsonSerialize()
  • SessionHandlerInterface::open()
  • SessionHandlerInterface::close()
  • SessionHandlerInterface::read()
  • SessionHandlerInterface::write()
  • SessionHandlerInterface::destroy()
  • SessionHandlerInterface::gc()
  • Iterator::current()
  • Iterator::next()
  • Iterator::key()
  • Iterator::valid()
  • Iterator::rewind()
  • Countable::count()
  • IteratorAggregate::getIterator()
  • php_user_filter::filter()
  • ArrayAccess::offsetGet()
  • ArrayAccess::offsetSet()
  • ArrayAccess::offsetUnset()
  • ArrayAccess::offsetExists()
  • RecursiveIterator::getChildren()
  • FilterIterator::accept()
  • ReflectionClass::*()
  • Exception::__wakeup()

These trigger a deprecation warning on existing code. Code that is compatible with PHP 8.1 only can add the return type now, for backward compatibility we can declare #[ReturnTypeWillChange] which silences the warning and tells PHP we will add the return type later on.

Steps to reproduce

Proposed resolution

Add #[ReturnTypeWillChange] to all the necessary implementations.

Remaining tasks

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

longwave created an issue. See original summary.

longwave’s picture

Title: [PHP 8.1] Add ReturnTypeWillChange attribute to JsonSerializable implementations » [PHP 8.1] Add ReturnTypeWillChange attribute to core PHP class/interface implementations
Issue summary: View changes

Expanding scope to all PHP core interface implementations.

longwave’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
8.6 KB

There is precedent for this upstream, Symfony did the same thing in e.g. https://github.com/symfony/symfony/pull/41495

alexpott’s picture

I wonder if it is okay to actually add the return typehint to \Drupal\Component\Render\MarkupTrait. I think if you use the trait you should be overriding __jsonSerialize() - no one in contrib does this... http://grep.xnddx.ru/search?text=MarkupTrait&filename=

alexpott’s picture

+++ b/core/lib/Drupal/Core/Session/SessionHandler.php
@@ -46,6 +46,7 @@ public function __construct(RequestStack $request_stack, Connection $connection)
   /**
    * {@inheritdoc}
    */
+  #[\ReturnTypeWillChange]
   public function open($save_path, $name) {
     return TRUE;
   }

Shall we broaden the scope of this issue? I think it might be better to move the session stuff into its own one. And leave the test one to a fix tests on PHP 8.1 issue.

longwave’s picture

Status: Active » Needs work

Reverted back to the previous scope.

Also #4 is probably a good idea, we will have to add these return types sooner or later somehow (probably with the same strategy Symfony uses), but if we can do it now I think we should.

andypost’s picture

@longwave where the changes reverted? I think it needs new patch and session looks better to split out to separate issue

longwave’s picture

@andypost I changed the issue title but it did not show in the metadata of comment #6 for some reason, the patch needs work to reduce scope back to JsonSerializable only.

alexpott’s picture

I've been thinking about the scope here and actually I'm more and more thinking let's add all the #[ReturnTypeWillChange] in one issue and the open issues to add the proper typehints. These annotations are merely comments so I don't think a scope really helps...

alexpott’s picture

Title: [PHP 8.1] Add ReturnTypeWillChange attribute to JsonSerializable implementations » [PHP 8.1] Add ReturnTypeWillChange attribute where necessary
Issue summary: View changes
Status: Needs work » Needs review
FileSize
47.51 KB
alexpott’s picture

Issue summary: View changes
andypost’s picture

Curious about how to make sure no places are missed, maybe there a way to catch it with static analysis....

longwave’s picture

So this silences the deprecations, but what is our longer term plan here? Are we going to add the return types in D10 with the assumption that PHP 9 may be released in D10's lifetime? I assume that if we add these here, downstream users that extend these classes again will get the same deprecation - what do we tell them to do?

andypost’s picture

alexpott’s picture

+++ b/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php
@@ -64,6 +64,7 @@ public function __sleep() {
   /**
    * {@inheritdoc}
    */
+  #[\ReturnTypeWillChange]
   public function __wakeup() {

This is kinda the odd one out. It's because this trait is used in \Drupal\jsonapi\Exception\UnprocessableHttpEntityException and \Drupal\jsonapi\Exception\EntityAccessDeniedHttpException - I'm not sure that we should be adding : void here in the future...

Re #12 we get testing once we enable PHP 8.1 testing. It doesn't matter if we missing something here. We can't enable PHP 8.1 till we've fixed everything else.

Re D10 - yes we're going to need to add return types for PHP 9. My guess is that we'll see more and more of this in the next PHP 8 minor releases.

Re downstream - if they are not extended or don't care then they can implement the return type otherwise they've have to add the annotation to to be PHP 8.1 compatible. The reason we're doing this is to make downstream code not have to change right away.

alexpott’s picture

I don't think either of the issues added in #14 should block this. This is only adding comments.

alexpott’s picture

Re DependencySerializationTrait - I'm not sure we should be using that on exceptions - as far as our test coverage goes these usages of the trait are not necessary - see #2561915-24: alexpott's test issue

longwave’s picture

Opened #3231040: Remove DependencySerializationTrait from JSON API exceptions for #15/17.

Agreed with the rest of #15. Should we open a followup from here to actually figure out how we are going to add these return types in D10, because that seems like a huge can of worms waiting for us.

catch’s picture

For policy we already have #1158720: Improve text for parameter type hinting in function declaration. However we need a specific follow-up to remove ReturnTypeWillChange from everywhere we've added it probably.

There are several places in the patch where I think we could add the return type hint in Drupal 9 too - it's not different to any other change to @internal code where we don't expect subclassing.

alexpott’s picture

Added a follow-up to determine what we can do in Drupal 9 already - see #3232639: Determine which ReturnTypeWillChange can be fixed in Drupal 9

longwave’s picture

Status: Needs review » Reviewed & tested by the community

This looks comprehensive enough for now, if there are any missed stragglers we can deal with them in another PHP 8.1 issue, and the followup is open to investigate what we can move ahead with straight away.

  • larowlan committed f06ad93 on 9.3.x
    Issue #3224523 by alexpott, longwave: [PHP 8.1] Add ReturnTypeWillChange...
larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed f06ad93 and pushed to 9.3.x. Thanks!

Feels like a good first step to unlock progress and then go over it with a finer tooth comb

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.