Skip to content

Commit

Permalink
ThrowableReturnTypeExtension needs to honor @method tag
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 7, 2022
1 parent 27bc69b commit e04cc8d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Type/Php/ThrowableReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
$pdoException = new ObjectType('PDOException');
foreach (TypeUtils::getDirectClassNames($type) as $class) {
$classType = new ObjectType($class);
if ($classType->getClassReflection() !== null) {
$classReflection = $classType->getClassReflection();
foreach ($classReflection->getMethodTags() as $methodName => $methodTag) {
if (strtolower($methodName) !== 'getcode') {
continue;
}

$types[] = $methodTag->getReturnType();
continue 2;
}
}
if ($pdoException->isSuperTypeOf($classType)->yes()) {
$types[] = new StringType();
continue;
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/data/bug-6001.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function doFoo(\Throwable $t): void
assertType('string', (new \PDOException())->getCode());
assertType('int', (new MyException())->getCode());
assertType('string', (new SubPDOException())->getCode());
assertType('1|2|3', (new ExceptionWithMethodTag())->getCode());
}

/**
Expand All @@ -37,3 +38,11 @@ class SubPDOException extends \PDOException
{

}

/**
* @method 1|2|3 getCode()
*/
class ExceptionWithMethodTag extends \Exception
{

}

0 comments on commit e04cc8d

Please sign in to comment.