Skip to content

Commit a585526

Browse files
committed
assert types
1 parent 2b089b1 commit a585526

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ private static function findTestFiles(): iterable
9898
yield __DIR__ . '/../Rules/Generics/data/bug-3769.php';
9999
yield __DIR__ . '/../Rules/Generics/data/bug-6301.php';
100100
yield __DIR__ . '/../Rules/PhpDoc/data/bug-4643.php';
101+
yield __DIR__ . '/../Rules/Arrays/data/bug-13538.php';
101102

102103
if (PHP_VERSION_ID >= 80000) {
103104
yield __DIR__ . '/../Rules/Comparison/data/bug-4857.php';

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,36 @@ public function testBug13538(): void
10051005
$this->reportPossiblyNonexistentConstantArrayOffset = true;
10061006
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
10071007

1008-
$this->analyse([__DIR__ . '/data/bug-13538.php'], []);
1008+
$this->analyse([__DIR__ . '/data/bug-13538.php'], [
1009+
[
1010+
"Offset int might not exist on non-empty-array<int, ''>.",
1011+
13,
1012+
],
1013+
[
1014+
"Offset int might not exist on non-empty-array<int, ''>.",
1015+
17,
1016+
],
1017+
[
1018+
"Offset int might not exist on non-empty-array<int, ''>.",
1019+
21,
1020+
],
1021+
[
1022+
"Offset int might not exist on non-empty-array<int, ''>.",
1023+
25,
1024+
],
1025+
[
1026+
"Offset int might not exist on non-empty-array<int, ''>.",
1027+
41,
1028+
],
1029+
[
1030+
"Offset int might not exist on non-empty-array<int, ''>.",
1031+
45,
1032+
],
1033+
[
1034+
"Offset int might not exist on non-empty-array<int, ''>.",
1035+
49,
1036+
],
1037+
]);
10091038
}
10101039

10111040
}

tests/PHPStan/Rules/Arrays/data/bug-13538.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,64 @@
33
namespace Bug13538;
44

55
use LogicException;
6+
use function PHPStan\Testing\assertType;
67

78
/** @param list<string> $arr */
8-
function doFoo(array $arr, string $s): void
9+
function doFoo(array $arr, int $i, int $i2): void
910
{
1011
$logs = [];
11-
$logs[$s] = '';
12+
$logs[$i] = '';
13+
echo $logs[$i2];
14+
15+
assertType("non-empty-array<int, ''>", $logs);
16+
assertType("''", $logs[$i]);
17+
assertType("string", $logs[$i2]);
18+
1219
foreach ($arr as $value) {
13-
echo $logs[$s];
20+
echo $logs[$i];
21+
echo $logs[$i2];
22+
23+
assertType("non-empty-array<int, ''>", $logs);
24+
assertType("''", $logs[$i]);
25+
assertType("string", $logs[$i2]);
1426
}
1527
}
1628

1729
/** @param list<string> $arr */
18-
function doFooBar(array $arr): void
30+
function doFooBar(array $arr, int $i): void
1931
{
2032
if (!defined('LOG_DIR')) {
2133
throw new LogicException();
2234
}
2335

2436
$logs = [];
2537
$logs[LOG_DIR] = '';
38+
39+
assertType("non-empty-array<''>", $logs);
40+
assertType("''", $logs[LOG_DIR]);
41+
assertType("string", $logs[$i]);
42+
2643
foreach ($arr as $value) {
2744
echo $logs[LOG_DIR];
45+
echo $logs[$i];
46+
47+
assertType("non-empty-array<''>", $logs);
48+
assertType("''", $logs[LOG_DIR]);
49+
assertType("string", $logs[$i]);
2850
}
2951
}
3052

3153
function doBar(array $arr, int $i, string $s): void
3254
{
3355
$logs = [];
3456
$logs[$i][$s] = '';
57+
assertType("non-empty-array<int, non-empty-array<string, ''>>", $logs);
58+
assertType("non-empty-array<string, ''>", $logs[$i]);
59+
assertType("''", $logs[$i][$s]);
3560
foreach ($arr as $value) {
61+
assertType("non-empty-array<int, non-empty-array<string, ''>>", $logs);
62+
assertType("non-empty-array<string, ''>", $logs[$i]);
63+
assertType("''", $logs[$i][$s]);
3664
echo $logs[$i][$s];
3765
}
3866
}

0 commit comments

Comments
 (0)