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 no longer recognizes boolean returns on enum functions #9546

Open
bitwise-operators opened this issue Mar 20, 2023 · 4 comments
Open
Labels

Comments

@bitwise-operators
Copy link
Contributor

Take this example:

https://psalm.dev/r/8c3364a135

Failing the first if statement incorrectly assigns $value as null, ignoring the possibility that $value->isOne() evaluated to false.

This is a regression, as it popped up when I upgraded from tag 4e177bf (last commit on the 5.x branch) to 9cf4f60 (current 5.8 state)

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/8c3364a135
<?php
enum Val
{
    case One;
    case Two;
    case Three;
    
    public function isOne():bool {
        return $this == static::One;
    }
    
    public function isTwo(): bool {
        return $this == static::Two;
    }
    
	public static function random(): ?static {
    	return match (rand(1,5)) {
            1 => static::One,
            2 => static::Two,
            3 => static::Three,
            default => null,
        };
    }
}
$value = Val::random();

if ($value?->isOne()) {
    echo 'Solo';
} elseif ($value?->isTwo()) {
    echo 'In pairs';
} else {
    echo 'Grouped';
}
Psalm output (using commit 0af503a):

ERROR: RedundantCondition - 29:11 - $value is null has already been asserted

ERROR: NullReference - 29:20 - Cannot call method isTwo on null value

ERROR: TypeDoesNotContainType - 29:11 - Type null for $value is always !null

@weirdan weirdan added the bug label Mar 20, 2023
@weirdan
Copy link
Collaborator

weirdan commented Mar 20, 2023

Likely due to #9538

Well, no, that hasn't been tagged yet.

@weirdan
Copy link
Collaborator

weirdan commented Mar 20, 2023

@bitwise-operators can you check on master?

@orklah
Copy link
Collaborator

orklah commented Mar 20, 2023

#9457 could be related

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

No branches or pull requests

3 participants