Skip to content

Commit fe05863

Browse files
phpstan-botclaude
andcommitted
Add regression tests for phpstan/phpstan#11284 and phpstan/phpstan#7806
Both issues involve variables in try/catch blocks not being properly tracked - #11284 for by-reference parameters and #7806 for preg_match matches. These are also fixed by the throw point scope fix in #5240. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 6097052 commit fe05863

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,26 @@ public function testBug14318(): void
14321432
$this->analyse([__DIR__ . '/data/bug-14318.php'], []);
14331433
}
14341434

1435+
public function testBug11284(): void
1436+
{
1437+
$this->cliArgumentsVariablesRegistered = true;
1438+
$this->polluteScopeWithLoopInitialAssignments = false;
1439+
$this->checkMaybeUndefinedVariables = true;
1440+
$this->polluteScopeWithAlwaysIterableForeach = true;
1441+
1442+
$this->analyse([__DIR__ . '/data/bug-11284.php'], []);
1443+
}
1444+
1445+
public function testBug7806(): void
1446+
{
1447+
$this->cliArgumentsVariablesRegistered = true;
1448+
$this->polluteScopeWithLoopInitialAssignments = false;
1449+
$this->checkMaybeUndefinedVariables = true;
1450+
$this->polluteScopeWithAlwaysIterableForeach = true;
1451+
1452+
$this->analyse([__DIR__ . '/data/bug-7806.php'], []);
1453+
}
1454+
14351455
#[RequiresPhp('>= 8.0')]
14361456
public function testBug14274(): void
14371457
{
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug11284;
4+
5+
class HelloWorld
6+
{
7+
8+
/**
9+
* @param array<string> $err
10+
* @throws \RuntimeException
11+
*/
12+
public function maybeThrows(array &$err): void
13+
{
14+
$err[] = 'error';
15+
if (random_int(0, 1) === 1) {
16+
throw new \RuntimeException();
17+
}
18+
}
19+
20+
public function test(): void
21+
{
22+
$err = [];
23+
try {
24+
$this->maybeThrows($err);
25+
} catch (\RuntimeException $e) {
26+
if (!empty($err)) {
27+
echo implode(', ', $err);
28+
}
29+
}
30+
}
31+
32+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug7806;
4+
5+
class HelloWorld
6+
{
7+
8+
public function test(): void
9+
{
10+
try {
11+
preg_match('/pattern/', 'subject', $reasons);
12+
} catch (\Throwable $e) {
13+
if (!empty($reasons)) {
14+
echo implode(', ', $reasons);
15+
}
16+
}
17+
}
18+
19+
}

0 commit comments

Comments
 (0)