1.2.274. Could Be Typehinted Callable

Those arguments may use the callable Typehint.

‘callable’ is a PHP keyword that represents callback functions. Those may be used in dynamic function call, like $function(); or as callback functions, like with array_map();

callable may be a string representing a function name or a static call (including ::), an array with two elements, (a class or object, and a method), or a closure <https://www.php.net/`closure>`_.

When arguments are used to call a function, but are not marked with ‘callable’, they are reported by this analysis.

<?php

function foo(callable $callable) {
    // very simple callback
    return $callable();
}

function foo2($array, $callable) {
    // very simple callback
    return array_map($array, $callable);
}

?>

See also Callback / callable.

1.2.274.1. Suggestions

  • Add the typehint callable

  • Use the function is_callable() inside the method if ‘callable’ is too strong.

1.2.274.2. Specs

Short name

Functions/CouldBeCallable

Rulesets

All, Changed Behavior

Exakat since

0.10.5

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

callable

Examples

Magento, PrestaShop

Available in

Entreprise Edition, Exakat Cloud