Skip to content

Commit 0ca5284

Browse files
phpstan-botclaude
andcommitted
Add non-regression tests for #11218
- DefinedVariableRuleTest: ensure no "Variable $test might not be defined" error in for loop where variable is always defined on first iteration - NonexistentOffsetInArrayDimFetchRuleTest: ensure no "Offset 'test' does not exist" error when array offset is always set on first iteration Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent eaa4d5b commit 0ca5284

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,4 +1277,11 @@ public function testBug14308(): void
12771277
$this->analyse([__DIR__ . '/data/bug-14308.php'], []);
12781278
}
12791279

1280+
public function testBug11218(): void
1281+
{
1282+
$this->reportPossiblyNonexistentConstantArrayOffset = true;
1283+
1284+
$this->analyse([__DIR__ . '/data/bug-11218.php'], []);
1285+
}
1286+
12801287
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug11218Array;
4+
5+
function test(): void
6+
{
7+
$level = 'test';
8+
9+
$test = [];
10+
11+
for ($i = 0 ; $i <= 3 ; $i++) {
12+
if ($i === 0) {
13+
$test[$level] = 'this is a';
14+
} else {
15+
$test[$level] .= ' test';
16+
}
17+
}
18+
}

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,4 +1508,13 @@ public function testBug12597(): void
15081508
$this->analyse([__DIR__ . '/data/bug-12597.php'], []);
15091509
}
15101510

1511+
public function testBug11218(): void
1512+
{
1513+
$this->cliArgumentsVariablesRegistered = true;
1514+
$this->polluteScopeWithLoopInitialAssignments = false;
1515+
$this->checkMaybeUndefinedVariables = true;
1516+
$this->polluteScopeWithAlwaysIterableForeach = true;
1517+
$this->analyse([__DIR__ . '/data/bug-11218.php'], []);
1518+
}
1519+
15111520
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug11218;
4+
5+
function test(): void
6+
{
7+
$level = 'test';
8+
9+
for ($i = 0 ; $i <= 3 ; $i++) {
10+
if ($i === 0) {
11+
$test[$level] = 'this is a';
12+
} else {
13+
$test[$level] .= ' test';
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)