[PR #10002] Make paginator covariant #12036

Closed
opened 2026-01-22 16:12:43 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/10002

State: closed
Merged: Yes


see https://phpstan.org/blog/whats-up-with-template-covariant
see https://psalm.dev/docs/annotating_code/templated_annotations/

A method which accepts Paginator<Animal> should be able to accept Paginator<Cat>.

I received the error

Template type T is declared as covariant, but occurs in invariant position in return type of method Doctrine\ORM\Tools\Pagination\Paginator::getIterator().

It's because the template of ArrayIterator is not covariant.
But the template for Traversable is covariant.

It makes sens to use Traversable instead of ArrayIterator as return type because

  • IteratorAggregate::getIterator() use Traversable as return type
  • it doesn't make sens to allow adding elements to this Iterator

ArrayIterator implements SeekableIterator, ArrayAccess, Serializable, Countable
the issue is the ArrayAccess.

If you preferred I can use somethink like

@psalm-return Traversable<array-key, T>&Countable
@psalm-return Traversable<array-key, T>&Serializable&Countable
@psalm-return SeekableIterator<array-key, T>&Serializable&Countable
...

Any suggestion @greg0ire ?

**Original Pull Request:** https://github.com/doctrine/orm/pull/10002 **State:** closed **Merged:** Yes --- see https://phpstan.org/blog/whats-up-with-template-covariant see https://psalm.dev/docs/annotating_code/templated_annotations/ A method which accepts `Paginator<Animal>` should be able to accept `Paginator<Cat>`. I received the error ``` Template type T is declared as covariant, but occurs in invariant position in return type of method Doctrine\ORM\Tools\Pagination\Paginator::getIterator(). ``` It's because the template of ArrayIterator is not covariant. But the template for Traversable is covariant. It makes sens to use Traversable instead of ArrayIterator as return type because - IteratorAggregate::getIterator() use Traversable as return type - it doesn't make sens to allow adding elements to this Iterator ArrayIterator implements SeekableIterator, ArrayAccess, Serializable, Countable the issue is the ArrayAccess. If you preferred I can use somethink like ``` @psalm-return Traversable<array-key, T>&Countable @psalm-return Traversable<array-key, T>&Serializable&Countable @psalm-return SeekableIterator<array-key, T>&Serializable&Countable ... ``` Any suggestion @greg0ire ?
admin added the pull-request label 2026-01-22 16:12:43 +01:00
admin closed this issue 2026-01-22 16:12:43 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#12036