1.2.124. Check After Null Safe Operator¶
While it saves some checks in certain cases, the null-safe operator should be followed by a check on the returned value to process any misfire of the method.
This analysis checks that the result of the expression is collected, and compared to null.
<?php
$result = $object?->foo();
if ($result === null) {
throw new ObjectException(The object could not call $foo\n);
}
?>
1.2.124.1. Suggestions¶
Collect and check the result of the expression to null
Remove the null-safe operator and check before calling the object’s method or property
1.2.124.2. Specs¶
Short name |
Classes/CheckAfterNullSafeOperator |
Rulesets |
|
Exakat since |
2.6.4 |
PHP Version |
With PHP 8.1 and more recent |
Severity |
Major |
Time To Fix |
Quick (30 mins) |
Precision |
High |
Features |
nullsafe-object-operator |
Available in |