Skip to content

Commit

Permalink
Merge pull request #7673 from VincentLanglet/pdoException
Browse files Browse the repository at this point in the history
PDOException extends RuntimeException and can use int code errors
  • Loading branch information
orklah authored Feb 23, 2022
2 parents 919775c + 694157b commit 3a85f49
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dictionaries/CallMap.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@
],
'pdoexception' => [
'errorinfo' => 'array',
'code' => 'string',
'code' => 'int|string',
],
'domnode' => [
'nodeName' => 'string',
Expand Down
Original file line number Diff line number Diff line change
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
&& (
$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
Original file line number Diff line number Diff line change
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

0 comments on commit 3a85f49

Please sign in to comment.