Skip to content

CallableMethodValidity ​

warning on by default

Group: Probable bugs · PhpStorm name: CallableMethodValidityInspection

is_callable() on a method callback that names a non-public method, or a non-static method without an object, describes a callback that cannot really be invoked from the outside (or will break when invoked statically). Flag the argument so the method's visibility/static-ness gets fixed.

Example ​

php
<?php
class Shop
{
    public function open() {}
    protected function audit() {}
    public static function hours() {}
    private static function vault() {}
}

function probe(Shop $shop)
{
    $handler = ['Shop', 'open'];
    return [
        is_callable('ucfirst'),
        is_callable([Shop::class, 'open']),
        is_callable('Shop::open'),
        is_callable($handler),
        is_callable(['Shop', 'hours']),
        is_callable('Shop::hours'),
        is_callable([$shop, 'open']),
        is_callable([$shop, 'hours']),
        is_callable([$shop, 'audit']),
        is_callable([Shop::class, 'vault']),
        is_callable(['Shop::open']),
        is_callable([$shop, 'missing']),
    ];
}

Reported:

  • line 15: Method 'open' is not static but is referenced without an object.
  • line 16: Method 'open' is not static but is referenced without an object.
  • line 17: Method 'open' is not static but is referenced without an object.
  • line 22: Method 'audit' is not public, so the callback cannot be invoked from outside.
  • line 23: Method 'vault' is not public, so the callback cannot be invoked from outside.

Configure ​

In custos.json:

json
{
  "rules": {
    "CallableMethodValidity": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore CallableMethodValidity

/**
 * @noinspection CallableMethodValidityInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.