Skip to content

Commit 09a7355

Browse files
authored
Added regression test
1 parent 23dc4e7 commit 09a7355

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,13 @@ public function testBug4532(): void
990990
]);
991991
}
992992

993+
public function testBug12115(): void
994+
{
995+
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
996+
997+
$this->analyse([__DIR__ . '/data/bug-12115.php'], []);
998+
}
999+
9931000
public function testBug10492(): void
9941001
{
9951002
$this->analyse([__DIR__ . '/data/bug-10492.php'], [
@@ -1000,6 +1007,20 @@ public function testBug10492(): void
10001007
]);
10011008
}
10021009

1010+
public function testBug12574a(): void
1011+
{
1012+
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
1013+
1014+
$this->analyse([__DIR__ . '/data/bug-12574a.php'], []);
1015+
}
1016+
1017+
public function testBug12574b(): void
1018+
{
1019+
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
1020+
1021+
$this->analyse([__DIR__ . '/data/bug-12574b.php'], []);
1022+
}
1023+
10031024
public function testBug12926(): void
10041025
{
10051026
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug12115;
4+
5+
class HelloWorld
6+
{
7+
public function testMe(): void
8+
{
9+
$indexes = [1, 2, 3, 4, 5];
10+
$loopArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
11+
$counterArray = [];
12+
13+
14+
foreach ($indexes as $index) {
15+
// Check if the index is not set in the counter array
16+
if (!isset($counterArray[$index])) {
17+
$counterArray[$index] = ['string_index_1' => 0, 'string_index_2' => 0];
18+
}
19+
20+
foreach ($loopArray as $loop) {
21+
$stringIndex = $loop % 2 === 0 ? 'string_index_1' : 'string_index_2';
22+
$counterArray[$index][$stringIndex]++;
23+
}
24+
}
25+
26+
}
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug12574a;
4+
5+
function doFoo() {
6+
$i = 10;
7+
$x = [];
8+
while ($i-- > 0) {
9+
$r = rand(1, 10);
10+
if (!isset($x[$r])) {
11+
$x[$r] = 1;
12+
}
13+
var_dump($x[$r]);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12574b;
4+
5+
class HelloWorld
6+
{
7+
public function sayHello2(string $a): void
8+
{
9+
$b = [];
10+
11+
$b[$a] = 'abc';
12+
13+
echo $b[$a];
14+
}
15+
}

0 commit comments

Comments
 (0)