Skip to content

Commit d5bc2e7

Browse files
committed
Merge branch 2.1.x into 2.2.x
2 parents 073ec3f + 0e5233a commit d5bc2e7

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1878,6 +1878,7 @@ public function processStmtNode(
18781878

18791879
// explicit only
18801880
$onlyExplicitIsThrow = true;
1881+
$hasDirectExplicitNonThrowMatch = false;
18811882
if (count($matchingThrowPoints) === 0) {
18821883
foreach ($throwPoints as $throwPointIndex => $throwPoint) {
18831884
foreach ($catchTypes as $catchTypeIndex => $catchTypeItem) {
@@ -1895,14 +1896,17 @@ public function processStmtNode(
18951896
&& !($throwNode instanceof Node\Stmt\Expression && $throwNode->expr instanceof Expr\Throw_)
18961897
) {
18971898
$onlyExplicitIsThrow = false;
1899+
if ($catchTypeItem->isSuperTypeOf($throwPoint->getType())->yes()) {
1900+
$hasDirectExplicitNonThrowMatch = true;
1901+
}
18981902
}
18991903
$matchingThrowPoints[$throwPointIndex] = $throwPoint;
19001904
}
19011905
}
19021906
}
19031907

19041908
// implicit only
1905-
if (count($matchingThrowPoints) === 0 || $onlyExplicitIsThrow) {
1909+
if (count($matchingThrowPoints) === 0 || $onlyExplicitIsThrow || !$hasDirectExplicitNonThrowMatch) {
19061910
foreach ($throwPoints as $throwPointIndex => $throwPoint) {
19071911
if ($throwPoint->isExplicit()) {
19081912
continue;

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,25 @@ public function testBug14019(): void
14031403
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-14019.php'], []);
14041404
}
14051405

1406+
public function testBug9349(): void
1407+
{
1408+
$this->cliArgumentsVariablesRegistered = true;
1409+
$this->polluteScopeWithLoopInitialAssignments = false;
1410+
$this->checkMaybeUndefinedVariables = true;
1411+
$this->polluteScopeWithAlwaysIterableForeach = true;
1412+
1413+
$this->analyse([__DIR__ . '/data/bug-9349.php'], [
1414+
[
1415+
'Variable $sql might not be defined.',
1416+
19,
1417+
],
1418+
[
1419+
'Variable $sql might not be defined.',
1420+
78,
1421+
],
1422+
]);
1423+
}
1424+
14061425
#[RequiresPhp('>= 8.0')]
14071426
public function testBug14274(): void
14081427
{
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug9349;
4+
5+
class HelloWorld
6+
{
7+
public function test(): void
8+
{
9+
global $pdo;
10+
11+
try {
12+
$this->maybeThrows();
13+
$sql = "SELECT * FROM foo";
14+
$rs = $pdo->query($sql);
15+
if ($result = $rs->fetch(\PDO::FETCH_ASSOC)) {
16+
// do something
17+
}
18+
} catch (\PDOException $e) {
19+
var_dump($sql);
20+
}
21+
}
22+
23+
/**
24+
* @throws \RuntimeException
25+
*/
26+
public function maybeThrows(): void
27+
{
28+
if (random_int(0, 1) === 1) {
29+
throw new \RuntimeException();
30+
}
31+
}
32+
33+
}
34+
35+
class HelloWorld2
36+
{
37+
public function test2(): void
38+
{
39+
global $pdo;
40+
41+
try {
42+
$this->maybeThrows2();
43+
$sql = "SELECT * FROM foo";
44+
$rs = $pdo->query($sql);
45+
if ($result = $rs->fetch(\PDO::FETCH_ASSOC)) {
46+
// do something
47+
}
48+
} catch (\PDOException $e) {
49+
var_dump($sql);
50+
}
51+
}
52+
53+
/**
54+
* @throws \LogicException
55+
*/
56+
public function maybeThrows2(): void
57+
{
58+
if (random_int(0, 1) === 1) {
59+
throw new \LogicException();
60+
}
61+
}
62+
}
63+
64+
class HelloWorld3
65+
{
66+
public function test3(): void
67+
{
68+
global $pdo;
69+
70+
try {
71+
$this->maybeThrows3();
72+
$sql = "SELECT * FROM foo";
73+
$rs = $pdo->query($sql);
74+
if ($result = $rs->fetch(\PDO::FETCH_ASSOC)) {
75+
// do something
76+
}
77+
} catch (\PDOException $e) {
78+
var_dump($sql);
79+
}
80+
}
81+
82+
/**
83+
* @throws \LogicException|\RuntimeException
84+
*/
85+
public function maybeThrows3(): void
86+
{
87+
if (random_int(0, 1) === 1) {
88+
throw new \RuntimeException();
89+
}
90+
if (random_int(0, 1) === 1) {
91+
throw new \LogicException();
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)