1.2.865. Nullable Without Check

Nullable typed argument or properties should be checked before usage. When they are null, they probably won’t behave like the other type, and lead to an error.

<?php

// This will emit a fatal error when $a = null
function foo(?A $a) {
    return $a->m();
}

// This is stable
function foo(?A $a) {
    if ($a === null) {
        return 42;
    } else {
        return $a->m();
    }
}

?>

See also Null Return Types.

1.2.865.1. Suggestions

  • Add a check on return value

1.2.865.2. Specs

Short name

Functions/NullableWithoutCheck

Rulesets

All, Class Review

Exakat since

2.0.2

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

null, return-typehint

Available in

Entreprise Edition, Exakat Cloud