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

PDOException extends RuntimeException and can use int code errors #7673

Merged
merged 1 commit 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
2 changes: 1 addition & 1 deletion dictionaries/CallMap.php
Expand Up @@ -9892,7 +9892,7 @@
'PDO::sqliteCreateCollation' => ['bool', 'name'=>'string', 'callback'=>'callable'],
'PDO::sqliteCreateFunction' => ['bool', 'function_name'=>'string', 'callback'=>'callable', 'num_args='=>'int'],
'pdo_drivers' => ['array'],
'PDOException::getCode' => ['string'],
'PDOException::getCode' => ['int|string'],
'PDOException::getFile' => ['string'],
'PDOException::getLine' => ['int'],
'PDOException::getMessage' => ['string'],
Expand Down
2 changes: 1 addition & 1 deletion dictionaries/CallMap_historical.php
Expand Up @@ -4890,7 +4890,7 @@
'PDO::sqliteCreateAggregate' => ['bool', 'function_name'=>'string', 'step_func'=>'callable', 'finalize_func'=>'callable', 'num_args='=>'int'],
'PDO::sqliteCreateCollation' => ['bool', 'name'=>'string', 'callback'=>'callable'],
'PDO::sqliteCreateFunction' => ['bool', 'function_name'=>'string', 'callback'=>'callable', 'num_args='=>'int'],
'PDOException::getCode' => ['string'],
'PDOException::getCode' => ['int|string'],
'PDOException::getFile' => ['string'],
'PDOException::getLine' => ['int'],
'PDOException::getMessage' => ['string'],
Expand Down
2 changes: 1 addition & 1 deletion dictionaries/PropertyMap.php
Expand Up @@ -366,7 +366,7 @@
],
'pdoexception' => [
'errorinfo' => 'array',
'code' => 'string',
'code' => 'int|string',
],
'domnode' => [
'nodeName' => 'string',
Expand Down
Expand Up @@ -29,6 +29,7 @@
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Union;
use RuntimeException;
use Throwable;
use UnexpectedValueException;

Expand Down Expand Up @@ -97,16 +98,14 @@ public static function fetch(

if ($premixin_method_id->method_name === 'getcode'
&& $premixin_method_id->fq_class_name !== Exception::class
&& $premixin_method_id->fq_class_name !== RuntimeException::class
&& $premixin_method_id->fq_class_name !== PDOException::class
orklah marked this conversation as resolved.
Show resolved Hide resolved
&& (
$codebase->classImplements($premixin_method_id->fq_class_name, Throwable::class)
|| $codebase->interfaceExtends($premixin_method_id->fq_class_name, Throwable::class)
)
) {
if ($premixin_method_id->fq_class_name === PDOException::class) {
return Type::getString();
} else {
return Type::getInt(true); // TODO: Remove the flag in Psalm 5
}
return Type::getInt(true); // TODO: Remove the flag in Psalm 5
}

if ($declaring_method_id && $declaring_method_id !== $method_id) {
Expand Down
30 changes: 18 additions & 12 deletions tests/ReturnTypeProvider/ExceptionCodeTest.php
Expand Up @@ -13,27 +13,33 @@ public function providerValidCodeParse(): iterable
{
yield 'RuntimeException' => [
'<?php
function f(\RuntimeException $e): int {
return $e->getCode();
}
/** @var \RuntimeException $e */
$code = $e->getCode();
',
['$code' => 'int|string'],
];
yield 'CustomRuntimeException' => [
'<?php
class CustomRuntimeException extends \RuntimeException {}

/** @var CustomRuntimeException $e */
$code = $e->getCode();
',
[],
['$code' => 'int'],
];
yield 'LogicException' => [
'<?php
function f(\LogicException $e): int {
return $e->getCode();
}
/** @var \LogicException $e */
$code = $e->getCode();
',
[],
['$code' => 'int'],
];
yield 'PDOException' => [
'<?php
function f(\PDOException $e): string {
return $e->getCode();
}
/** @var \PDOException $e */
$code = $e->getCode();
',
[],
['$code' => 'int|string'],
];
yield 'CustomThrowable' => [
'<?php
Expand Down