1.2.1391. Useless NullSafe Operator

Nullsafe operator ?-> has no object when the types are never null, or when coalesce is active.

The nullsafe operator protects the execution from accessing a method or a property on a null value. If the object part of the syntax cannot be null, then the nullsafe operator ?-> will not protect it.

The nullsafe operator is filling the same duty as ?? operator, although with a more granular precision.

<?php

function foo() : A {
     return new A(); // or other code
}

// foo() always returns A, so it is always valid
foo()?->methodOnA();

// goo() may return NULL: ?-> and ?? are filling the same duty
goo()?->methodOnA ?? C;

function goo() : ?A {}

?>

1.2.1391.1. Suggestions

  • Replace the null safe operator with the normal one.

  • Add the type null to the type declaration.

  • Check for null-coalesce operator ?? and choose the most appropriate.

1.2.1391.2. Specs

Short name

Classes/UselessNullSafeOperator

Rulesets

All, Changed Behavior, Class Review

Exakat since

2.6.5

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

nullsafe-operator

Available in

Entreprise Edition, Exakat Cloud