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

Add Exception->getCode() return type provider #7390

Merged
merged 8 commits into from Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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: 2 additions & 0 deletions src/Psalm/Internal/Provider/FunctionReturnTypeProvider.php
Expand Up @@ -23,6 +23,7 @@
use Psalm\Internal\Provider\ReturnTypeProvider\ArraySpliceReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\ArrayUniqueReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\ArrayValuesReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\ExceptionCodeReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\ExplodeReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\FilterVarReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\FirstArgStringReturnTypeProvider;
Expand Down Expand Up @@ -109,6 +110,7 @@ public function __construct()
$this->registerClass(TriggerErrorReturnTypeProvider::class);
$this->registerClass(RandReturnTypeProvider::class);
$this->registerClass(InArrayReturnTypeProvider::class);
$this->registerClass(ExceptionCodeReturnTypeProvider::class);
}

/**
Expand Down
@@ -0,0 +1,38 @@
<?php

namespace Psalm\Internal\Provider\ReturnTypeProvider;

use Psalm\Plugin\EventHandler\Event\MethodReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\MethodReturnTypeProviderInterface;
use Psalm\Type;
use PDOException;

use function is_a;

class ExceptionCodeReturnTypeProvider implements MethodReturnTypeProviderInterface
{
public static function getClassLikeNames(): array
{
return ['Throwable'];
}

public static function getMethodReturnType(MethodReturnTypeProviderEvent $event): ?Type\Union
{
$method_name_lowercase = $event->getMethodNameLowercase();
$fqcn = $event->getCalledFqClasslikeName();

if ($method_name_lowercase !== 'getcode') {
return null;
}

if ($fqcn === 'Exception' || $fqcn === 'Throwable') {
return null;
}

if (is_a($fqcn, PDOException::class, true)) {
orklah marked this conversation as resolved.
Show resolved Hide resolved
return Type::parseString('string');
}

return Type::parseString('int');
}
}
62 changes: 62 additions & 0 deletions tests/ReturnTypeProvider/ExceptionCodeTest.php
@@ -0,0 +1,62 @@
<?php

namespace Psalm\Tests\ReturnTypeProvider;

use Psalm\Tests\TestCase;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;

class ExceptionCodeTest extends TestCase
{
use ValidCodeAnalysisTestTrait;

public function providerValidCodeParse(): iterable
{
yield 'RuntimeException' => [
'<?php
function f(\RuntimeException $e): int {
$code = $e->getCode();
return $code;
}
',
['$code' => 'int'],
];
yield 'LogicException' => [
'<?php
function f(\LogicException $e): int {
$code = $e->getCode();
return $code;
}
',
['$code' => 'int'],
];
yield 'PDOException' => [
'<?php
function f(\PDOException $e): string {
$code = $e->getCode();
return $code;
}
',
['$code' => 'string'],
];
yield 'Exception' => [
'<?php
/** @return int|string */
function f(\Exception $e) {
$code = $e->getCode();
return $code;
}
',
['$code' => 'int|string'],
];
yield 'Throwable' => [
'<?php
/** @return int|string */
function f(\Throwable $e) {
$code = $e->getCode();
return $code;
}
',
['$code' => 'int|string'],
];
}
}