Skip to content

Commit 89c16a6

Browse files
authored
Add ThrowableReturnTypeExtension (#795)
1 parent b0b6b67 commit 89c16a6

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

conf/config.neon

+5
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,11 @@ services:
12681268
tags:
12691269
- phpstan.broker.dynamicFunctionReturnTypeExtension
12701270

1271+
-
1272+
class: PHPStan\Type\Php\ThrowableReturnTypeExtension
1273+
tags:
1274+
- phpstan.broker.dynamicMethodReturnTypeExtension
1275+
12711276
-
12721277
class: PHPStan\Type\Php\ParseUrlFunctionDynamicReturnTypeExtension
12731278
tags:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Php;
4+
5+
use PhpParser\Node\Expr\MethodCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Type\BenevolentUnionType;
9+
use PHPStan\Type\IntegerType;
10+
use PHPStan\Type\StringType;
11+
use PHPStan\Type\Type;
12+
13+
final class ThrowableReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
14+
{
15+
16+
public function getClass(): string
17+
{
18+
return \Throwable::class;
19+
}
20+
21+
public function isMethodSupported(MethodReflection $methodReflection): bool
22+
{
23+
return $methodReflection->getName() === 'getCode';
24+
}
25+
26+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
27+
{
28+
return new BenevolentUnionType([new IntegerType(), new StringType()]);
29+
}
30+
31+
}

tests/PHPStan/Analyser/NodeScopeResolverTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ public function dataFileAsserts(): iterable
530530
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4743.php');
531531
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5017.php');
532532
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5992.php');
533+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6001.php');
533534

534535
if (PHP_VERSION_ID >= 70400) {
535536
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5458.php');
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug6001;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
assertType('(int|string)', (new \Exception())->getCode());
8+
9+
assertType('(int|string)', (new \RuntimeException())->getCode());
10+
11+
assertType('(int|string)', (new \PDOException())->getCode());

tests/PHPStan/Analyser/data/generics.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1400,7 +1400,7 @@ public function process($class): void {
14001400
}
14011401

14021402
function (\Throwable $e): void {
1403-
assertType('mixed', $e->getCode());
1403+
assertType('(int|string)', $e->getCode());
14041404
};
14051405

14061406
function (): void {

0 commit comments

Comments
 (0)