Skip to content

Commit c06bad0

Browse files
Add reproducer for 13984
1 parent a44d54e commit c06bad0

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

tests/PHPStan/Rules/Comparison/NumberComparisonOperatorsConstantConditionRuleTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class NumberComparisonOperatorsConstantConditionRuleTest extends RuleTestCase
1515

1616
private bool $treatPhpDocTypesAsCertain = true;
1717

18+
private bool $polluteScopeWithAlwaysIterableForeach = true;
19+
1820
protected function getRule(): Rule
1921
{
2022
return new NumberComparisonOperatorsConstantConditionRule(
@@ -23,6 +25,11 @@ protected function getRule(): Rule
2325
);
2426
}
2527

28+
protected function shouldPolluteScopeWithAlwaysIterableForeach(): bool
29+
{
30+
return $this->polluteScopeWithAlwaysIterableForeach;
31+
}
32+
2633
public function testBug8277(): void
2734
{
2835
$this->analyse([__DIR__ . '/data/bug-8277.php'], []);
@@ -269,6 +276,12 @@ public function testBug3387(): void
269276
$this->analyse([__DIR__ . '/data/bug-3387.php'], []);
270277
}
271278

279+
public function testBug13984(): void
280+
{
281+
$this->polluteScopeWithAlwaysIterableForeach = false;
282+
$this->analyse([__DIR__ . '/data/bug-13984.php'], []);
283+
}
284+
272285
public function testBug13874(): void
273286
{
274287
$this->analyse([__DIR__ . '/data/bug-13874.php'], []);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug13984;
6+
7+
$list = [
8+
'a',
9+
'b',
10+
'c',
11+
];
12+
13+
/** @param list<string> $list */
14+
function acceptList(array $list): bool {
15+
if (count($list) < 1) {
16+
return false;
17+
}
18+
19+
$compare = ['a', 'b', 'c'];
20+
21+
foreach($list as $key => $item) {
22+
foreach ($compare as $k => $v) {
23+
if ($item === $v && $v !== 'a') {
24+
unset($list[$key]);
25+
}
26+
}
27+
}
28+
29+
if (count($list) > 0) {
30+
return true;
31+
}
32+
33+
return false;
34+
}
35+
36+
assert(acceptList($list) === true);

0 commit comments

Comments
 (0)