Skip to content

Commit

Permalink
Add ThrowableReturnTypeExtension (phpstan#795)
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm authored and MidnightDesign committed Nov 30, 2021
1 parent 31b3a37 commit b1485a0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,11 @@ services:
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\ThrowableReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: PHPStan\Type\Php\ParseUrlFunctionDynamicReturnTypeExtension
tags:
Expand Down
31 changes: 31 additions & 0 deletions src/Type/Php/ThrowableReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;

final class ThrowableReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
{

public function getClass(): string
{
return \Throwable::class;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'getCode';
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
return new BenevolentUnionType([new IntegerType(), new StringType()]);
}

}
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4743.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5017.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5992.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6001.php');

if (PHP_VERSION_ID >= 70400) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5458.php');
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Analyser/data/bug-6001.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Bug6001;

use function PHPStan\Testing\assertType;

assertType('(int|string)', (new \Exception())->getCode());

assertType('(int|string)', (new \RuntimeException())->getCode());

assertType('(int|string)', (new \PDOException())->getCode());
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/generics.php
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,7 @@ public function process($class): void {
}

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

function (): void {
Expand Down

0 comments on commit b1485a0

Please sign in to comment.