1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +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::sqliteCreateCollation' => ['bool', 'name'=>'string', 'callback'=>'callable'],
'PDO::sqliteCreateFunction' => ['bool', 'function_name'=>'string', 'callback'=>'callable', 'num_args='=>'int'], 'PDO::sqliteCreateFunction' => ['bool', 'function_name'=>'string', 'callback'=>'callable', 'num_args='=>'int'],
'pdo_drivers' => ['array'], 'pdo_drivers' => ['array'],
'PDOException::getCode' => ['string'], 'PDOException::getCode' => ['int|string'],
'PDOException::getFile' => ['string'], 'PDOException::getFile' => ['string'],
'PDOException::getLine' => ['int'], 'PDOException::getLine' => ['int'],
'PDOException::getMessage' => ['string'], '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::sqliteCreateAggregate' => ['bool', 'function_name'=>'string', 'step_func'=>'callable', 'finalize_func'=>'callable', 'num_args='=>'int'],
'PDO::sqliteCreateCollation' => ['bool', 'name'=>'string', 'callback'=>'callable'], 'PDO::sqliteCreateCollation' => ['bool', 'name'=>'string', 'callback'=>'callable'],
'PDO::sqliteCreateFunction' => ['bool', 'function_name'=>'string', 'callback'=>'callable', 'num_args='=>'int'], 'PDO::sqliteCreateFunction' => ['bool', 'function_name'=>'string', 'callback'=>'callable', 'num_args='=>'int'],
'PDOException::getCode' => ['string'], 'PDOException::getCode' => ['int|string'],
'PDOException::getFile' => ['string'], 'PDOException::getFile' => ['string'],
'PDOException::getLine' => ['int'], 'PDOException::getLine' => ['int'],
'PDOException::getMessage' => ['string'], 'PDOException::getMessage' => ['string'],

View File

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

View File

@ -29,6 +29,7 @@ use Psalm\Type\Atomic\TGenericObject;
use Psalm\Type\Atomic\TNamedObject; use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TTemplateParam; use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Union; use Psalm\Type\Union;
use RuntimeException;
use Throwable; use Throwable;
use UnexpectedValueException; use UnexpectedValueException;
@ -97,16 +98,14 @@ class MethodCallReturnTypeFetcher
if ($premixin_method_id->method_name === 'getcode' if ($premixin_method_id->method_name === 'getcode'
&& $premixin_method_id->fq_class_name !== Exception::class && $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->classImplements($premixin_method_id->fq_class_name, Throwable::class)
|| $codebase->interfaceExtends($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::getInt(true); // TODO: Remove the flag in Psalm 5
return Type::getString();
} else {
return Type::getInt(true); // TODO: Remove the flag in Psalm 5
}
} }
if ($declaring_method_id && $declaring_method_id !== $method_id) { if ($declaring_method_id && $declaring_method_id !== $method_id) {

View File

@ -13,27 +13,33 @@ class ExceptionCodeTest extends TestCase
{ {
yield 'RuntimeException' => [ yield 'RuntimeException' => [
'<?php '<?php
function f(\RuntimeException $e): int { /** @var \RuntimeException $e */
return $e->getCode(); $code = $e->getCode();
}
', ',
[], ['$code' => 'int|string'],
];
yield 'CustomRuntimeException' => [
'<?php
class CustomRuntimeException extends \RuntimeException {}
/** @var CustomRuntimeException $e */
$code = $e->getCode();
',
['$code' => 'int'],
]; ];
yield 'LogicException' => [ yield 'LogicException' => [
'<?php '<?php
function f(\LogicException $e): int { /** @var \LogicException $e */
return $e->getCode(); $code = $e->getCode();
}
', ',
[], ['$code' => 'int'],
]; ];
yield 'PDOException' => [ yield 'PDOException' => [
'<?php '<?php
function f(\PDOException $e): string { /** @var \PDOException $e */
return $e->getCode(); $code = $e->getCode();
}
', ',
[], ['$code' => 'int|string'],
]; ];
yield 'CustomThrowable' => [ yield 'CustomThrowable' => [
'<?php '<?php