Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions src/Analyser/ExprHandler/AssignHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,9 @@ private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, ar
$offsetValueTypeStack[] = $offsetValueType;
}

foreach (array_reverse($offsetTypes) as $i => [$offsetType]) {
$reversedOffsetTypes = array_reverse($offsetTypes);
$lastOffsetIndex = count($reversedOffsetTypes) - 1;
foreach ($reversedOffsetTypes as $i => [$offsetType]) {
/** @var Type $offsetValueType */
$offsetValueType = array_pop($offsetValueTypeStack);
if (
Expand Down Expand Up @@ -980,7 +982,11 @@ private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, ar
}

} else {
$valueToWrite = $offsetValueType->setOffsetValueType($offsetType, $valueToWrite, $i === 0);
$unionValues = $i === 0;
if (!$unionValues && $i === $lastOffsetIndex && $offsetType !== null) {
$unionValues = $this->shouldUnionExistingItemType($offsetValueType, $valueToWrite);
}
$valueToWrite = $offsetValueType->setOffsetValueType($offsetType, $valueToWrite, $unionValues);
}

if ($arrayDimFetch === null || !$offsetValueType->isList()->yes()) {
Expand Down Expand Up @@ -1029,4 +1035,34 @@ private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, ar
return [$valueToWrite, $additionalExpressions];
}

/**
* When modifying a nested array dimension with a non-constant key,
* check if the composed value changes any existing constant-array
* key values. If it does, the existing item type should be unioned
* because unmodified elements still have their original types.
*/
private function shouldUnionExistingItemType(Type $offsetValueType, Type $composedValue): bool
{
$existingItemType = $offsetValueType->getIterableValueType();

if (!$existingItemType->isConstantArray()->yes() || !$composedValue->isConstantArray()->yes()) {
return false;
}

foreach ($existingItemType->getConstantArrays() as $existingArray) {
foreach ($existingArray->getKeyTypes() as $i => $keyType) {
$existingValue = $existingArray->getValueTypes()[$i];
if ($composedValue->hasOffsetValueType($keyType)->no()) {
continue;
}
$newValue = $composedValue->getOffsetValueType($keyType);
if (!$newValue->isSuperTypeOf($existingValue)->yes()) {
return true;
}
}
}

return false;
}

}
47 changes: 47 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-8270.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types = 1);

namespace Bug8270;

use function PHPStan\Testing\assertType;

function () {
/** @var non-empty-list<array{test: false, value: int}> $list */
$list = [];
$list[0]['test'] = true;

foreach ($list as $item) {
assertType('array{test: bool, value: int}', $item);
if ($item['test']) {
assertType('true', $item['test']);
echo $item['value'];
}
}
};

function () {
$list = [];

for ($i = 0; $i < 10; $i++) {
$list[] = [
'test' => false,
'value' => rand(),
];
}

if ($list === []) {
return;
}

$k = array_key_first($list);
assertType('int<0, max>', $k);
$list[$k]['test'] = true;

foreach ($list as $item) {
assertType('array{test: bool, value: int<0, max>}', $item);
if ($item['test']) {
echo $item['value'];
}
}
};
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Arrays/data/bug-11679.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function sayHello(int $index): bool
assertType('array<int, array{foo?: bool}>', $this->arr);
if (!isset($this->arr[$index]['foo'])) {
$this->arr[$index]['foo'] = true;
assertType('non-empty-array<int, array{foo: true}>', $this->arr);
assertType('non-empty-array<int, array{foo?: bool}>', $this->arr);
}
assertType('array<int, array{foo?: bool}>', $this->arr);
return $this->arr[$index]['foo']; // PHPStan does not realize 'foo' is set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function doFoo(array $percentageIntervals, array $changes): void
assertType('non-empty-array<array{itemsCount: mixed, interval: mixed}>', $intervalResults);
assertType('array{itemsCount: mixed, interval: mixed}', $intervalResults[$key]);
$intervalResults[$key]['itemsCount'] += $itemsCount;
assertType('non-empty-array<array{itemsCount: (array|float|int), interval: mixed}>', $intervalResults);
assertType('non-empty-array<array{itemsCount: mixed, interval: mixed}>', $intervalResults);
assertType('array{itemsCount: (array|float|int), interval: mixed}', $intervalResults[$key]);
} else {
assertType('array<array{itemsCount: mixed, interval: mixed}>', $intervalResults);
Expand Down