Skip to content

Commit b435be3

Browse files
committed
more tests
1 parent 1776b40 commit b435be3

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/Analyser/ExprHandler/AssignHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,10 +1106,10 @@ private function shouldUnionExistingItemType(Type $offsetValueType, Type $compos
11061106

11071107
foreach ($existingItemType->getConstantArrays() as $existingArray) {
11081108
foreach ($existingArray->getKeyTypes() as $i => $keyType) {
1109-
$existingValue = $existingArray->getValueTypes()[$i];
11101109
if ($composedValue->hasOffsetValueType($keyType)->no()) {
11111110
continue;
11121111
}
1112+
$existingValue = $existingArray->getValueTypes()[$i];
11131113
$newValue = $composedValue->getOffsetValueType($keyType);
11141114
if (!$newValue->isSuperTypeOf($existingValue)->yes()) {
11151115
return true;

tests/PHPStan/Analyser/nsrt/bug-13857.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,30 @@ function test(array $array, int $id): void {
1414
// only one element was set to 'foo', not all of them.
1515
assertType("non-empty-array<int, array{state: string}>", $array);
1616
}
17+
18+
/**
19+
* @param array<int, array{state?: string}> $array
20+
*/
21+
function testMaybe(array $array, int $id): void {
22+
$array[$id]['state'] = 'foo';
23+
// only one element was set to 'foo', not all of them.
24+
assertType("non-empty-array<int, array{state?: string}>", $array);
25+
}
26+
27+
/**
28+
* @param array<int, array{state: string|bool}> $array
29+
*/
30+
function testUnionValue(array $array, int $id): void {
31+
$array[$id]['state'] = 'foo';
32+
// only one element was set to 'foo', not all of them.
33+
assertType("non-empty-array<int, array{state: bool|string}>", $array);
34+
}
35+
36+
/**
37+
* @param array<int, array{state: string}|array{foo: int}> $array
38+
*/
39+
function testUnionArray(array $array, int $id): void {
40+
$array[$id]['state'] = 'foo';
41+
// only one element was set to 'foo', not all of them.
42+
assertType("non-empty-array<int, non-empty-array{foo?: int, state?: string}>", $array);
43+
}

0 commit comments

Comments
 (0)