Skip to content

VariableFunctionsUsage ​

info on by default quick-fix

Group: Performance · PhpStorm name: VariableFunctionsUsageInspection

call_user_func() / forward_static_call() with a callable that is known at the call site can be written as a direct call ($fn(...), Cls::m(...), $obj->m(...)), which is faster and analysable. Likewise call_user_func_array() / forward_static_call_array() with a literal argument array can pass the arguments inline.

Example ​

Level 8.0+:

php
<?php
class Mailer {
    public static function send($to, $body) { return true; }
}
$m = new Mailer();

call_user_func($hook, $to, $log);
call_user_func(['Mailer', 'send'], $to, $body);
call_user_func(array($m, 'send'), $to, $body);
forward_static_call([$m, 'send'], ...$rest);
call_user_func([$m, $verb]);
\call_user_func('App\\Mailer::send', $to);
call_user_func_array($hook, [0 => $to, 1 => $body]);
forward_static_call_array('Mailer::send', array(1, 2));

call_user_func_array($hook, []);
call_user_func_array($hook, [&$to]);
call_user_func_array($hook, $args);
call_user_func_array($hook, ['to' => $to, $body]);
call_user_func($hook, $to, &$log);
call_user_func([$pool[0], 'Base::send'], $to);
call_user_func([$m, 'parent::send'], $to);
call_user_func([$pool[0], 'send'], $to);
call_user_func("{$cls}::send", $to);
call_user_func(build(), $to);
$name = 'mailer';
call_user_func([$name, 'send'], $to);
php
<?php
class Mailer {
    public static function send($to, $body) { return true; }
}
$m = new Mailer();

$hook($to, $log);
Mailer::send($to, $body);
$m->send($to, $body);
$m::send(...$rest);
$m->$verb();
App\Mailer::send($to);
call_user_func($hook, $to, $body);
forward_static_call('Mailer::send', 1, 2);

call_user_func_array($hook, []);
call_user_func_array($hook, [&$to]);
call_user_func_array($hook, $args);
call_user_func_array($hook, ['to' => $to, $body]);
call_user_func($hook, $to, &$log);
call_user_func([$pool[0], 'Base::send'], $to);
call_user_func([$m, 'parent::send'], $to);
call_user_func([$pool[0], 'send'], $to);
call_user_func("{$cls}::send", $to);
call_user_func(build(), $to);
$name = 'mailer';
call_user_func([$name, 'send'], $to);

Reported:

  • line 7: Call it directly: '$hook($to, $log)'.
  • line 8: Call it directly: 'Mailer::send($to, $body)'.
  • line 9: Call it directly: '$m->send($to, $body)'.
  • line 10: Call it directly: '$m::send(...$rest)'.
  • line 11: Call it directly: '$m->$verb()'.
  • line 12: Call it directly: 'App\Mailer::send($to)'.
  • line 13: Pass the arguments inline: 'call_user_func($hook, $to, $body)'.
  • line 14: Pass the arguments inline: 'forward_static_call('Mailer::send', 1, 2)'.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore VariableFunctionsUsage

/**
 * @noinspection VariableFunctionsUsageInspection
 */

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