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

Psalm mixes function/method param types with changed variable type in catch block #4889

Closed
fluffycondor opened this issue Dec 24, 2020 · 3 comments

Comments

@fluffycondor
Copy link
Contributor

https://psalm.dev/r/6638c80c36

I'm fine with MixedAssignment here, but MixedArgument claims that the variable is mixed|string, but if json_decode throws an exception it can't be mixed, it can be only the string that passed to the parseResponse(string $response).

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/6638c80c36
<?php

/** @return mixed */
function parseResponse(string $response) {
    try {
        $response = json_decode($response, true, 512, JSON_THROW_ON_ERROR);
    } catch (JsonException $e) {
        throw new RuntimeException(sprintf("Failed to parse response:\n%s", $response), 0, $e);
    }
    
    return $response;
}
Psalm output (using commit 3390097):

INFO: MixedAssignment - 6:9 - Unable to determine the type that $response is being assigned to

INFO: MixedArgument - 8:77 - Argument 2 of sprintf cannot be mixed|string, expecting float|int|string

@weirdan
Copy link
Collaborator

weirdan commented Dec 24, 2020

This would require Psalm to know what operations can and can't throw, check that there was a single throw point and that the assignment happens after that point.

@AndrolGenhald
Copy link
Collaborator

Closing in favor of #7689 which has a more concrete idea of how to go about fixing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants