Problem/Motivation
When a class uses ComputedItemListTrait, a PHP 8.1 deprecation is trigger on offsetExists because return is not typed as bool.
Steps to reproduce
For example the profile module uses this trait in the class Drupal\profile\Plugin\Field\ProfileEntityFieldItemList. Displaying the login pane in a commerce checkout displays this error.
Proposed resolution
As for the others deprecated functions of this trait we could simply add #[\ReturnTypeWillChange] to suppress the deprecation.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | drupal-offsetexists_deprecation-3257713-2.patch | 522 bytes | aerzas |
Comments
Comment #2
aerzas commentedHere is the patch.
Comment #3
mondrakeWill be fixed in #3257654: Fix PHPStan L0 failures that cannot be included in baseline
Comment #4
aerzas commentedThank you for the heads up, I didn't find the issue.
Closing this one in favor of https://www.drupal.org/project/drupal/issues/3257654
Comment #5
alexpottThe duplicate has been fixed. What's interesting here is that we didn't catch with with core testing. I was going to ask for further testing to be added to code but there's no way that \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList is not tested by core - that is tested by \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList - or maybe we need to ensure the offsetExists method is called... weird.
Comment #6
alexpottYeah if I remove the annotation from \Drupal\Core\TypedData\Plugin\DataType\ItemList::offsetExists() and run \Drupal\Tests\content_moderation\Kernel\ModerationStateFieldItemListTest::testArrayIndex() then the test fails with
I'm not sure why this deprecation is being reported for ProfileEntityFieldItemList as that extends from ItemList too....
Comment #7
aerzas commentedIf I understand it correctly,
ProfileEntityFieldItemListextendsFieldItemListwhich extendsItemList, and then it uses the traitComputedItemListTrait. So the methodoffsetExistsonProfileEntityFieldItemListis not inherited fromItemListbut injected by the traitComputedItemListTraitwhich misses the return type comment. To test this behaviour we would need a dummy class that reproduces this inheritance/trait architecture.Comment #8
aerzas commentedRegarding
ModerationStateFieldItemList, the class explicitly only inherit from thegetmethod of the trait so there is no overridance ofoffsetExistslikeProfileEntityFieldItemList.Of course
ProfileEntityFieldItemListcould be more selective to avoid this override and deprecation but fixing the trait in Drupal core would also be needed for consistency.Comment #9
quietone commentedignore, cleaning up tag.