Phpstan: Reports possibly null value even when null coalescing already protects us

Created on 12 Nov 2017  路  2Comments  路  Source: phpstan/phpstan

phpstan error: Accessing property $prop on possibly null value

code: myFunction($possiblyNullObject->prop ?? null);

explanation: In this case, $possiblyNullObject, could be null based on its type information. But that is OK because the null coalescing operator takes care of that for us. The operator returns the first operand if it exists and is not null. Fortunately for us the property is considered not to exist if the object you are accessing it on does not exist, and that is safe to check it that way.

bug

Most helpful comment

You're right, I'll fix this.

All 2 comments

You're right, I'll fix this.

Was this page helpful?
0 / 5 - 0 ratings