← All results

iterator_to_array() keeps the iterator's key and value types

Group: PHPDoc generics · Category: phpdoc · File: generics_iterator_to_array.php

iterator_to_array($it) on an Iterator<int, int> is array<int, int>; with preserve_keys: false the values become a list. Passing the result where the other element type is required is the probe. The iterators are declared parameters, so the measurement does not depend on inferring a template from a constructor — that is generics_array_iterator_constructor_inference. Source: jorgsowa/mir v0.86.0. References: - https://github.com/jorgsowa/mir/releases/tag/v0.86.0

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.11Phan 6.0.11 php-ast version 1.1.3 PHP version used to run Phan: 8.5.11 Pass
generics_iterator_to_array.php:44: Argument 1 ($list) is iterator_to_array($wrong) of type int[] but \Conformance\Tests\GenericsIteratorToArray\takesStringList() takes list<string> defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/generics_iterator_to_array.php:33 [PhanTypeMismatchArgument]
generics_iterator_to_array.php:54: Argument 1 ($array) is iterator_to_array($wrong, preserve_keys: false) of type list<string> but \Conformance\Tests\GenericsIteratorToArray\takesIntMap() takes array<int,int> defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/generics_iterator_to_array.php:26 [PhanTypeMismatchArgument]
phpstan 2.3.5PHPStan - PHP Static Analysis Tool 2.3.5 Pass reported Lv.5+
generics_iterator_to_array.php:44: Parameter #1 $list of function Conformance\Tests\GenericsIteratorToArray\takesStringList expects list<string>, array<int, int> given. [reported-from-level=5]
generics_iterator_to_array.php:54: Parameter #1 $array of function Conformance\Tests\GenericsIteratorToArray\takesIntMap expects array<int, int>, list<string> given. [reported-from-level=5]
With strict-rules
generics_iterator_to_array.php:44: Parameter #1 $list of function Conformance\Tests\GenericsIteratorToArray\takesStringList expects list<string>, array<int, int> given.
generics_iterator_to_array.php:54: Parameter #1 $array of function Conformance\Tests\GenericsIteratorToArray\takesIntMap expects array<int, int>, list<string> given.
psalm 6.20.9Psalm 6.20.9@f67170ac3c3b931f5088681e21d6c8babc601c08
next: 7.0.4
Pass
generics_iterator_to_array.php:44: Argument 1 of Conformance\Tests\GenericsIteratorToArray\takesStringList expects list<string>, but parent type array<int, int> provided [ArgumentTypeCoercion]
generics_iterator_to_array.php:54: Argument 1 of Conformance\Tests\GenericsIteratorToArray\takesIntMap expects array<int, int>, but list<string> provided [InvalidArgument]
mago 1.59.0mago 1.59.0 Pass
generics_iterator_to_array.php:44: Invalid argument type for argument #1 of `Conformance\Tests\GenericsIteratorToArray\takesStringList`: expected `list<string>`, but found `array<int, int>`. [invalid-argument]
generics_iterator_to_array.php:54: Invalid argument type for argument #1 of `Conformance\Tests\GenericsIteratorToArray\takesIntMap`: expected `array<int, int>`, but found `list<string>`. [invalid-argument]
mir 0.91.0mir 0.91.0 Pass
generics_iterator_to_array.php:44: InvalidArgument: Argument $list of takesStringList() expects 'list<string>', got 'array<int, int>' [MIR0201]
generics_iterator_to_array.php:54: InvalidArgument: Argument $array of takesIntMap() expects 'array<int, int>', got 'list<string>' [MIR0201]
phpantom 0.11.1phpantom_lsp 0.11.1 Pass
generics_iterator_to_array.php:44: Argument 1 ($list) expects list<string>, got array<int, int> [type_mismatch_argument]
generics_iterator_to_array.php:54: Argument 1 ($array) expects array<int, int>, got list<string> [type_mismatch_argument]
intelephense 1.18.5intelephense 1.18.5 Fail

No diagnostics reported.

Expectation diff
Line 44: Expected 1 error(s)
Line 54: Expected 1 error(s)
phpactor 2026.10.07.0Phpactor 2026.10.07.0 Fail

No diagnostics reported.

Expectation diff
Line 44: Expected 1 error(s)
Line 54: Expected 1 error(s)
phpy 1.0.19490phpy 1.0.19490 Fail

No diagnostics reported.

Expectation diff
Line 44: Expected 1 error(s)
Line 54: Expected 1 error(s)
qodana 262.11879.104PhpStorm 262.11879.104 Fail

No diagnostics reported.

