← 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.7Phan 6.0.7 php-ast version 1.1.3 PHP version used to run Phan: 8.5.9 Fail

No diagnostics reported.

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

No diagnostics reported.

Expectation diff
Line 36: Expected 1 error(s)
psalm 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac
next: 7.0.0-beta19
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.46.0mago 1.46.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.70.1mir 0.70.1 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.9.0phpantom_lsp 0.9.0 Fail

No diagnostics reported.

Expectation diff
Line 36: Expected 1 error(s)
intelephense 1.18.5intelephense 1.18.5 Fail

No diagnostics reported.

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

No diagnostics reported.

Expectation diff
Line 36: Expected 1 error(s)
qodana 262.8665.325Qodana 262.8665.325 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.5steins 0.1.5 (2026-08-12 revision 39b4cc1) 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.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Fail

No diagnostics reported.

Expectation diff
Line 36: Expected 1 error(s)
psalm-next 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 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>