regular_callable_call.rst 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. ==============================
  2. Rule ``regular_callable_call``
  3. ==============================
  4. Callables must be called without using ``call_user_func*`` when possible.
  5. Warning
  6. -------
  7. Using this rule is risky
  8. ~~~~~~~~~~~~~~~~~~~~~~~~
  9. Risky when the ``call_user_func`` or ``call_user_func_array`` function is
  10. overridden or when are used in constructions that should be avoided, like
  11. ``call_user_func_array('foo', ['bar' => 'baz'])`` or ``call_user_func($foo, $foo
  12. = 'bar')``.
  13. Examples
  14. --------
  15. Example #1
  16. ~~~~~~~~~~
  17. .. code-block:: diff
  18. --- Original
  19. +++ New
  20. <?php
  21. - call_user_func("var_dump", 1, 2);
  22. + var_dump(1, 2);
  23. - call_user_func("Bar\Baz::d", 1, 2);
  24. + Bar\Baz::d(1, 2);
  25. - call_user_func_array($callback, [1, 2]);
  26. + $callback(...[1, 2]);
  27. Example #2
  28. ~~~~~~~~~~
  29. .. code-block:: diff
  30. --- Original
  31. +++ New
  32. <?php
  33. -call_user_func(function ($a, $b) { var_dump($a, $b); }, 1, 2);
  34. +(function ($a, $b) { var_dump($a, $b); })(1, 2);
  35. -call_user_func(static function ($a, $b) { var_dump($a, $b); }, 1, 2);
  36. +(static function ($a, $b) { var_dump($a, $b); })(1, 2);
  37. References
  38. ----------
  39. - Fixer class: `PhpCsFixer\\Fixer\\FunctionNotation\\RegularCallableCallFixer <./../../../src/Fixer/FunctionNotation/RegularCallableCallFixer.php>`_
  40. - Test class: `PhpCsFixer\\Tests\\Fixer\\FunctionNotation\\RegularCallableCallFixerTest <./../../../tests/Fixer/FunctionNotation/RegularCallableCallFixerTest.php>`_
  41. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.