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

Free-standing binary op exressions should be treated as equivalent "if" expressions #2741

Closed
muglug opened this issue Dec 16, 2019 · 5 comments
Labels
Milestone

Comments

@muglug
Copy link
Contributor

muglug commented Dec 16, 2019

function maybeString(): ?string {
    return rand(0, 10) > 4 ? "test" : null;
}

function test(): string {
    $foo = maybeString();
    ($foo !== null) || ($foo = "");
    return $foo;
}

is directly equivalent to

function maybeString(): ?string {
    return rand(0, 10) > 4 ? "test" : null;
}

function test(): string {
    $foo = maybeString();
    if (!($foo !== null)) {
        $foo = "";
    }
    return $foo;
}

Similarly,

function maybeString(): ?string {
    return rand(0, 10) > 4 ? "test" : null;
}

function test(): string {
    $foo = maybeString();
    ($foo === null) && ($foo = "");
    return $foo;
}

is directly equivalent to

function maybeString(): ?string {
    return rand(0, 10) > 4 ? "test" : null;
}

function test(): string {
    $foo = maybeString();
    if ($foo === null) {
        $foo = "";
    }
    return $foo;
}

https://phpstan.org/r/de46736e-f6da-495a-ad3d-a1484626dfeb and
https://phpstan.org/r/ae3b7244-1720-4bf5-8bf3-5222caf2dacf

@muglug
Copy link
Contributor Author

muglug commented Dec 16, 2019

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Dec 30, 2019
@phpstan-bot
Copy link
Contributor

@muglug PHPStan now reports different result with your code snippet:

@@ @@
+PHP 7.2 – 8.0 (2 errors)
+==========
+
+ 9: Right side of || is always false.
+10: Function test() should return string but returns string|null.
+
+PHP 7.1 (3 errors)
+==========
+
+ 9: Only booleans are allowed in ||, string given on the right side.
  9: Right side of || is always false.
 10: Function test() should return string but returns string|null.
Full report

PHP 7.2 – 8.0 (2 errors)

Line Error
9 Function test() should return string but returns string
10 Function test() should return string but returns string

PHP 7.1 (3 errors)

Line Error
9 Function test() should return string but returns string
9 Function test() should return string but returns string
10 Function test() should return string but returns string

@phpstan-bot
Copy link
Contributor

@muglug PHPStan now reports different result with your code snippet:

@@ @@
+PHP 7.4 – 8.0 (3 errors)
+==========
+
+ 9: Only booleans are allowed in &&, string given on the right side.
+ 9: Right side of && is always false.
+10: Function test() should return string but returns string|null.
+
+PHP 7.1 – 7.3 (2 errors)
+==========
+
  9: Right side of && is always false.
 10: Function test() should return string but returns string|null.
Full report

PHP 7.4 – 8.0 (3 errors)

Line Error
9 Function test() should return string but returns string
9 Function test() should return string but returns string
10 Function test() should return string but returns string

PHP 7.1 – 7.3 (2 errors)

Line Error
9 Function test() should return string but returns string
10 Function test() should return string but returns string

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@f9374e7

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants