1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

PDOException extends RuntimeException and can use int code errors

This commit is contained in:
Vincent Langlet 2022-02-14 20:54:26 +01:00
parent 293937fbc2
commit 694157b2e0
5 changed files with 25 additions and 20 deletions

View File

@ -9892,7 +9892,7 @@ return [
'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'],

View File

@ -4890,7 +4890,7 @@ return [
'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'],

View File

@ -366,7 +366,7 @@ return [
],
'pdoexception' => [
'errorinfo' => 'array',
'code' => 'string',
'code' => 'int|string',
],
'domnode' => [
'nodeName' => 'string',

View File

@ -29,6 +29,7 @@ use Psalm\Type\Atomic\TGenericObject;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Union;
use RuntimeException;
use Throwable;
use UnexpectedValueException;
@ -97,16 +98,14 @@ class MethodCallReturnTypeFetcher
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
&& (
$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) {

View File

@ -13,27 +13,33 @@ class ExceptionCodeTest extends TestCase
{
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