Skip to content

Commit

Permalink
Throwable catch block also uses implicit throw points even when there…
Browse files Browse the repository at this point in the history
… are explicit ones
  • Loading branch information
ondrejmirtes committed Apr 23, 2021
1 parent 088518f commit 54a204e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ private function processStmtNode(
$matchingThrowPoints = [];
$newThrowPoints = [];
foreach ($throwPoints as $throwPoint) {
if (!$throwPoint->isExplicit()) {
if (!$throwPoint->isExplicit() && !$catchType->isSuperTypeOf(new ObjectType(\Throwable::class))->yes()) {
continue;
}
$isSuperType = $catchType->isSuperTypeOf($throwPoint->getType());
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/DateTimeDynamicReturnTypes.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4821.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4838.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4879.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4820.php');
}

/**
Expand Down
40 changes: 40 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4820.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Bug4820;

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

class Param {

public bool $foo;
}

class HelloWorld
{
public function sayHello(Param $param): void
{

try {
$result = call_user_func([$this, 'mayThrow']);
if ($param->foo) {
$this->mayThrow();
}

} catch (\Throwable $e) {
assertType('bool', $param->foo);
assertVariableCertainty(TrinaryLogic::createMaybe(), $result);
throw $e;
}
}

/**
* @throws \RuntimeException
*/
private function mayThrow(): void
{
throw new \RuntimeException();
}

}
44 changes: 44 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4879.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Bug4879;

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

class HelloWorld
{
public function sayHello(bool $bool1): void
{
try {
if ($bool1) {
throw new \Exception();
}

$var = 'foo';

$this->test();
} catch (\Throwable $ex) {
assertVariableCertainty(TrinaryLogic::createMaybe(), $var);
}
}

public function sayHello2(bool $bool1): void
{
try {
if ($bool1) {
throw new \Exception();
}

$var = 'foo';

$this->test();
} catch (\Exception $ex) {
assertVariableCertainty(TrinaryLogic::createNo(), $var);
}
}

public function test(): void
{
return;
}
}

0 comments on commit 54a204e

Please sign in to comment.