[PHP-DEV] iterator_count signature for iterables

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> 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

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: Playground | PHPStan

Best regards
Tim Düsterhus

Hey Tim,

···

Marco Pivetta

https://mastodon.social/@ocramius

https://ocramius.github.io/

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: Convert iterable into an internal alias for Traversable|array by Girgias · Pull Request #7309 · php/php-src · GitHub. `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

Hey Tim,

···

Marco Pivetta

https://mastodon.social/@ocramius

https://ocramius.github.io/