native_function_invocation.rst 3.8 KB

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