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 23, 2022
1 parent e504c3f commit 9d81ab9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/Type/Php/ThrowableReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
continue 2;
}
}

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

if (in_array(strtolower($class), [
'throwable',
'exception',
'runtimeexception',
], true)) {
$types[] = new BenevolentUnionType([new IntegerType(), new StringType()]);
continue;
Expand Down
9 changes: 5 additions & 4 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 All @@ -24,7 +25,7 @@ public function doFoo(\Throwable $t): void
*/
public function doBar($exception): void
{
assertType('int|string', $exception->getCode());
assertType('(int|string)', $exception->getCode());
}

}
Expand Down

0 comments on commit 9d81ab9

Please sign in to comment.