Skip to content

Union of two literal arrays with the same keys in a different order keeps only one key order #15679

Open
phpstan/phpstan-src
#7108

Description

@gnutix

Bug report

When a variable holds one of two arrays written as literals, and both arrays have the same keys in a different order, PHPStan infers a single array shape that follows the key order of only one of the two arrays. The key order of the other array is lost, so everything that depends on key order gets a certain answer that is wrong for one of the two arrays:

  • array_is_list() is reported as always true when one of the arrays is not a list (function.alreadyNarrowedType), and the branch where it is not a list is treated as impossible (the variable is *NEVER* there);
  • with the branches written the other way round, array_is_list() is reported as always false although one of the arrays is a list (function.impossibleType);
  • array_values() is reported as having no effect (arrayValues.list) on a value that may not be a list;
  • array_keys() on string-keyed arrays returns the keys in one order only.

It happens when the two arrays differ in the value of at most one key, including when they hold exactly the same key/value pairs. When the values differ at two keys, PHPStan keeps both arrays in a union and everything above is correct.

Related: #11600 and #12725, about array shapes from PHPDoc, whose key order is not known. Here both arrays are literals, so their key order is known.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

function f(bool $b): void
{
	$x = $b ? [1 => 0, 0 => 0] : [0 => 1, 1 => 0];
	\PHPStan\dumpType($x);
	\PHPStan\dumpType(array_is_list($x));

	$y = $b ? [1 => 'a', 0 => 'b'] : [0 => 'c', 1 => 'a'];
	\PHPStan\dumpType($y);
	\PHPStan\dumpType(array_is_list($y));
}

https://phpstan.org/r/d522e353-9273-4654-b30e-139e3a79cc35

Actual output on 2.3.x HEAD (phpstan/phpstan-src@28494e56d) and 2.3.6, level max:

 6  Dumped type: array{0|1, 0}
 7  Call to function array_is_list() with array{0|1, 0} will always evaluate to true.  [function.alreadyNarrowedType]
 7  Dumped type: true
10  Dumped type: array{'b'|'c', 'a'}
11  Call to function array_is_list() with array{'b'|'c', 'a'} will always evaluate to true.  [function.alreadyNarrowedType]
11  Dumped type: true

More variants, with what works for comparison at the end:

<?php declare(strict_types = 1);

function g(bool $b): void
{
	// Same key/value pairs, only the order differs
	$same = $b ? [1 => 'a', 0 => 'b'] : [0 => 'b', 1 => 'a'];
	\PHPStan\dumpType($same);
	\PHPStan\dumpType(array_is_list($same));

	// The branch where the array is not a list is considered impossible
	$y = $b ? [1 => 'a', 0 => 'b'] : [0 => 'c', 1 => 'a'];
	if (!array_is_list($y)) {
		\PHPStan\dumpType($y);
	}

	// array_values() on the union is reported as having no effect
	$x = $b ? [1 => 0, 0 => 0] : [0 => 1, 1 => 0];
	\PHPStan\dumpType(array_values($x));

	// if/else instead of a ternary: the shape follows the other array
	if ($b) {
		$z = [1 => 'a', 0 => 'b'];
	} else {
		$z = [0 => 'c', 1 => 'a'];
	}
	\PHPStan\dumpType($z);
	\PHPStan\dumpType(array_is_list($z));

	// String keys
	$s = $b ? ['x' => 1, 'y' => 2] : ['y' => 2, 'x' => 3];
	\PHPStan\dumpType($s);
	\PHPStan\dumpType(array_keys($s));

	// For comparison: values differ at two keys, both arrays are kept
	$ok = $b ? [1 => 'a', 0 => 'b'] : [0 => 'c', 1 => 'd'];
	\PHPStan\dumpType($ok);
	\PHPStan\dumpType(array_is_list($ok));
}

Actual output on 2.3.x HEAD (phpstan/phpstan-src@28494e56d), level max:

 7  Dumped type: array{'b', 'a'}
 8  Call to function array_is_list() with array{'b', 'a'} will always evaluate to true.  [function.alreadyNarrowedType]
 8  Dumped type: true
12  Call to function array_is_list() with array{'b'|'c', 'a'} will always evaluate to true.  [function.alreadyNarrowedType]
13  Dumped type: *NEVER*
18  Dumped type: array{0|1, 0}
18  Parameter #1 $array (array{0|1, 0}) of array_values is already a list, call has no effect.  [arrayValues.list]
26  Dumped type: array{1: 'a', 0: 'b'|'c'}
27  Call to function array_is_list() with array{1: 'a', 0: 'b'|'c'} will always evaluate to false.  [function.impossibleType]
27  Dumped type: false
31  Dumped type: array{y: 2, x: 1|3}
32  Dumped type: array{'y', 'x'}
36  Dumped type: array{'c', 'd'}|array{1: 'a', 0: 'b'}
37  Dumped type: bool

Swapping the two branches of the if/else gives array{'b'|'c', 'a'} and "always evaluate to true" instead. The output is the same with and without bleeding edge. The function.alreadyNarrowedType, function.impossibleType and arrayValues.list reports appear from the level where those rules are enabled; the wrong inferred types are there at every level.

Expected output

A type that keeps the key order of both arrays, like array{1, 0}|array{1: 0, 0: 0} for $x and array{'c', 'a'}|array{1: 'a', 0: 'b'} for $y (the same kind of union PHPStan already infers for $ok above). Then:

  • bool for every array_is_list() call above, with no function.alreadyNarrowedType or function.impossibleType;
  • $y in the !array_is_list() branch is array{1: 'a', 0: 'b'}, not *NEVER*;
  • no arrayValues.list on array_values($x);
  • array{'x', 'y'}|array{'y', 'x'} for array_keys($s).

PHPStan version

Reproduced on 2.3.6 and on 2.3.x HEAD (phpstan/phpstan-src@28494e56d). The merged shape (array{'b'|'c', 'a'} for $y) is the same in every release I tried back to 1.12.0. What changed in 2.3.0 is only the wrong answer for array_is_list($y): 1.12.0 to 2.2.0 report it as always false, 2.3.0 and later as always true.

Why it matters

Code that builds an array in two branches and then checks or normalises its key order (array_is_list(), array_values(), array_keys()) gets false positives. In the branch that handles the other case, the variable is *NEVER*, so the code there is checked against the wrong type: with if (!array_is_list($y)) { return strlen($y[1]); }, $y[1] is mixed and PHPStan reports strlen() expecting string, mixed given, although $y[1] is 'a' in both arrays. I ran into it while making a PHPStan extension infer the carry of our own reduce() helpers, where the accumulated array can be built in either key order.

Activity

  1. mergeable commented on Nov 7, 2026

    @mergeable

    This bug report is missing a link to reproduction at phpstan.org/try.

    It will most likely be closed after manual review.

  2. added this to the Easy fixes milestone on Nov 19, 2026
  3. changed the title [-]Union of two literal arrays with the same keys in a different order becomes a list[/-] [+]Union of two literal arrays with the same keys in a different order keeps only one key order[/+] on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions