From 694157b2e0afbf5869d93fd91f2cecec273ee1fb Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 14 Feb 2022 20:54:26 +0100 Subject: [PATCH] PDOException extends RuntimeException and can use int code errors --- dictionaries/CallMap.php | 2 +- dictionaries/CallMap_historical.php | 2 +- dictionaries/PropertyMap.php | 2 +- .../Method/MethodCallReturnTypeFetcher.php | 9 +++--- .../ReturnTypeProvider/ExceptionCodeTest.php | 30 +++++++++++-------- 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/dictionaries/CallMap.php b/dictionaries/CallMap.php index 5c8237d38..7fff40940 100644 --- a/dictionaries/CallMap.php +++ b/dictionaries/CallMap.php @@ -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'], diff --git a/dictionaries/CallMap_historical.php b/dictionaries/CallMap_historical.php index 8ca16b883..eca9d13fb 100644 --- a/dictionaries/CallMap_historical.php +++ b/dictionaries/CallMap_historical.php @@ -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'], diff --git a/dictionaries/PropertyMap.php b/dictionaries/PropertyMap.php index e0f91665d..1b2ffd01f 100644 --- a/dictionaries/PropertyMap.php +++ b/dictionaries/PropertyMap.php @@ -366,7 +366,7 @@ return [ ], 'pdoexception' => [ 'errorinfo' => 'array', - 'code' => 'string', + 'code' => 'int|string', ], 'domnode' => [ 'nodeName' => 'string', diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallReturnTypeFetcher.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallReturnTypeFetcher.php index a5dc24d17..76d3dfa8e 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallReturnTypeFetcher.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallReturnTypeFetcher.php @@ -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) { diff --git a/tests/ReturnTypeProvider/ExceptionCodeTest.php b/tests/ReturnTypeProvider/ExceptionCodeTest.php index 84e8acb28..a6aa3b8ee 100644 --- a/tests/ReturnTypeProvider/ExceptionCodeTest.php +++ b/tests/ReturnTypeProvider/ExceptionCodeTest.php @@ -13,27 +13,33 @@ class ExceptionCodeTest extends TestCase { yield 'RuntimeException' => [ 'getCode(); - } + /** @var \RuntimeException $e */ + $code = $e->getCode(); ', - [], + ['$code' => 'int|string'], + ]; + yield 'CustomRuntimeException' => [ + 'getCode(); + ', + ['$code' => 'int'], ]; yield 'LogicException' => [ 'getCode(); - } + /** @var \LogicException $e */ + $code = $e->getCode(); ', - [], + ['$code' => 'int'], ]; yield 'PDOException' => [ 'getCode(); - } + /** @var \PDOException $e */ + $code = $e->getCode(); ', - [], + ['$code' => 'int|string'], ]; yield 'CustomThrowable' => [ '