native_function_invocation.rst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. <?php
  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. <?php
  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. <?php
  71. namespace space1 {
  72. - echo count([1]);
  73. + echo \count([1]);
  74. }
  75. namespace {
  76. - echo count([1]);
  77. + echo \count([1]);
  78. }
  79. Example #4
  80. ~~~~~~~~~~
  81. With configuration: ``['scope' => 'namespaced']``.
  82. .. code-block:: diff
  83. --- Original
  84. +++ New
  85. <?php
  86. namespace space1 {
  87. - echo count([1]);
  88. + echo \count([1]);
  89. }
  90. namespace {
  91. echo count([1]);
  92. }
  93. Example #5
  94. ~~~~~~~~~~
  95. With configuration: ``['include' => ['myGlobalFunction']]``.
  96. .. code-block:: diff
  97. --- Original
  98. +++ New
  99. <?php
  100. -myGlobalFunction();
  101. +\myGlobalFunction();
  102. count();
  103. Example #6
  104. ~~~~~~~~~~
  105. With configuration: ``['include' => ['@all']]``.
  106. .. code-block:: diff
  107. --- Original
  108. +++ New
  109. <?php
  110. -myGlobalFunction();
  111. -count();
  112. +\myGlobalFunction();
  113. +\count();
  114. Example #7
  115. ~~~~~~~~~~
  116. With configuration: ``['include' => ['@internal']]``.
  117. .. code-block:: diff
  118. --- Original
  119. +++ New
  120. <?php
  121. myGlobalFunction();
  122. -count();
  123. +\count();
  124. Example #8
  125. ~~~~~~~~~~
  126. With configuration: ``['include' => ['@compiler_optimized']]``.
  127. .. code-block:: diff
  128. --- Original
  129. +++ New
  130. <?php
  131. $a .= str_repeat($a, 4);
  132. -$c = get_class($d);
  133. +$c = \get_class($d);
  134. Rule sets
  135. ---------
  136. The rule is part of the following rule sets:
  137. @PhpCsFixer:risky
  138. Using the `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_ rule set will enable the ``native_function_invocation`` rule with the config below:
  139. ``['include' => ['@compiler_optimized'], 'scope' => 'namespaced', 'strict' => true]``
  140. @Symfony:risky
  141. Using the `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_ rule set will enable the ``native_function_invocation`` rule with the config below:
  142. ``['include' => ['@compiler_optimized'], 'scope' => 'namespaced', 'strict' => true]``