Skip to content

Commit a0cd5cd

Browse files
committed
test overwriting elements
1 parent d1ebed8 commit a0cd5cd

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/Type/Accessory/AccessoryArrayListType.php

+7
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
151151
return $this;
152152
}
153153

154+
if (
155+
$valueType->isArray()->yes()
156+
&& IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($offsetType)->yes()
157+
) {
158+
return $this;
159+
}
160+
154161
return new ErrorType();
155162
}
156163

tests/PHPStan/Rules/Methods/data/bug-12927.php

+30-3
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,42 @@ public function sayHello(array $list): array
2222

2323
/**
2424
* @param list<array<string, string>> $list
25-
* @return list<array<string>>
2625
*/
27-
public function sayFoo(array $list): array
26+
public function sayFoo(array $list): void
2827
{
2928
foreach($list as $k => $v) {
3029
unset($list[$k]['abc']);
3130
assertType('non-empty-list<array<string, string>>', $list);
3231
assertType('array<string, string>', $list[$k]);
3332
}
34-
return $list;
33+
assertType('list<array<string, string>>', $list);
34+
}
35+
36+
/**
37+
* @param list<array<string, string>> $list
38+
*/
39+
public function sayFoo2(array $list): void
40+
{
41+
foreach($list as $k => $v) {
42+
$list[$k]['abc'] = 'world';
43+
assertType("non-empty-list<non-empty-array<string, string>&hasOffsetValue('abc', 'world')>", $list);
44+
assertType("non-empty-array<string, string>&hasOffsetValue('abc', 'world')", $list[$k]);
45+
}
46+
assertType("list<non-empty-array<string, string>&hasOffsetValue('abc', 'world')>", $list);
47+
}
48+
49+
/**
50+
* @param list<array<string, string>> $list
51+
*/
52+
public function sayFooBar(array $list): void
53+
{
54+
foreach($list as $k => $v) {
55+
if (rand(0,1)) {
56+
unset($list[$k]);
57+
}
58+
assertType('array<int<0, max>, array<string, string>>', $list);
59+
assertType('array<string, string>', $list[$k]);
60+
}
61+
assertType('array<string, string>', $list[$k]);
3562
}
3663
}

0 commit comments

Comments
 (0)