← All results

Union of homogeneous arrays is not an array of unions

Group: Cross-analyzer regressions · Category: regression · File: regressions_union_of_arrays_vs_array_of_unions.php

@param array<int>|array<string> means "all ints, or all strings" — not "each element may be int or string". A mixed literal such as [1, 'b', 3] is therefore out of range: it is neither array<int> nor array<string>. By contrast array<int|string> would accept the same value. PHPStan historically accepted the mixed array (issue #8963, open as a feature request). This records whether each analyzer distinguishes the two spellings. Reference: phpstan#8963

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.10Phan 6.0.10 php-ast version 1.1.3 PHP version used to run Phan: 8.5.11 Fail

No diagnostics reported.

Expectation diff
Line 36: Expected 1 error(s)
phpstan 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Fail

No diagnostics reported.

Expectation diff
Line 36: Expected 1 error(s)
psalm 6.20.0Psalm 6.20.0@804f03c0c183d149fcd4b8c7c0c9b3fbf836d34c
next: 7.0.0-rc2
Fail (pzoom≠)
pzoom (Psalm port)
regressions_union_of_arrays_vs_array_of_unions.php:36: Argument 1 of takesHomogeneousIntOrStringArray expects array<array-key, int>|array<array-key, string>, but list{1, 'b', 3} provided [InvalidArgument]
Expectation diff
Line 36: Expected 1 error(s)
mago 1.57.0mago 1.57.0 Pass
regressions_union_of_arrays_vs_array_of_unions.php:36: Possible argument type mismatch for argument #1 of `Conformance\Tests\RegressionsUnionOfArraysVsArrayOfUnions\takesHomogeneousIntOrStringArray`: expected `array<array-key, int>|array<array-key, string>`, but possibly received `list{int(1), string('b'), int(3)}`. [possibly-invalid-argument]
mir 0.87.0mir 0.87.0 Pass
regressions_union_of_arrays_vs_array_of_unions.php:36: InvalidArgument: Argument $values of takesHomogeneousIntOrStringArray() expects 'array<int|string, int>|array<int|string, string>', got 'array{0: 1, 1: "b", 2: 3}' [MIR0201]
phpantom 0.11.1phpantom_lsp 0.11.1 Pass
regressions_union_of_arrays_vs_array_of_unions.php:36: Argument 1 ($values) expects array<int>|array<string>, got array{1, 'b', 3} [type_mismatch_argument]
intelephense 1.18.5intelephense 1.18.5 Fail

No diagnostics reported.

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

No diagnostics reported.

Expectation diff
Line 36: Expected 1 error(s)
phpy 1.0.19409phpy 1.0.19409 Fail

No diagnostics reported.

Expectation diff
Line 36: Expected 1 error(s)
qodana 262.10968.76PhpStorm 262.10968.76 Fail

No diagnostics reported.

Expectation diff
Line 36: 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
regressions_union_of_arrays_vs_array_of_unions.php:25: param $values miss matched with phpdoc type <<array<int>|array<string>>> [funcParamTypeMissMatch]
Expectation diff
Line 36: Expected 1 error(s)
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Pass
regressions_union_of_arrays_vs_array_of_unions.php:36: argument [1, 'b', 3] to takesHomogeneousIntOrStringArray() violates declared @param (array<int> | array<string>) $values — declared contract violation [phpdoc.param-mismatch]
phpstan-strict 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Fail

No diagnostics reported.

Expectation diff
Line 36: Expected 1 error(s)
psalm-next 7.0.0-rc2Psalm 7.0.0-rc2@20339e3fe5b8d03bcd8061030eb2a83ad50e5783 Fail

No diagnostics reported.

Expectation diff
Line 36: Expected 1 error(s)

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\RegressionsUnionOfArraysVsArrayOfUnions;

/**
 * Union of homogeneous arrays is not an array of unions.
 *
 * `@param array<int>|array<string>` means "all ints, or all strings" — not
 * "each element may be int or string". A mixed literal such as `[1, 'b', 3]`
 * is therefore out of range: it is neither `array<int>` nor `array<string>`.
 * By contrast `array<int|string>` would accept the same value.
 *
 * PHPStan historically accepted the mixed array (issue #8963, open as a
 * feature request). This records whether each analyzer distinguishes the two
 * spellings.
 *
 * Reference: https://github.com/phpstan/phpstan/issues/8963
 */

/**
 * @param array<int>|array<string> $values
 */
function takesHomogeneousIntOrStringArray(array $values): void // E<noverify>: NoVerify cannot evaluate the generic array union and flags the signature itself
{
}

// Pure int list — member of array<int>.
takesHomogeneousIntOrStringArray([1, 2, 3]);

// Pure string list — member of array<string>.
takesHomogeneousIntOrStringArray(['a', 'b', 'c']);

// Mixed elements: neither array<int> nor array<string>.
takesHomogeneousIntOrStringArray([1, 'b', 3]); // E: array<int|string> is not accepted by array<int>|array<string>