1.2.1399. Useless Null Coalesce¶
When the type system ensure the condition is never null, the operator becomes useless.
This is particularly true for properties (static or not) and returntype of methods and functions. And, to a lesser extend, to variables and parameters.
<?php
function foo(A $a, ?B $b) {
// $a is never null, so this is OK
$a ?? 'a';
// $b might be null, so this is OK
$b ?? 'b';
}
?>
See also Null coalescing operator.
1.2.1399.1. Connex PHP features¶
1.2.1399.1.1. Suggestions¶
Remove the ?? operator
Switch to a ?: operator
Updated the properties to accept NULL as a possible type
1.2.1399.1.2. Specs¶
Short name |
Structures/UselessNullCoalesce |
Rulesets |
|
Exakat since |
2.4.0 |
PHP Version |
With PHP 7.0 and more recent |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
High |
Available in |