Skip to content

Commit 3c170f7

Browse files
phpstan-botVincentLanglet
authored andcommitted
Add regression tests for #11545, #10245, #5919, #5477
Closes phpstan/phpstan#11545 Closes phpstan/phpstan#10245 Closes phpstan/phpstan#5919 Closes phpstan/phpstan#5477
1 parent 4ddc2b1 commit 3c170f7

File tree

5 files changed

+196
-0
lines changed

5 files changed

+196
-0
lines changed

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

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

1280+
public function testBug11545(): void
1281+
{
1282+
$this->cliArgumentsVariablesRegistered = true;
1283+
$this->polluteScopeWithLoopInitialAssignments = false;
1284+
$this->checkMaybeUndefinedVariables = true;
1285+
$this->polluteScopeWithAlwaysIterableForeach = true;
1286+
1287+
$this->analyse([__DIR__ . '/data/bug-11545.php'], [
1288+
[
1289+
'Variable $result might not be defined.',
1290+
24,
1291+
],
1292+
[
1293+
'Variable $result might not be defined.',
1294+
36,
1295+
],
1296+
]);
1297+
}
1298+
1299+
public function testBug10245(): void
1300+
{
1301+
$this->cliArgumentsVariablesRegistered = true;
1302+
$this->polluteScopeWithLoopInitialAssignments = false;
1303+
$this->checkMaybeUndefinedVariables = true;
1304+
$this->polluteScopeWithAlwaysIterableForeach = true;
1305+
1306+
$this->analyse([__DIR__ . '/data/bug-10245.php'], []);
1307+
}
1308+
1309+
public function testBug5919(): void
1310+
{
1311+
$this->cliArgumentsVariablesRegistered = true;
1312+
$this->polluteScopeWithLoopInitialAssignments = false;
1313+
$this->checkMaybeUndefinedVariables = true;
1314+
$this->polluteScopeWithAlwaysIterableForeach = true;
1315+
1316+
$this->analyse([__DIR__ . '/data/bug-5919.php'], []);
1317+
}
1318+
1319+
public function testBug5477(): void
1320+
{
1321+
$this->cliArgumentsVariablesRegistered = true;
1322+
$this->polluteScopeWithLoopInitialAssignments = false;
1323+
$this->checkMaybeUndefinedVariables = true;
1324+
$this->polluteScopeWithAlwaysIterableForeach = true;
1325+
1326+
$this->analyse([__DIR__ . '/data/bug-5477.php'], []);
1327+
}
1328+
12801329
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types = 1); // lint >= 8.0
2+
3+
namespace Bug10245;
4+
5+
/**
6+
* @throws \Exception
7+
*/
8+
function produceInt(): int
9+
{
10+
return 1;
11+
}
12+
13+
function testTryCatchInWhileTrue(): void
14+
{
15+
while (true) {
16+
try {
17+
$a = produceInt();
18+
break;
19+
} catch (\Throwable) {}
20+
}
21+
22+
echo $a;
23+
}
24+
25+
function testIfBreakInWhileTrue(int $max): void
26+
{
27+
$i = 0;
28+
while (true) {
29+
if ($i > $max) {
30+
$result = 'done';
31+
break;
32+
}
33+
++$i;
34+
}
35+
print $result;
36+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug11545;
4+
5+
function foo(int $max): void {
6+
$i = 0;
7+
while (true) {
8+
if ($i > $max) {
9+
$result = 'done';
10+
break;
11+
}
12+
++$i;
13+
}
14+
print $result;
15+
}
16+
17+
function foo_for(int $max): void {
18+
for ($i = 0;; ++$i) {
19+
if ($i > $max) {
20+
$result = 'done';
21+
break;
22+
}
23+
}
24+
print $result;
25+
}
26+
27+
function foo_do_while(int $max): void {
28+
$i = 0;
29+
do {
30+
if ($i > $max) {
31+
$result = 'done';
32+
break;
33+
}
34+
++$i;
35+
} while (true);
36+
print $result;
37+
}
38+
39+
function bar(int $max): void {
40+
while (true) {
41+
$result = 'done';
42+
break;
43+
}
44+
print $result;
45+
}
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 Bug5477;
4+
5+
function foo(): int {
6+
while (true) {
7+
try {
8+
$transaction = 1;
9+
break;
10+
} catch (\Throwable $e) {
11+
continue;
12+
}
13+
}
14+
15+
return $transaction;
16+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bug5919;
6+
7+
/**
8+
* @return mixed[]
9+
*/
10+
function queryApi(): array
11+
{
12+
return [5];
13+
}
14+
15+
function isTimeout(): bool
16+
{
17+
return true;
18+
}
19+
20+
function testSimple(): void
21+
{
22+
while (true) {
23+
try {
24+
$s = queryApi();
25+
break;
26+
} catch (\Exception $e) {
27+
if (false) {
28+
throw $e;
29+
}
30+
}
31+
}
32+
33+
var_dump(count($s));
34+
}
35+
36+
function testWithIsTimeout(): void
37+
{
38+
while (true) {
39+
try {
40+
$s = queryApi();
41+
break;
42+
} catch (\Exception $e) {
43+
if (isTimeout()) {
44+
throw $e;
45+
}
46+
}
47+
}
48+
49+
var_dump(count($s));
50+
}

0 commit comments

Comments
 (0)