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 1 commit
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
5 changes: 4 additions & 1 deletion 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()]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather be if this was a benevolent union type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I made the change

continue;
}

if (in_array(strtolower($class), [
'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