Skip to content

Commit 04a99c1

Browse files
Add regression test for @var annotation before foreach not defining the expression variable (#5458)
Co-authored-by: ondrejmirtes <104888+ondrejmirtes@users.noreply.github.com>
1 parent 03834ec commit 04a99c1

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,4 +1517,18 @@ public function testBug11218(): void
15171517
$this->analyse([__DIR__ . '/data/bug-11218.php'], []);
15181518
}
15191519

1520+
public function testBug6688(): void
1521+
{
1522+
$this->cliArgumentsVariablesRegistered = true;
1523+
$this->polluteScopeWithLoopInitialAssignments = false;
1524+
$this->checkMaybeUndefinedVariables = true;
1525+
$this->polluteScopeWithAlwaysIterableForeach = true;
1526+
$this->analyse([__DIR__ . '/data/bug-6688.php'], [
1527+
[
1528+
'Variable $a might not be defined.',
1529+
5,
1530+
],
1531+
]);
1532+
}
1533+
15201534
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug6688;
4+
5+
echo $a; // possibly undefined
6+
7+
/** @var string $b */
8+
echo $b; // willed into existence
9+
10+
/** @var string[] $c */
11+
foreach ($c as $v) { // willed into existence
12+
}
13+
14+
/** @var string[][] $result */
15+
foreach ($result as $data) {
16+
echo '"' . implode('", "', $data) . '"' . "\n";
17+
}

0 commit comments

Comments
 (0)