Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment | File | Size | Author |
---|---|---|---|
#10 | 3224523-10.patch | 47.51 KB | alexpott |
Comments
Comment #2
longwaveExpanding scope to all PHP core interface implementations.
Comment #3
longwaveThere is precedent for this upstream, Symfony did the same thing in e.g. https://github.com/symfony/symfony/pull/41495
Comment #4
alexpottI 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=
Comment #5
alexpottShall 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.
Comment #6
longwaveReverted 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.
Comment #7
andypost@longwave where the changes reverted? I think it needs new patch and session looks better to split out to separate issue
Comment #8
longwave@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.
Comment #9
alexpottI'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...Comment #10
alexpottChanging the scope and generating the patch from #3220021: [meta] Ensure compatibility of Drupal 9 with PHP 8.1 (as it evolves)
Comment #11
alexpottComment #12
andypostCurious about how to make sure no places are missed, maybe there a way to catch it with static analysis....
Comment #13
longwaveSo 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?
Comment #14
andypost@longwave I think we should accept policy first to improve type-hinting, that's why static analysis sounds promising
Comment #15
alexpottThis 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.
Comment #16
alexpottI don't think either of the issues added in #14 should block this. This is only adding comments.
Comment #17
alexpottRe 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
Comment #18
longwaveOpened #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.
Comment #19
catchFor 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.
Comment #20
alexpottAdded a follow-up to determine what we can do in Drupal 9 already - see #3232639: Determine which ReturnTypeWillChange can be fixed in Drupal 9
Comment #21
longwaveThis 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.
Comment #23
larowlanCommitted 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