1.2.1400. Useless Type Check

With the type system, checking the type of arguments is handled by PHP.

In particular, a typed argument can’t be null, unless it is explicitly nullable, or has a null value as default.

<?php

// The test on null is useless, it will never happen
function foo(A $a) {
    if (is_null($a)) {
        // do something
    }
}

// Either nullable ? is too much, either the default null is
function barbar(?A $a = null) {
}

// The test on null is useful, the default value null allows it
function bar(A $a = null) {
    if ($a === null) {
        // do something
    }
}


?>

See also Type Declarations.

1.2.1400.1. Suggestions

  • Remove the nullable typehint

  • Remove the null default value

  • Remove tests on null

1.2.1400.2. Specs

Short name

Functions/UselessTypeCheck

Rulesets

All, Dead code

Exakat since

1.8.9

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

typehint

Available in

Entreprise Edition, Exakat Cloud