Skip to content

Commit e04cc8d

Browse files
committed
ThrowableReturnTypeExtension needs to honor @method tag
1 parent 27bc69b commit e04cc8d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Type/Php/ThrowableReturnTypeExtension.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
3939
$pdoException = new ObjectType('PDOException');
4040
foreach (TypeUtils::getDirectClassNames($type) as $class) {
4141
$classType = new ObjectType($class);
42+
if ($classType->getClassReflection() !== null) {
43+
$classReflection = $classType->getClassReflection();
44+
foreach ($classReflection->getMethodTags() as $methodName => $methodTag) {
45+
if (strtolower($methodName) !== 'getcode') {
46+
continue;
47+
}
48+
49+
$types[] = $methodTag->getReturnType();
50+
continue 2;
51+
}
52+
}
4253
if ($pdoException->isSuperTypeOf($classType)->yes()) {
4354
$types[] = new StringType();
4455
continue;

tests/PHPStan/Analyser/data/bug-6001.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function doFoo(\Throwable $t): void
1515
assertType('string', (new \PDOException())->getCode());
1616
assertType('int', (new MyException())->getCode());
1717
assertType('string', (new SubPDOException())->getCode());
18+
assertType('1|2|3', (new ExceptionWithMethodTag())->getCode());
1819
}
1920

2021
/**
@@ -37,3 +38,11 @@ class SubPDOException extends \PDOException
3738
{
3839

3940
}
41+
42+
/**
43+
* @method 1|2|3 getCode()
44+
*/
45+
class ExceptionWithMethodTag extends \Exception
46+
{
47+
48+
}

0 commit comments

Comments
 (0)