Skip to content

Expression on left side of ?? is only nullable, but non-nullable properties do not cause an error #6063

@atushi-koga

Description

@atushi-koga

Bug report

When the return value of a method is non-nullable, using the null coalescing operator causes an error.
But, When a property is non-nullable, using the null coalescing operator doesn't cause an error.
Isn't it right to make an error?

Code snippet that reproduces the problem

https://phpstan.org/r/2e5a05dc-01e0-4374-8fb6-9e9c5c3ac7d0

Expected output

expect errors like this.

+--------------------------------------------------------------------------------------------------+
| Line | User.php |
+--------------------------------------------------------------------------------------------------+
| 14 | Expression on left side of ?? is not nullable. |
+--------------------------------------------------------------------------------------------------+

Did PHPStan help you today? Did it make you happy in any way?

Thanks to PHPStan, PHP is now a null-safe language! That's wonderful!!
It supports the latest PHP versions so quickly, and I was very impressed when the null safe operator gave an error when called on a non-nullable type.

I reported this because I wanted the null coalesce operator to have the same error when it is used with non-nullable types.
Please consider.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions