native_function_invocation.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. ===================================
  2. Rule ``native_function_invocation``
  3. ===================================
  4. Add leading ``\`` before function invocation to speed up resolving.
  5. Warning
  6. -------
  7. Using this rule is risky
  8. ~~~~~~~~~~~~~~~~~~~~~~~~
  9. Risky when any of the functions are overridden.
  10. Configuration
  11. -------------
  12. ``exclude``
  13. ~~~~~~~~~~~
  14. List of functions to ignore.
  15. Allowed types: ``list<string>``
  16. Default value: ``[]``
  17. ``include``
  18. ~~~~~~~~~~~
  19. List of function names or sets to fix. Defined sets are ``@internal`` (all
  20. native functions), ``@all`` (all global functions) and ``@compiler_optimized``
  21. (functions that are specially optimized by Zend).
  22. Allowed types: ``list<string>``
  23. Default value: ``['@compiler_optimized']``
  24. ``scope``
  25. ~~~~~~~~~
  26. Only fix function calls that are made within a namespace or fix all.
  27. Allowed values: ``'all'`` and ``'namespaced'``
  28. Default value: ``'all'``
  29. ``strict``
  30. ~~~~~~~~~~
  31. Whether leading ``\`` of function call not meant to have it should be removed.
  32. Allowed types: ``bool``
  33. Default value: ``true``
  34. Examples
  35. --------
  36. Example #1
  37. ~~~~~~~~~~
  38. *Default* configuration.
  39. .. code-block:: diff
  40. --- Original
  41. +++ New
  42. <?php
  43. function baz($options)
  44. {
  45. - if (!array_key_exists("foo", $options)) {
  46. + if (!\array_key_exists("foo", $options)) {
  47. throw new \InvalidArgumentException();
  48. }
  49. return json_encode($options);
  50. }
  51. Example #2
  52. ~~~~~~~~~~
  53. With configuration: ``['exclude' => ['json_encode']]``.
  54. .. code-block:: diff
  55. --- Original
  56. +++ New
  57. <?php
  58. function baz($options)
  59. {
  60. - if (!array_key_exists("foo", $options)) {
  61. + if (!\array_key_exists("foo", $options)) {
  62. throw new \InvalidArgumentException();
  63. }
  64. return json_encode($options);
  65. }
  66. Example #3
  67. ~~~~~~~~~~
  68. With configuration: ``['scope' => 'all']``.
  69. .. code-block:: diff
  70. --- Original
  71. +++ New
  72. <?php
  73. namespace space1 {
  74. - echo count([1]);
  75. + echo \count([1]);
  76. }
  77. namespace {
  78. - echo count([1]);
  79. + echo \count([1]);
  80. }
  81. Example #4
  82. ~~~~~~~~~~
  83. With configuration: ``['scope' => 'namespaced']``.
  84. .. code-block:: diff
  85. --- Original
  86. +++ New
  87. <?php
  88. namespace space1 {
  89. - echo count([1]);
  90. + echo \count([1]);
  91. }
  92. namespace {
  93. echo count([1]);
  94. }
  95. Example #5
  96. ~~~~~~~~~~
  97. With configuration: ``['include' => ['myGlobalFunction']]``.
  98. .. code-block:: diff
  99. --- Original
  100. +++ New
  101. <?php
  102. -myGlobalFunction();
  103. +\myGlobalFunction();
  104. count();
  105. Example #6
  106. ~~~~~~~~~~
  107. With configuration: ``['include' => ['@all']]``.
  108. .. code-block:: diff
  109. --- Original
  110. +++ New
  111. <?php
  112. -myGlobalFunction();
  113. -count();
  114. +\myGlobalFunction();
  115. +\count();
  116. Example #7
  117. ~~~~~~~~~~
  118. With configuration: ``['include' => ['@internal']]``.
  119. .. code-block:: diff
  120. --- Original
  121. +++ New
  122. <?php
  123. myGlobalFunction();
  124. -count();
  125. +\count();
  126. Example #8
  127. ~~~~~~~~~~
  128. With configuration: ``['include' => ['@compiler_optimized']]``.
  129. .. code-block:: diff
  130. --- Original
  131. +++ New
  132. <?php
  133. $a .= str_repeat($a, 4);
  134. -$c = get_class($d);
  135. +$c = \get_class($d);
  136. Rule sets
  137. ---------
  138. The rule is part of the following rule sets:
  139. - `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_ with config:
  140. ``['include' => ['@compiler_optimized'], 'scope' => 'namespaced', 'strict' => true]``
  141. - `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_ with config:
  142. ``['include' => ['@compiler_optimized'], 'scope' => 'namespaced', 'strict' => true]``
  143. References
  144. ----------
  145. - Fixer class: `PhpCsFixer\\Fixer\\FunctionNotation\\NativeFunctionInvocationFixer <./../../../src/Fixer/FunctionNotation/NativeFunctionInvocationFixer.php>`_
  146. - Test class: `PhpCsFixer\\Tests\\Fixer\\FunctionNotation\\NativeFunctionInvocationFixerTest <./../../../tests/Fixer/FunctionNotation/NativeFunctionInvocationFixerTest.php>`_
  147. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.