fix: IterableFieldTrait:current() should be compatible with Iterator:… #3404
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
…:current()
As of PHP 8.1 methods return types should be compatible with their parent/interface. I.e. deprecration as of 8.1 enforced as of 9.0. IterableFieldTrait::current() should therefore return mixed, the same as Iterator::current(). However this is not possible to do without dropping support for PHP < 8.0, as mixed was introduced then.
We fix this by creating different versions of IterableFieldTrait based on the PHP version; defining current() with(out) a return type based on the (major) version of PHP and/or the availability of mixed.
phpstan: flip logic so that phpstan loads correct trait.
This fix is not a nice fix, nor is it elegant. However, it is a fix.. and that's something.
It aims to fix the issue first raised in #3395 by @enrise-mbraam. TLDR; Anything implementing the Iterator interface should have
mixed
as the return type ofcurrent()
. However, adding that return type, would break the code for PHP < 8, as this is whenmixed
was first introduced.Unfortunately this PR is the only viable solution that I see, short of dropping support for PHP < 8.
This work around should be removed when minimum PHP version is bumped to 8.0. Which should/will be done in bolt/bolt#7969 (couldn't find the moved issue).
Marking this as WIP, as we should discuss/explore more elegant solutions.