1.2.1390. 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.1390.1. Suggestions

  • Remove the ?? operator

  • Switch to a ?: operator

  • Updated the properties to accept NULL as a possible type

1.2.1390.2. Specs

Short name

Structures/UselessNullCoalesce

Rulesets

All, Analyze

Exakat since

2.4.0

PHP Version

With PHP 7.0 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

coalesce

Available in

Entreprise Edition, Exakat Cloud