Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PDOException::getCode return type #1018

Merged
merged 2 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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