1.2.589. Insufficient Property Type

The type used for a class property doesn’t cover all it usage.

The type is insufficient when a undefined method or constant is called, or if members are accessed while the type is an interface.

This analysis relies on typed properties, as introduced in PHP 7.4. It also relies on typed assignations at construct time : the type of the assigned argument will be used as the property type. Getters and setters are not considered here.

<?php

class A {
    function a1() {}
}

// PHP 7.4 and more recent
class B {
    private A $a = null;

    function b2() {
        // this method is available in A
        $this->a->a1();
        // this method is NOT available in A
        $this->a->a2();
    }
}

// Supported by all PHP versions
class C {
    private $a = null;

    function __construct(A $a) {
        $this->a = $a;
    }

    function b2() {
        // this method is available in A
        $this->a->a1();
        // this method is NOT available in A
        $this->a->a2();
    }
}

?>

1.2.589.1. Connex PHP features

1.2.589.1.1. Suggestions

  • Change the type to match the actual usage of the object in the class.

1.2.589.1.2. Specs

Short name

Classes/InsufficientPropertyTypehint

Rulesets

All, Changed Behavior, Class Review

Exakat since

2.0.2

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Available in

Entreprise Edition, Exakat Cloud