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

(Throwable~PDOException)::getCode() should be an int #8802

Open
VincentLanglet opened this issue Dec 1, 2022 · 3 comments
Open

(Throwable~PDOException)::getCode() should be an int #8802

VincentLanglet opened this issue Dec 1, 2022 · 3 comments
Labels
easy problems Issues that can be fixed without background knowledge of Psalm enhancement Help wanted

Comments

@VincentLanglet
Copy link
Contributor

Follow up of
#7390
#7525
#7673
@orklah

There is an error in psalm with
https://psalm.dev/r/23800504eb
but it works fine for phpstan
https://phpstan.org/r/014a6319-6af4-4fac-971d-4f4dbb76f57d

Is possible to say to psalm that (Throwable~ PDOException)::getCode() always return an int ?

Current PHPStan implementation: https://github.com/phpstan/phpstan-src/blob/1.9.x/src/Type/Php/ThrowableReturnTypeExtension.php
Current Psalm implementation:

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/23800504eb
<?php

function test(Throwable $e): int {
    if (!$e instanceof Exception) {
        return $e->getCode();
    }
    
    return 0;
}
Psalm output (using commit c3cc906):

ERROR: InvalidReturnStatement - 5:16 - The inferred type 'int|string' does not match the declared return type 'int' for test

ERROR: InvalidReturnType - 3:30 - The declared return type 'int' for test is incorrect, got 'int|string'

@orklah
Copy link
Collaborator

orklah commented Dec 1, 2022

Psalm can't handle Throwable~PDOException as a type in itself.

This means this can't be handled in Psalm with the current type system: https://phpstan.org/r/0112ebba-a9c6-454d-9182-a0e78fa7d014

However, there is a small trick that we can use for your specific example of Throwable~Exception because it can be transformed into Error and Error::getCode is indeed an int

If you're interested, this could be handled the same way as DateTime/DateTimeImmutable:

&& $assertion_type instanceof TNamedObject

@orklah orklah added enhancement easy problems Issues that can be fixed without background knowledge of Psalm Help wanted labels Dec 1, 2022
@weirdan
Copy link
Collaborator

weirdan commented Dec 1, 2022

Can't we access (and evaluate) assertions in MethodCallReturnTypeFetcher?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy problems Issues that can be fixed without background knowledge of Psalm enhancement Help wanted
Projects
None yet
Development

No branches or pull requests

3 participants