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
Ignore second value of ternary operator #910
Comments
Simpler testcase that fails in all versions of PHPStan: function weirdEmpty(string $s): ?string {
return (rand(0, 1) ? $s : false) ?: null;
} Problem goes away if you put the function weirdEmpty(string $s): ?string {
$s2 = rand(0, 1) ? $s : false;
return $s2 ?: null;
} @mitsuru793 you can use the same workaround |
Yes, it’s a bug. Currenly only null is filtered, not false. Will fix it.
On Thu, 12 Apr 2018 at 09:15, sato-sm ***@***.***> wrote:
Hi, @muglug <https://github.com/muglug>. Thank you very much for
replaying.
I confirmed your code and it worked. A left of ?: must be variable.
I should write code along the following.
https://phpstan.org/r/008068a6be84e300959ddb89ec983478
<?php declare(strict_types = 1);class Data{ private $data; public function toJson(): ?string {
$json = json_encode($this->data); return $json ?: null; }}
return json_encode($this->data) ?: null;
This doesn't work. Is this a bug?
Is this a bad coding?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#910 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAGZuHaBgRubEq_-sS-j-6uTkTGQaNGkks5tnv8MgaJpZM4TOmH1>
.
--
Ondřej Mirtes
|
Sorry, I replied as wrong another account. Hi, @muglug. Thank you very much for replaying. I confirmed your code and it worked. A left of https://phpstan.org/r/008068a6be84e300959ddb89ec983478 <?php declare(strict_types = 1);
class Data
{
private $data;
public function toJson(): ?string
{
$json = json_encode($this->data);
return $json ?: null;
}
}
|
Hi, @ondrejmirtes. I see that it's a bug. I'm looking forward to fix it. |
Fixed: ea47684 It wasn't about null vs. false, it was about function type-specifying - normally, you can't rely on the fact that the function will return always the same value when called again in a truthy branch of a condition, except for the short ternary operator, where the value itself is used in the |
Sorry, I couldn't understood it correctly, but I could do loosely. I have confirmed your fix. |
Hi, first, thank you very much for this excellent project!
related issues:
Summary of a problem or a feature request
return json_encode($this->data) ?: '';
This code ensures that returns alyways string.
Howerver, phpstan(master) considered that it returns
string|false
, so phpstan ignores?: ''
.This problem occur only on master branch, but not on 0.9.2 tag.
Code snippet that reproduces the problem
json_encode returns string or false, so I ensured that it returns always string or null.
http://php.net/manual/en/function.json-encode.php
master branch
https://phpstan.org/r/8f97c7e5ac57cb845f37071cc7885b0f
0.9.2 tags
https://phpstan.org/r/12c1a9eda8e04bbd0c4c20d21136e4d0
Expected output
This bug occurs again.
I want phpstan to consider
?: <second value>
.The text was updated successfully, but these errors were encountered: