Skip to content

Commit

Permalink
Fix PDOException::getCode return type
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Feb 14, 2022
1 parent 832a9a1 commit 62acc52
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/Type/Php/ThrowableReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use Throwable;
use function count;
use function in_array;
Expand Down Expand Up @@ -50,14 +51,16 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
continue 2;
}
}

if ($pdoException->isSuperTypeOf($classType)->yes()) {
$types[] = new StringType();
$types[] = new UnionType([new IntegerType(), new StringType()]);
continue;
}

if (in_array(strtolower($class), [
'throwable',
'exception',
'throwable',
'exception',
'runtimeexception',
], true)) {
$types[] = new BenevolentUnionType([new IntegerType(), new StringType()]);
continue;
Expand Down
7 changes: 4 additions & 3 deletions tests/PHPStan/Analyser/data/bug-6001.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ public function doFoo(\Throwable $t): void
{
assertType('(int|string)', (new \Exception())->getCode());
assertType('(int|string)', $t->getCode());
assertType('int', (new \RuntimeException())->getCode());
assertType('string', (new \PDOException())->getCode());
assertType('(int|string)', (new \RuntimeException())->getCode());
assertType('int', (new \LogicException())->getCode());
assertType('int|string', (new \PDOException())->getCode());
assertType('int', (new MyException())->getCode());
assertType('string', (new SubPDOException())->getCode());
assertType('int|string', (new SubPDOException())->getCode());
assertType('1|2|3', (new ExceptionWithMethodTag())->getCode());
}

Expand Down

0 comments on commit 62acc52

Please sign in to comment.