Skip to content

Commit 638c010

Browse files
phpstan-botclaude
andcommitted
Add rule test for bug-13705 to verify no false positive in_array errors
Adds a rule test for ImpossibleCheckTypeFunctionCallRule that verifies in_array() inside while loops with count comparisons no longer produces false "will always evaluate to false" errors. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1b549b9 commit 638c010

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,12 @@ public function testBug14429(): void
12021202
$this->analyse([__DIR__ . '/data/bug-14429.php'], []);
12031203
}
12041204

1205+
public function testBug13705(): void
1206+
{
1207+
$this->treatPhpDocTypesAsCertain = true;
1208+
$this->analyse([__DIR__ . '/data/bug-13705.php'], []);
1209+
}
1210+
12051211
public function testBug13799(): void
12061212
{
12071213
$this->treatPhpDocTypesAsCertain = true;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13705Rule;
4+
5+
function whileLoopWithInArray(): void
6+
{
7+
$quantity = random_int(1, 42);
8+
$codes = [];
9+
while (count($codes) < $quantity) {
10+
$code = random_bytes(16);
11+
if (!in_array($code, $codes, true)) {
12+
$codes[] = $code;
13+
}
14+
}
15+
}
16+
17+
function whileLoopOriginal(int $length, int $quantity): void
18+
{
19+
if ($length < 8) {
20+
throw new \InvalidArgumentException();
21+
}
22+
$codes = [];
23+
while ($quantity >= 1 && count($codes) < $quantity) {
24+
$code = '';
25+
for ($i = 0; $i < $length; $i++) {
26+
$code .= 'x';
27+
}
28+
if (!in_array($code, $codes, true)) {
29+
$codes[] = $code;
30+
}
31+
}
32+
}
33+
34+
class HelloWorld
35+
{
36+
private const MIN_LENGTH = 8;
37+
38+
/**
39+
* @return list<non-empty-string>
40+
*/
41+
public function generatePlainRecoveryCodes(int $length = 8, int $quantity = 8): array
42+
{
43+
if ($length < self::MIN_LENGTH) {
44+
throw new \InvalidArgumentException(
45+
$length . ' is not allowed as length for recovery codes. Must be at least ' . self::MIN_LENGTH,
46+
1613666803
47+
);
48+
}
49+
$codes = [];
50+
while ($quantity >= 1 && count($codes) < $quantity) {
51+
$code = '';
52+
for ($i = 0; $i < $length; $i++) {
53+
$code .= 'x';
54+
}
55+
if (!in_array($code, $codes, true)) {
56+
$codes[] = $code;
57+
}
58+
}
59+
return $codes;
60+
}
61+
}

0 commit comments

Comments
 (0)