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
8 changes: 0 additions & 8 deletions src/Analyser/InternalThrowPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ public function getType(): Type
return $this->type;
}

/**
* @return Node\Expr|Node\Stmt
*/
public function getNode()
{
return $this->node;
}

public function isExplicit(): bool
{
return $this->explicit;
Expand Down
28 changes: 9 additions & 19 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1915,7 +1915,6 @@ private function processStmtNode(
}

// explicit only
$onlyExplicitIsThrow = true;
if (count($matchingThrowPoints) === 0) {
foreach ($throwPoints as $throwPointIndex => $throwPoint) {
foreach ($catchTypes as $catchTypeIndex => $catchTypeItem) {
Expand All @@ -1927,32 +1926,23 @@ private function processStmtNode(
if (!$throwPoint->isExplicit()) {
continue;
}
$throwNode = $throwPoint->getNode();
if (
!$throwNode instanceof Expr\Throw_
&& !($throwNode instanceof Node\Stmt\Expression && $throwNode->expr instanceof Expr\Throw_)
) {
$onlyExplicitIsThrow = false;
}
$matchingThrowPoints[$throwPointIndex] = $throwPoint;
}
}
}

// implicit only
if (count($matchingThrowPoints) === 0 || $onlyExplicitIsThrow) {
foreach ($throwPoints as $throwPointIndex => $throwPoint) {
if ($throwPoint->isExplicit()) {
// implicit
foreach ($throwPoints as $throwPointIndex => $throwPoint) {
if ($throwPoint->isExplicit()) {
continue;
}

foreach ($catchTypes as $catchTypeItem) {
if ($catchTypeItem->isSuperTypeOf($throwPoint->getType())->no()) {
continue;
}

foreach ($catchTypes as $catchTypeItem) {
if ($catchTypeItem->isSuperTypeOf($throwPoint->getType())->no()) {
continue;
}

$matchingThrowPoints[$throwPointIndex] = $throwPoint;
}
$matchingThrowPoints[$throwPointIndex] = $throwPoint;
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-4821.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function sayHello(): void
return;
} catch (\ReflectionException $e) {
assertVariableCertainty(TrinaryLogic::createYes(), $object);
assertVariableCertainty(TrinaryLogic::createNo(), $method);
assertVariableCertainty(TrinaryLogic::createMaybe(), $method);
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/explicit-throws.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function doBar(): void
$a = 1;
$this->throwInvalidArgument();
} catch (\InvalidArgumentException $e) {
assertVariableCertainty(TrinaryLogic::createYes(), $a);
assertVariableCertainty(TrinaryLogic::createMaybe(), $a);
}
}

Expand All @@ -38,7 +38,7 @@ public function doBaz(): void
$this->throwInvalidArgument();
throw new \InvalidArgumentException();
} catch (\InvalidArgumentException $e) {
assertVariableCertainty(TrinaryLogic::createYes(), $a);
assertVariableCertainty(TrinaryLogic::createMaybe(), $a);
}
}

Expand Down
57 changes: 57 additions & 0 deletions tests/PHPStan/Analyser/nsrt/throw-points/bug-9349.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace ThrowPoints\Bug9349;

use PHPStan\TrinaryLogic;
use function PHPStan\Testing\assertType;
use function PHPStan\Testing\assertVariableCertainty;

class Foo
{
/**
* @throws \RuntimeException
*/
public function throwsRuntime(): void
{
}

/**
* @throws \LogicException
*/
public function throwsLogic(): void
{
}

/** @return mixed */
public function doSomething(): void
{
}
}

function (Foo $foo): void {
try {
$foo->throwsRuntime();
$sql = 'SELECT * FROM foo';
$foo->doSomething();
} catch (\PDOException $e) {
// throwsRuntime() declares @throws RuntimeException
// PDOException extends RuntimeException, so throwsRuntime()
// might throw a PDOException before $sql is assigned.
// But doSomething() also has implicit throws after $sql is assigned.
// So $sql might or might not be defined.
assertVariableCertainty(TrinaryLogic::createMaybe(), $sql);
}
};

function (Foo $foo): void {
try {
$foo->throwsLogic();
$sql = 'SELECT * FROM foo';
$foo->doSomething();
} catch (\PDOException $e) {
// LogicException and PDOException are unrelated
// Only implicit throws from doSomething() can reach here
// $sql is assigned before doSomething()
assertVariableCertainty(TrinaryLogic::createYes(), $sql);
}
};
Loading