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

Elseif causes incorrect type inference (4.x only) #8532

Closed
theodorejb opened this issue Oct 4, 2022 · 3 comments
Closed

Elseif causes incorrect type inference (4.x only) #8532

theodorejb opened this issue Oct 4, 2022 · 3 comments

Comments

@theodorejb
Copy link
Contributor

Reproduction:

function possiblyNull(): string|null
{
    return random_int(0, 1) ? "x" : null;
}

$data = [
    'a' => possiblyNull(),
    'b' => possiblyNull(),
    'c' => possiblyNull(),
];

if (!$data['a'] || !$data['b']) {
    throw new Exception("a or b is null");
} elseif (!$data['c']) {
    throw new Exception("c is null");
}

echo strlen($data['a']);

Expected: no error

Actual result:

ERROR: MixedArgument - test.php:20:13 - Argument 1 of strlen cannot be mixed, expecting string

If I remove the elseif condition, the error goes away. E.g. there is no error with the following identical logic using if instead of elseif:

if (!$data['a'] || !$data['b']) {
    throw new Exception("a or b is null");
}

if (!$data['c']) {
    throw new Exception("c is null");
}

This bug seems specific to the 4.x branch (I reproduced with v4.27 and the latest commit on the 4.x branch, but couldn't reproduce with dev-master or on https://psalm.dev/).

@orklah
Copy link
Collaborator

orklah commented Oct 4, 2022

Can you also reproduce it on an oldest version of Psalm?

If this is a recent regression, it may not be merged on master yet. If this is something that was fixed on master, I fear this won't ever be on 4.x. There was a lot of change made on master that can't be put back in current version.

Psalm 5 is to be released very soon now

@theodorejb
Copy link
Contributor Author

I tested all the way back to v4.7.1 and the issue also exists there. It turns out it was fixed on master by faaf769 (pull request #7484).

If Psalm 5 is planned for release soon, I'm fine with closing this issue as won't-fix for 4.x.

@orklah
Copy link
Collaborator

orklah commented Oct 4, 2022

Nice to know this PR has fixed things :)

I'll close this then!

@orklah orklah closed this as completed Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants