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
False positive: Using nullsafe property access on non-nullable type #7109
Comments
/cc @rajyan Looks like a job for you :) Thanks. |
@ondrejmirtes @schlndh The error message should be improved though. |
Oh nice, I haven't realized PHPStan is actually right here! |
Hmm, you're right that it works (I didn't realize that). But since the left side can be null, I don't think it's necessary to complain about the |
Agreed with you. |
Hello, I came across this issue with native php enum, replicated here: https://phpstan.org/r/8525ac50-91f9-4c0d-a343-36842dbdf9fe Hope it helps |
It might be annoying, but PHPStan is right :) This code is functionally equivalent: So we might be able to improve the error message, but otherwise this error is going to continue be reported :) |
Oh yeah, you're right my mistake. Sorry 🤦 |
tbh i'm not sure if it should be checked, without this rule code could be more consistent: <?php
class Foo
{
public string $str = '';
public function test(): string
{
return '';
}
}
class Bar
{
public ?Foo $foo = null;
}
$bar = new Bar;
echo $bar->foo?->str ?? 'one';
echo $bar->foo?->test() ?? 'two'; In the example above php indeed doesn't require |
but that doesn't produce any errors in my case though.. |
@jacekkarczmarczyk https://phpstan.org/r/c5fbd20e-c88a-47e9-a6e6-e45a983ed5ea but looking at number of reports from users, I was thinking that it might be better not to report it in property fetch case too. |
Not sure what you mean? It does produce the error as shown in the @rajyan comment. echo $bar->foo->str ?? 'one';
echo $bar->foo?->test() ?? 'two'; where as you see sometimes |
@jacekkarczmarczyk |
@ondrejmirtes |
@rajyan I consider it useful and from the same category as "Strict comparison using === between string and null will always evaluate to false.". My thinking is that the path forward is:
Could you please implement that? Thanks. |
I like this checker, but it doesn't seem to fit the actual use cases. // This point is useful because neither getUser() nor User->user_id are nullable.
$user_id_1 = getUser()?->user_id ?? 0; // expected
// This point is verbose because `null?->user_id` returns null.
$user_id_2 = getUserOrNull()?->user_id ?? 0; |
The point (and where opinions are divided) here is that in coalesce/isset/empty null->test ?? "test";
isset(null->test) ?: "test";
null->test->call() ?? "test";
empty(null->test->call()) ?: "test"; https://3v4l.org/fltef PHPStan is understanding that behavior right, but because the expression before null safe can be null, at least the error message is wrong. |
@rajyan Thank you, I finally understand. <?php
var_dump(null->user_id); // Raise the warning as expected
var_dump(null->user_id ?? 0); // Without Warning Focusing on using Knowing that, I could understand that the focus was only on whether to warn of such preventative and verbose code. |
Nightmare is over! Fix is released :) https://github.com/phpstan/phpstan/releases/tag/1.6.2 |
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. |
Bug report
It seems that phpstan 1.6 gets confused when using
?->
together with??
and complains about?->
even when the left side of it is indeed nullable.Code snippet that reproduces the problem
https://phpstan.org/r/b9aade5f-f220-4b10-934f-dd6338acfe05
Expected output
No issues.
The text was updated successfully, but these errors were encountered: