Skip to content

Commit a3f2431

Browse files
committed
Added failling test
while looking into `isset($arr[$i])` I realized that we have this untested case in `array_kex_exists`
1 parent 3b8c06e commit a3f2431

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,16 @@ public function testBug7000(): void
406406
]);
407407
}
408408

409+
public function testBug7000b(): void
410+
{
411+
$this->analyse([__DIR__ . '/data/bug-7000b.php'], [
412+
[
413+
"Offset 'require'|'require-dev' might not exist on array{require?: array<string, string>, require-dev?: array<string, string>}.",
414+
18,
415+
],
416+
]);
417+
}
418+
409419
public function testBug6508(): void
410420
{
411421
$this->analyse([__DIR__ . '/data/bug-6508.php'], []);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug7000b;
4+
5+
use function array_key_exists;
6+
7+
class Foo
8+
{
9+
public function doBar(): void
10+
{
11+
/** @var array{require?: array<string, string>, require-dev?: array<string, string>} $composer */
12+
$composer = array();
13+
/** @var 'require'|'require-dev' $foo */
14+
$foo = '';
15+
foreach (array('require', 'require-dev') as $linkType) {
16+
if (array_key_exists($linkType, $composer)) {
17+
foreach ($composer[$linkType] as $x) {} // should not report error
18+
foreach ($composer[$foo] as $x) {} // should report error. It can be $linkType = 'require', $foo = 'require-dev'
19+
}
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)