diff --git a/conf/config.neon b/conf/config.neon index 7f494ce35b..0b8b0ca3d3 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -1268,6 +1268,11 @@ services: tags: - phpstan.broker.dynamicFunctionReturnTypeExtension + - + class: PHPStan\Type\Php\ThrowableReturnTypeExtension + tags: + - phpstan.broker.dynamicMethodReturnTypeExtension + - class: PHPStan\Type\Php\ParseUrlFunctionDynamicReturnTypeExtension tags: diff --git a/src/Type/Php/ThrowableReturnTypeExtension.php b/src/Type/Php/ThrowableReturnTypeExtension.php new file mode 100644 index 0000000000..3fc33738a0 --- /dev/null +++ b/src/Type/Php/ThrowableReturnTypeExtension.php @@ -0,0 +1,31 @@ +getName() === 'getCode'; + } + + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type + { + return new BenevolentUnionType([new IntegerType(), new StringType()]); + } + +} diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index e9ffac52e5..475ed7d688 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -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'); diff --git a/tests/PHPStan/Analyser/data/bug-6001.php b/tests/PHPStan/Analyser/data/bug-6001.php new file mode 100644 index 0000000000..0e950940cd --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-6001.php @@ -0,0 +1,11 @@ +getCode()); + +assertType('(int|string)', (new \RuntimeException())->getCode()); + +assertType('(int|string)', (new \PDOException())->getCode()); diff --git a/tests/PHPStan/Analyser/data/generics.php b/tests/PHPStan/Analyser/data/generics.php index 032fa71f17..f35120282e 100644 --- a/tests/PHPStan/Analyser/data/generics.php +++ b/tests/PHPStan/Analyser/data/generics.php @@ -1400,7 +1400,7 @@ public function process($class): void { } function (\Throwable $e): void { - assertType('mixed', $e->getCode()); + assertType('(int|string)', $e->getCode()); }; function (): void {