native_constant_invocation.rst 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ===================================
  2. Rule ``native_constant_invocation``
  3. ===================================
  4. Add leading ``\`` before constant invocation of internal constant to speed up
  5. resolving. Constant name match is case-sensitive, except for ``null``, ``false``
  6. and ``true``.
  7. Warning
  8. -------
  9. Using this rule is risky
  10. ~~~~~~~~~~~~~~~~~~~~~~~~
  11. Risky when any of the constants are namespaced or overridden.
  12. Configuration
  13. -------------
  14. ``exclude``
  15. ~~~~~~~~~~~
  16. List of constants to ignore.
  17. Allowed types: ``list<string>``
  18. Default value: ``['null', 'false', 'true']``
  19. ``fix_built_in``
  20. ~~~~~~~~~~~~~~~~
  21. Whether to fix constants returned by ``get_defined_constants``. User constants
  22. are not accounted in this list and must be specified in the include one.
  23. Allowed types: ``bool``
  24. Default value: ``true``
  25. ``include``
  26. ~~~~~~~~~~~
  27. List of additional constants to fix.
  28. Allowed types: ``list<string>``
  29. Default value: ``[]``
  30. ``scope``
  31. ~~~~~~~~~
  32. Only fix constant invocations that are made within a namespace or fix all.
  33. Allowed values: ``'all'`` and ``'namespaced'``
  34. Default value: ``'all'``
  35. ``strict``
  36. ~~~~~~~~~~
  37. Whether leading ``\`` of constant invocation not meant to have it should be
  38. removed.
  39. Allowed types: ``bool``
  40. Default value: ``true``
  41. Examples
  42. --------
  43. Example #1
  44. ~~~~~~~~~~
  45. *Default* configuration.
  46. .. code-block:: diff
  47. --- Original
  48. +++ New
  49. -<?php var_dump(PHP_VERSION, M_PI, MY_CUSTOM_PI);
  50. +<?php var_dump(\PHP_VERSION, \M_PI, MY_CUSTOM_PI);
  51. Example #2
  52. ~~~~~~~~~~
  53. With configuration: ``['scope' => 'namespaced']``.
  54. .. code-block:: diff
  55. --- Original
  56. +++ New
  57. <?php
  58. namespace space1 {
  59. - echo PHP_VERSION;
  60. + echo \PHP_VERSION;
  61. }
  62. namespace {
  63. echo M_PI;
  64. }
  65. Example #3
  66. ~~~~~~~~~~
  67. With configuration: ``['include' => ['MY_CUSTOM_PI']]``.
  68. .. code-block:: diff
  69. --- Original
  70. +++ New
  71. -<?php var_dump(PHP_VERSION, M_PI, MY_CUSTOM_PI);
  72. +<?php var_dump(\PHP_VERSION, \M_PI, \MY_CUSTOM_PI);
  73. Example #4
  74. ~~~~~~~~~~
  75. With configuration: ``['fix_built_in' => false, 'include' => ['MY_CUSTOM_PI']]``.
  76. .. code-block:: diff
  77. --- Original
  78. +++ New
  79. -<?php var_dump(PHP_VERSION, M_PI, MY_CUSTOM_PI);
  80. +<?php var_dump(PHP_VERSION, M_PI, \MY_CUSTOM_PI);
  81. Example #5
  82. ~~~~~~~~~~
  83. With configuration: ``['exclude' => ['M_PI']]``.
  84. .. code-block:: diff
  85. --- Original
  86. +++ New
  87. -<?php var_dump(PHP_VERSION, M_PI, MY_CUSTOM_PI);
  88. +<?php var_dump(\PHP_VERSION, M_PI, MY_CUSTOM_PI);
  89. Rule sets
  90. ---------
  91. The rule is part of the following rule sets:
  92. - `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_ with config:
  93. ``['fix_built_in' => false, 'include' => ['DIRECTORY_SEPARATOR', 'PHP_INT_SIZE', 'PHP_SAPI', 'PHP_VERSION_ID'], 'scope' => 'namespaced', 'strict' => true]``
  94. - `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_ with config:
  95. ``['strict' => false]``
  96. References
  97. ----------
  98. - Fixer class: `PhpCsFixer\\Fixer\\ConstantNotation\\NativeConstantInvocationFixer <./../../../src/Fixer/ConstantNotation/NativeConstantInvocationFixer.php>`_
  99. - Test class: `PhpCsFixer\\Tests\\Fixer\\ConstantNotation\\NativeConstantInvocationFixerTest <./../../../tests/Fixer/ConstantNotation/NativeConstantInvocationFixerTest.php>`_
  100. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.