Expectation diff
Line 44: Expected 1 error(s)
Line 54: Expected 1 error(s)
noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Fail
generics_iterator_to_array.php:26: param $array miss matched with phpdoc type <<array<int, int>>> [funcParamTypeMissMatch]
generics_iterator_to_array.php:33: param $list miss matched with phpdoc type <<list<string>>> [funcParamTypeMissMatch]
generics_iterator_to_array.php:41: param $ok miss matched with phpdoc type <<\Iterator<int, int>>> [funcParamTypeMissMatch]
generics_iterator_to_array.php:41: param $wrong miss matched with phpdoc type <<\Iterator<int, int>>> [funcParamTypeMissMatch]
generics_iterator_to_array.php:51: param $ok miss matched with phpdoc type <<\Iterator<int, string>>> [funcParamTypeMissMatch]
generics_iterator_to_array.php:51: param $wrong miss matched with phpdoc type <<\Iterator<int, string>>> [funcParamTypeMissMatch]
Expectation diff
Line 44: Expected 1 error(s)
Line 54: Expected 1 error(s)
Line 26: Unexpected errors ["param $array miss matched with phpdoc type <<array<int, int>>> [funcParamTypeMissMatch]"]
Line 33: Unexpected errors ["param $list miss matched with phpdoc type <<list<string>>> [funcParamTypeMissMatch]"]
Line 41: Unexpected errors ["param $ok miss matched with phpdoc type <<\\Iterator<int, int>>> [funcParamTypeMissMatch]","param $wrong miss matched with phpdoc type <<\\Iterator<int, int>>> [funcParamTypeMissMatch]"]
Line 51: Unexpected errors ["param $ok miss matched with phpdoc type <<\\Iterator<int, string>>> [funcParamTypeMissMatch]","param $wrong miss matched with phpdoc type <<\\Iterator<int, string>>> [funcParamTypeMissMatch]"]
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Fail

No diagnostics reported.

Expectation diff
Line 44: Expected 1 error(s)
Line 54: Expected 1 error(s)
phpstan-strict 2.3.5PHPStan - PHP Static Analysis Tool 2.3.5 Pass
generics_iterator_to_array.php:44: Parameter #1 $list of function Conformance\Tests\GenericsIteratorToArray\takesStringList expects list<string>, array<int, int> given.
generics_iterator_to_array.php:54: Parameter #1 $array of function Conformance\Tests\GenericsIteratorToArray\takesIntMap expects array<int, int>, list<string> given.
psalm-next 7.0.4Psalm 7.0.4@fa5b1f6e665cb743838ffd865b8fc96207f9409f Pass
generics_iterator_to_array.php:44: Argument 1 of Conformance\Tests\GenericsIteratorToArray\takesStringList expects list<string>, but parent type array<int, int> provided [ArgumentTypeCoercion]
generics_iterator_to_array.php:54: Argument 1 of Conformance\Tests\GenericsIteratorToArray\takesIntMap expects array<int, int>, but list<string> provided [InvalidArgument]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\GenericsIteratorToArray;

/**
 * `iterator_to_array()` keeps the iterator's key and value types.
 *
 * `iterator_to_array($it)` on an `Iterator<int, int>` is `array<int, int>`;
 * with `preserve_keys: false` the values become a `list`. Passing the result
 * where the other element type is required is the probe. The iterators are
 * declared parameters, so the measurement does not depend on inferring a
 * template from a constructor — that is
 * `generics_array_iterator_constructor_inference`.
 *
 * Source: jorgsowa/mir v0.86.0.
 *
 * References:
 * - https://github.com/jorgsowa/mir/releases/tag/v0.86.0
 */

/**
 * @param array<int, int> $array
 */
function takesIntMap(array $array): void
{
}

/**
 * @param list<string> $list
 */
function takesStringList(array $list): void
{
}

/**
 * @param \Iterator<int, int> $ok
 * @param \Iterator<int, int> $wrong
 */
function fromIntIterator(\Iterator $ok, \Iterator $wrong): void
{
    takesIntMap(iterator_to_array($ok)); // V
    takesStringList(iterator_to_array($wrong)); // E: values are int, not string
}

/**
 * @param \Iterator<int, string> $ok
 * @param \Iterator<int, string> $wrong
 */
function fromStringIteratorWithoutKeys(\Iterator $ok, \Iterator $wrong): void
{
    takesStringList(iterator_to_array($ok, preserve_keys: false)); // V
    takesIntMap(iterator_to_array($wrong, preserve_keys: false)); // E: values are string, not int
}