Skip to content

Commit b788e4e

Browse files
phpstan-botclaude
andcommitted
Add rule test for phpstan/phpstan#6822 to prove no false-positive rule error
The nsrt test only verified type inference. This adds an IfConstantConditionRule test to prove that impure closures don't cause false "If condition is always true" errors when used in nested conditions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 93e1e21 commit b788e4e

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

tests/PHPStan/Rules/Comparison/IfConstantConditionRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,10 @@ public function testBug4284(): void
213213
]);
214214
}
215215

216+
public function testBug6822(): void
217+
{
218+
$this->treatPhpDocTypesAsCertain = true;
219+
$this->analyse([__DIR__ . '/data/bug-6822.php'], []);
220+
}
221+
216222
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug6822Rule;
4+
5+
/** @phpstan-impure */
6+
$closure = function (): bool {
7+
return (bool) rand(0, 1);
8+
};
9+
10+
if ($closure()) {
11+
if ($closure()) { // should not be reported as "always true"
12+
echo 'yes';
13+
}
14+
}
15+
16+
/** @phpstan-impure */
17+
$impureFn = function (): int {
18+
return rand(0, 100);
19+
};
20+
21+
if ($impureFn() > 50) {
22+
if ($impureFn() > 50) { // should not be reported as "always true"
23+
echo 'yes';
24+
}
25+
}

0 commit comments

Comments
 (0)