iterator_count signature for iterables

php.internals

[ ]

1 year ago
Hello internals! Currently, iterator_count has the following signature: iterator_count(Traversable|array $iterator): int If we try to use iterable type and pass it to iterator_count function, we'd get following static analysis error: Argument 1 of iterator_count expects Traversable|array<array-key, mixed>, but iterable<int, list<string>> provided (see https://psalm.dev/004) $start(iterator_count($chunks)); From what I know, iterable is just built-in compile time type alias for array|Traversable (https://www.php.net/manual/en/language.types.iterable.php) I think it would make sense to change the signature of iterator_count to accept iterable rather than array|Traversable so that static analysis tools would pick it up better

Tim Düsterhus

1 year ago
Hi Am 2024-11-26 10:40, schrieb Eugene Sidelnyk:
> I think it would make sense to change the signature of iterator_count > to > accept iterable rather than array|Traversable so that static analysis > tools > would pick it up better
That sounds like a bug in Psalm's understanding of union types. PHPStan handles it just fine: https://phpstan.org/r/8604ef1d-3500-4695-8c23-bb5d30ce4268 Best regards Tim Düsterhus

Marco Pivetta

1 year ago
Hey Tim, On Tue, 26 Nov 2024 at 10:50, Tim Düsterhus <tim@bastelstu.be> wrote:
> Hi > > Am 2024-11-26 10:40, schrieb Eugene Sidelnyk: > > I think it would make sense to change the signature of iterator_count > > to > > accept iterable rather than array|Traversable so that static analysis > > tools > > would pick it up better > > That sounds like a bug in Psalm's understanding of union types. PHPStan > handles it just fine: > https://phpstan.org/r/8604ef1d-3500-4695-8c23-bb5d30ce4268 >
Is `Traversable|array` an alias of `iterable`? Is it **always** like that? Marco Pivetta https://mastodon.social/@ocramius https://ocramius.github.io/

Tim Düsterhus

1 year ago
Hi Am 2024-11-26 12:00, schrieb Marco Pivetta:
> Is `Traversable|array` an alias of `iterable`? Is it **always** like > that?
Since PHP 8.2 `iterable` is internally transformed into `Traversable|array` on the engine level: https://github.com/php/php-src/pull/7309. `iterable` no longer exists as a type since then. But even before that it was *semantically* identical, but technically it was a distinct type. Best regards Tim Düsterhus

Marco Pivetta

1 year ago
Hey Tim, On Tue, 26 Nov 2024 at 12:07, Tim Düsterhus <tim@bastelstu.be> wrote:
> it was *semantically* identical, >
That's the most important bit, thanks! That means that upstream Psalm may translate back/forth from it into a uniform version :-) To Eugene: consider posting this to the Psalm issue tracker. Marco Pivetta https://mastodon.social/@ocramius https://ocramius.github.io/