Repository navigation
list is wrongly lost or wrongly kept after a two-dimensional offset assignment like $list[array_key_last($list)][] = … or $matrix[$row][$column] = … #15245
Description
Bug report
When PHPStan infers the type of an array after a nested offset write ($a[X][Y] = $v), it checks the wrong chain link at each dimension. It asks whether an offset expression type is tracked, and whether the list type should be kept, about $a[X] while it writes Y into $a[X], and about $a[X][Y] while it writes X into $a. Depending on the code, this gives a false positive or an unsound list<…> type.
/**
* @param non-empty-list<list<int>|null> $groups
* @return non-empty-list<list<int>|null>
*/
function appendToLastGroup(array $groups, int $value): array
{
$groups[array_key_last($groups)][] = $value;
\PHPStan\dumpType($groups);
// expected: non-empty-list<non-empty-list<int>>
return $groups;
}
/**
* @param non-empty-list<list<int>> $matrix
* @return non-empty-list<list<int>>
*/
function setInLastRow(array $matrix, int $column, int $value): array
{
$matrix[array_key_last($matrix)][$column] = $value;
\PHPStan\dumpType($matrix);
// expected: the row is no longer a list ([[]] becomes [[5 => 1]])
return $matrix;
}
/**
* @param list<list<int>> $matrix
* @return list<list<int>>
*/
function setInExistingRow(array $matrix, int $row, int $column, int $value): array
{
if (isset($matrix[$row])) {
$matrix[$row][$column] = $value;
\PHPStan\dumpType($matrix);
// expected: the row is no longer a list
}
return $matrix;
}Actual output (level 8):
10 Dumped type: non-empty-array<int<0, max>, non-empty-list<int>>
14 Function appendToLastGroup() should return non-empty-list<list<int>|null> but returns non-empty-array<int<0, max>, non-empty-list<int>>.
24 Dumped type: non-empty-list<non-empty-list<int>>
39 Dumped type: non-empty-list<non-empty-list<int>>
appendToLastGroup()gets a false positive. Writing to$groups[array_key_last($groups)]does not add a key, and the one-dimensional$groups[array_key_last($groups)] = [$value];keeps thelist.setInLastRow()andsetInExistingRow()get no error. The rows are still typed as lists even though$columncan be any integer, so the missingreturn.typeerror is a false negative.
Mechanism (line numbers from 2.3.x):
prepareTarget()reverses the dim-fetch stack so it goes from the innermost link to the outermost one (AssignHandler.php:771). For$a[X][Y]the stack is[$a[X], $a[X][Y]].$offsetTypesuses the same order, and each entry already carries its ownArrayDimFetch(L779, L804).produceArrayDimFetchAssignValueToWrite()walks the chain withforeach (array_reverse($offsetTypes) as $i => [$offsetType])(L2632), so$i === 0is the outermost dimension. The other uses of$iagree with this:$unionValuesfor$i === 0, and$containerKey = $lastDimKey - $i - 1(L2698).- However,
$arrayDimFetch = $dimFetchStack[$i] ?? null;(L2646) indexes the stack from the innermost end. With two or more dimensions, the outermost offset is paired with the innermost link and vice versa. Only the middle link of an odd-length chain happens to match. - The mismatched
$arrayDimFetchfeeds two decisions:$scope->hasExpressionType($arrayDimFetch)(L2650) pickssetExistingOffsetValueType(), which keepslistand adds non-empty, oversetOffsetValueType(). InsetInExistingRow(),isset($matrix[$row])tracks$matrix[$row], so the write of$columninto the row wrongly usessetExistingOffsetValueType().shouldKeepList($arrayDimFetch, …)(L2694, L2731) recognises$list[array_key_last($list)],$list[count($list) - 1]and similar forms. InsetInLastRow()that pattern is matched while writing$columninto the row, so the row keepslist. InappendToLastGroup()it is checked against$groups[…][]while writing into$groups, so the outerlistis dropped.
The false-positive direction only shows up when the element type is not always an array. For list<array> values, IntersectionType::setOffsetValueType() adds list back for any integer offset (src/Type/IntersectionType.php:1154).
The same code is in 2.2.x (AssignHandler.php:2056), so it also reproduces on the playground.
Code snippet that reproduces the problem
https://phpstan.org/r/19c7bbb6-f3fe-4b8c-8a80-672f287f4a97
Expected output
appendToLastGroup():non-empty-list<non-empty-list<int>>and noreturn.typeerror.setInLastRow()andsetInExistingRow(): the rows are not typed aslist<int>, and thereturn.typeerrors are reported.
Suggested fix: use the dim fetch that is paired with the offset in $offsetTypes instead of indexing $dimFetchStack with the reversed loop index:
foreach (array_reverse($offsetTypes) as $i => [$offsetType, $arrayDimFetch]) {(or $dimFetchStack[$lastDimKey - $i]). With this change, the three dumps become non-empty-list<non-empty-list<int>>, non-empty-list<array<int, int>> and non-empty-list<array<int, int>>. The return.type error moves from appendToLastGroup() to setInLastRow() and setInExistingRow().
Did PHPStan help you today? Did it make you happy in any way?
No response
Activity
phpstan-bot commentedon Sep 17, 2026
Closed via merging phpstan/phpstan-src#6466
github-actions commentedon Oct 19, 2026
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.