1.2.581. Insufficient Typehint

An argument is typehinted, but it actually calls methods that are not listed in the interface.

Classes may be implementing more methods than the one that are listed in the interface they also implements. This means that filtering objects with a typehint, but calling other methods will be solved at execution time : if the method is available, it will be used; if it is not, a fatal error is reported. Inspired by discussion with Brandon Savage.

<?php

class x implements i {
    function methodI() {}
    function notInI() {}
}

interface i {
    function methodI();
}

function foo(i $x) {
    $x->methodI(); // this call is valid
    $x->notInI();  // this call is not garanteed
}
?>

See also Interface segregation principle.

1.2.581.1. Suggestions

  • Extend the interface with the missing called methods

  • Change the body of the function to use only the methods that are available in the interface

  • Change the used objects so they don’t depend on extra methods

1.2.581.2. Specs

Short name

Functions/InsufficientTypehint

Rulesets

All, Analyze, Typechecks

Exakat since

1.6.6

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Medium

Features

type, interface, abstract-class

Available in

Entreprise Edition, Exakat Cloud