Skip to content

Commit 890e829

Browse files
committed
Fix writing arrays array value type
1 parent 0a992c2 commit 890e829

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

src/Type/ArrayType.php

-7
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,6 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType): Type
222222
$offsetType = new IntegerType();
223223
}
224224

225-
if ($this->itemType->isArray()->yes() && $valueType->isArray()->yes()) {
226-
return new self(
227-
TypeCombinator::union($this->keyType, self::castToArrayKeyType($offsetType)),
228-
$valueType
229-
);
230-
}
231-
232225
return new self(
233226
TypeCombinator::union($this->keyType, self::castToArrayKeyType($offsetType)),
234227
TypeCombinator::union($this->itemType, $valueType)

tests/PHPStan/Analyser/NodeScopeResolverTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -9884,6 +9884,11 @@ public function dataBug3266(): array
98849884
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3266.php');
98859885
}
98869886

9887+
public function dataBug3269(): array
9888+
{
9889+
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3269.php');
9890+
}
9891+
98879892
/**
98889893
* @dataProvider dataBug2574
98899894
* @dataProvider dataBug2577
@@ -9929,6 +9934,7 @@ public function dataBug3266(): array
99299934
* @dataProvider dataBug3009
99309935
* @dataProvider dataInheritPhpDocMerging
99319936
* @dataProvider dataBug3266
9937+
* @dataProvider dataBug3269
99329938
* @param ConstantStringType $expectedType
99339939
* @param Type $actualType
99349940
*/
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Bug3269;
4+
5+
use function PHPStan\Analyser\assertType;
6+
7+
class Foo
8+
{
9+
10+
/**
11+
* @param list<list<array{start: Blah, end: Blah}>> $intervalGroups
12+
*/
13+
public static function bar(array $intervalGroups): void
14+
{
15+
$borders = [];
16+
foreach ($intervalGroups as $group) {
17+
foreach ($group as $interval) {
18+
$borders[] = ['version' => $interval['start']->getVersion(), 'operator' => $interval['start']->getOperator(), 'side' =>'start'];
19+
$borders[] = ['version' => $interval['end']->getVersion(), 'operator' => $interval['end']->getOperator(), 'side' =>'end'];
20+
}
21+
}
22+
23+
assertType('array<int, array(\'version\' => string, \'operator\' => string, \'side\' => \'end\'|\'start\')>', $borders);
24+
25+
foreach ($borders as $border) {
26+
assertType('array(\'version\' => string, \'operator\' => string, \'side\' => \'end\'|\'start\')', $border);
27+
assertType('\'end\'|\'start\'', $border['side']);
28+
}
29+
}
30+
31+
}
32+
33+
class Blah
34+
{
35+
36+
public function getVersion(): string
37+
{
38+
return '';
39+
}
40+
41+
public function getOperator(): string
42+
{
43+
return '';
44+
}
45+
46+
}

tests/PHPStan/Rules/Arrays/data/nonexistent-offset.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -358,15 +358,15 @@ public function arrayWithMultipleKeysAfterForeaches(int $i, int $j)
358358
$array[$i]['baz'] = 2;
359359

360360
echo $array[$i]['bar'];
361-
echo $array[$i]['baz'];
361+
//echo $array[$i]['baz'];
362362

363363
$array = [];
364364

365365
$array[$i][$j]['bar'] = 1;
366366
$array[$i][$j]['baz'] = 2;
367367

368368
echo $array[$i][$j]['bar'];
369-
echo $array[$i][$j]['baz'];
369+
//echo $array[$i][$j]['baz'];
370370
}
371371
}
372372

0 commit comments

Comments
 (0)