Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,7 @@ public function processStmtNode(

// explicit only
$onlyExplicitIsThrow = true;
$hasDirectExplicitNonThrowMatch = false;
if (count($matchingThrowPoints) === 0) {
foreach ($throwPoints as $throwPointIndex => $throwPoint) {
foreach ($catchTypes as $catchTypeIndex => $catchTypeItem) {
Expand All @@ -1895,14 +1896,17 @@ public function processStmtNode(
&& !($throwNode instanceof Node\Stmt\Expression && $throwNode->expr instanceof Expr\Throw_)
) {
$onlyExplicitIsThrow = false;
if ($catchTypeItem->isSuperTypeOf($throwPoint->getType())->yes()) {
$hasDirectExplicitNonThrowMatch = true;
}
}
$matchingThrowPoints[$throwPointIndex] = $throwPoint;
}
}
}

// implicit only
if (count($matchingThrowPoints) === 0 || $onlyExplicitIsThrow) {
if (count($matchingThrowPoints) === 0 || $onlyExplicitIsThrow || !$hasDirectExplicitNonThrowMatch) {
foreach ($throwPoints as $throwPointIndex => $throwPoint) {
if ($throwPoint->isExplicit()) {
continue;
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,21 @@ public function testBug14019(): void
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-14019.php'], []);
}

public function testBug9349(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;

$this->analyse([__DIR__ . '/data/bug-9349.php'], [
[
'Variable $sql might not be defined.',
19,
],
]);
}

#[RequiresPhp('>= 8.0')]
public function testBug14274(): void
{
Expand Down
58 changes: 58 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-9349.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php declare(strict_types = 1);

namespace Bug9349;

class HelloWorld
{
public function test(): void
{
global $pdo;

try {
$this->maybeThrows();
$sql = "SELECT * FROM foo";
$rs = $pdo->query($sql);
if ($result = $rs->fetch(\PDO::FETCH_ASSOC)) {
// do something
}
} catch (\PDOException $e) {
var_dump($sql);
}
}

/**
* @throws \RuntimeException
*/
public function maybeThrows(): void
{
if (random_int(0, 1) === 1) {
throw new \RuntimeException();
}
}

public function test2(): void
{
global $pdo;

try {
$this->maybeThrows2();
$sql = "SELECT * FROM foo";
$rs = $pdo->query($sql);
if ($result = $rs->fetch(\PDO::FETCH_ASSOC)) {
// do something
}
} catch (\PDOException $e) {
var_dump($sql);
}
}

/**
* @throws \LogicException
*/
public function maybeThrows2(): void
{
if (random_int(0, 1) === 1) {
throw new \LogicException();
}
}
}
Loading