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

Logical OR leads to TypeDoesNotContainType #7079

Closed
sanmai opened this issue Dec 6, 2021 · 10 comments
Closed

Logical OR leads to TypeDoesNotContainType #7079

sanmai opened this issue Dec 6, 2021 · 10 comments
Labels
bug hard problems Problems without an obvious easy solution

Comments

@sanmai
Copy link
Contributor

sanmai commented Dec 6, 2021

https://psalm.dev/r/2b0bbc6018

ERROR: TypeDoesNotContainType - 21:6 - Type ""|"0"|null for $email is always falsy

But if I split it into several ifs, Psalm gets it without issues:
https://psalm.dev/r/c8564dd6d9

You can see this works:
https://3v4l.org/era8I

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/2b0bbc6018
<?php

interface X {
	public function getStringOptional(): ?string;
}

/** @var X $x */

$url = $x->getStringOptional();
$email = $x->getStringOptional();


if (
    (empty($url) && empty($email)) ||
    (!empty($url) && !empty($email)) ||
    (!empty($email) && strpos($email, '@') == false)
) {
 	throw new \Exception();
}

if (!empty($email)) {}
Psalm output (using commit 4a35a85):

ERROR: TypeDoesNotContainType - 21:6 - Type ""|"0"|null for $email is always falsy
https://psalm.dev/r/c8564dd6d9
<?php

interface X {
	public function getStringOptional(): ?string;
}

/** @var X $x */

$url = $x->getStringOptional();
$email = $x->getStringOptional();


if (
    (empty($url) && empty($email)) 
) {
 	throw new \Exception();
}


if (
    (!empty($url) && !empty($email))
) {
 	throw new \Exception();
}

if (
    (!empty($email) && strpos($email, '@') == false)
) {
 	throw new \Exception();
}

if (!empty($email)) {}
Psalm output (using commit 4a35a85):

No issues!

@orklah
Copy link
Collaborator

orklah commented Dec 6, 2021

probably related or even a duplicate of #6683

@orklah orklah added bug hard problems Problems without an obvious easy solution labels Dec 6, 2021
@WossomGonzo

This comment has been minimized.

@psalm-github-bot

This comment has been minimized.

@orklah

This comment has been minimized.

@WossomGonzo

This comment has been minimized.

@psalm-github-bot

This comment has been minimized.

@orklah

This comment has been minimized.

@WossomGonzo

This comment has been minimized.

@muglug
Copy link
Collaborator

muglug commented Feb 10, 2022

Yeah closing this in favour of #6683, since it has the same root cause.

@muglug muglug closed this as completed Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug hard problems Problems without an obvious easy solution
Projects
None yet
Development

No branches or pull requests

4 participants