native_constant_invocation.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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:: Using this rule is risky.
  8. Risky when any of the constants are namespaced or overridden.
  9. Configuration
  10. -------------
  11. ``fix_built_in``
  12. ~~~~~~~~~~~~~~~~
  13. Whether to fix constants returned by ``get_defined_constants``. User constants
  14. are not accounted in this list and must be specified in the include one.
  15. Allowed types: ``bool``
  16. Default value: ``true``
  17. ``include``
  18. ~~~~~~~~~~~
  19. List of additional constants to fix.
  20. Allowed types: ``array``
  21. Default value: ``[]``
  22. ``exclude``
  23. ~~~~~~~~~~~
  24. List of constants to ignore.
  25. Allowed types: ``array``
  26. Default value: ``['null', 'false', 'true']``
  27. ``scope``
  28. ~~~~~~~~~
  29. Only fix constant invocations that are made within a namespace or fix all.
  30. Allowed values: ``'all'``, ``'namespaced'``
  31. Default value: ``'all'``
  32. Examples
  33. --------
  34. Example #1
  35. ~~~~~~~~~~
  36. *Default* configuration.
  37. .. code-block:: diff
  38. --- Original
  39. +++ New
  40. @@ -1 +1 @@
  41. -<?php var_dump(PHP_VERSION, M_PI, MY_CUSTOM_PI);
  42. +<?php var_dump(\PHP_VERSION, \M_PI, MY_CUSTOM_PI);
  43. Example #2
  44. ~~~~~~~~~~
  45. With configuration: ``['scope' => 'namespaced']``.
  46. .. code-block:: diff
  47. --- Original
  48. +++ New
  49. @@ -1,7 +1,7 @@
  50. <?php
  51. namespace space1 {
  52. - echo PHP_VERSION;
  53. + echo \PHP_VERSION;
  54. }
  55. namespace {
  56. echo M_PI;
  57. }
  58. Example #3
  59. ~~~~~~~~~~
  60. With configuration: ``['include' => ['MY_CUSTOM_PI']]``.
  61. .. code-block:: diff
  62. --- Original
  63. +++ New
  64. @@ -1 +1 @@
  65. -<?php var_dump(PHP_VERSION, M_PI, MY_CUSTOM_PI);
  66. +<?php var_dump(\PHP_VERSION, \M_PI, \MY_CUSTOM_PI);
  67. Example #4
  68. ~~~~~~~~~~
  69. With configuration: ``['fix_built_in' => false, 'include' => ['MY_CUSTOM_PI']]``.
  70. .. code-block:: diff
  71. --- Original
  72. +++ New
  73. @@ -1 +1 @@
  74. -<?php var_dump(PHP_VERSION, M_PI, MY_CUSTOM_PI);
  75. +<?php var_dump(PHP_VERSION, M_PI, \MY_CUSTOM_PI);
  76. Example #5
  77. ~~~~~~~~~~
  78. With configuration: ``['exclude' => ['M_PI']]``.
  79. .. code-block:: diff
  80. --- Original
  81. +++ New
  82. @@ -1 +1 @@
  83. -<?php var_dump(PHP_VERSION, M_PI, MY_CUSTOM_PI);
  84. +<?php var_dump(\PHP_VERSION, M_PI, MY_CUSTOM_PI);
  85. Rule sets
  86. ---------
  87. The rule is part of the following rule sets:
  88. @PhpCsFixer:risky
  89. Using the `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_ rule set will enable the ``native_constant_invocation`` rule with the config below:
  90. ``['fix_built_in' => false, 'include' => ['DIRECTORY_SEPARATOR', 'PHP_SAPI', 'PHP_VERSION_ID'], 'scope' => 'namespaced']``
  91. @Symfony:risky
  92. Using the `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_ rule set will enable the ``native_constant_invocation`` rule with the config below:
  93. ``['fix_built_in' => false, 'include' => ['DIRECTORY_SEPARATOR', 'PHP_SAPI', 'PHP_VERSION_ID'], 'scope' => 'namespaced']``