4.2.44. Set Typehints

Automagically add scalar typehints to methods and properties. Arguments and return values are both supported.

When multiple possible types are identified, no typehint is added. If a typehint is already set, no typehint is added.

Magic methods, such as __get(), __set(), __construct(), __desctruct(), etc are not modified by this cobbler.

Methods which have parent’s methods (resp. children’s) are skipped for argument typing (resp return typing) : this may introduce a incompatible definition. On the other hand, methods which have children’s methods (resp. parents’) are modified for argument typing (resp return typing), thanks to covariance (resp. contravariance).

Void (as a scalar type) and Null types are processed in a separate cobbler.

By default, and in case of conflict, array is chosen over iterable and int is chosen over float. There are parameter to alter this behavior.

4.2.44.1. Before

<?php

class x {
    private int $p = 2;

    function foo(int $a = 1) : int {
        return intdiv($a, $this->p);
    }
}
?>

4.2.44.2. After

<?php

class x {
    private int $p = 2;

    function foo(int $a = 1) : int {
        return intdiv($a, $this->p);
    }
}
?>

4.2.44.3. Parameters

Name

Default

Type

Description

array_or_iterable

array

string

When array and iterable are the only suggestions, choose ‘array’, ‘iterable’, or ‘omit’. By default, it is array.

int_or_float

float

string

When int and float are the only suggestions, choose ‘int’, ‘float’, or ‘omit’. By default, it is float.

4.2.44.4. Suggested Analysis

4.2.44.6. Reverse Cobbler

  • No anchor for Functions/RemoveTypehint

4.2.44.7. Specs

Short Name

Functions/SetTypehints

Exakat version

2.3.0

Available in

Entreprise Edition, Exakat Cloud