Skip to content

Commit b9d2cd8

Browse files
patrickkusebauchondrejmirtes
authored andcommitted
Dynamic "assert" throw type extension
1 parent bcdb95b commit b9d2cd8

4 files changed

Lines changed: 90 additions & 0 deletions

File tree

‎conf/config.neon‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,11 @@ services:
13261326
tags:
13271327
- phpstan.broker.dynamicFunctionReturnTypeExtension
13281328

1329+
-
1330+
class: PHPStan\Type\Php\AssertThrowTypeExtension
1331+
tags:
1332+
- phpstan.dynamicFunctionThrowTypeExtension
1333+
13291334
-
13301335
class: PHPStan\Type\Php\BackedEnumFromMethodDynamicReturnTypeExtension
13311336
tags:
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Php;
4+
5+
use PhpParser\Node\Expr\FuncCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Reflection\FunctionReflection;
8+
use PHPStan\Type\DynamicFunctionThrowTypeExtension;
9+
use PHPStan\Type\ObjectType;
10+
use PHPStan\Type\Type;
11+
use Throwable;
12+
use function count;
13+
14+
class AssertThrowTypeExtension implements DynamicFunctionThrowTypeExtension
15+
{
16+
17+
public function isFunctionSupported(FunctionReflection $functionReflection): bool
18+
{
19+
return $functionReflection->getName() === 'assert';
20+
}
21+
22+
public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): ?Type
23+
{
24+
if (count($funcCall->getArgs()) < 2) {
25+
return $functionReflection->getThrowType();
26+
}
27+
28+
$customThrow = $scope->getType($funcCall->getArgs()[1]->value);
29+
if ((new ObjectType(Throwable::class))->isSuperTypeOf($customThrow)->yes()) {
30+
return $customThrow;
31+
}
32+
33+
return $functionReflection->getThrowType();
34+
}
35+
36+
}

‎tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,4 +480,18 @@ public function testBug9406(): void
480480
$this->analyse([__DIR__ . '/data/bug-9406.php'], []);
481481
}
482482

483+
public function testBug5650(): void
484+
{
485+
$this->analyse([__DIR__ . '/data/bug-5650.php'], [
486+
[
487+
'Dead catch - RuntimeException is never thrown in the try block.',
488+
24,
489+
],
490+
[
491+
'Dead catch - RuntimeException is never thrown in the try block.',
492+
32,
493+
],
494+
]);
495+
}
496+
483497
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Bug5650;
4+
5+
function (): void {
6+
try {
7+
assert(false, new RuntimeException("I am being thrown"));
8+
} catch (\RuntimeException $e) {
9+
10+
}
11+
};
12+
13+
function (): void {
14+
try {
15+
assert(true, new RuntimeException("I could be but this time am not being thrown"));
16+
} catch (\RuntimeException $e) {
17+
18+
}
19+
};
20+
21+
function (): void {
22+
try {
23+
assert(false);
24+
} catch (\RuntimeException $e) {
25+
26+
}
27+
};
28+
29+
function (): void {
30+
try {
31+
assert(true);
32+
} catch (\RuntimeException $e) {
33+
34+
}
35+
};

0 commit comments

Comments
 (0)