native_constant_invocation.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. ``strict``
  33. ~~~~~~~~~~
  34. Whether leading ``\`` of constant invocation not meant to have it should be
  35. removed.
  36. Allowed types: ``bool``
  37. Default value: ``false``
  38. Examples
  39. --------
  40. Example #1
  41. ~~~~~~~~~~
  42. *Default* configuration.
  43. .. code-block:: diff
  44. --- Original
  45. +++ New
  46. -<?php var_dump(PHP_VERSION, M_PI, MY_CUSTOM_PI);
  47. +<?php var_dump(\PHP_VERSION, \M_PI, MY_CUSTOM_PI);
  48. Example #2
  49. ~~~~~~~~~~
  50. With configuration: ``['scope' => 'namespaced']``.
  51. .. code-block:: diff
  52. --- Original
  53. +++ New
  54. <?php
  55. namespace space1 {
  56. - echo PHP_VERSION;
  57. + echo \PHP_VERSION;
  58. }
  59. namespace {
  60. echo M_PI;
  61. }
  62. Example #3
  63. ~~~~~~~~~~
  64. With configuration: ``['include' => ['MY_CUSTOM_PI']]``.
  65. .. code-block:: diff
  66. --- Original
  67. +++ New
  68. -<?php var_dump(PHP_VERSION, M_PI, MY_CUSTOM_PI);
  69. +<?php var_dump(\PHP_VERSION, \M_PI, \MY_CUSTOM_PI);
  70. Example #4
  71. ~~~~~~~~~~
  72. With configuration: ``['fix_built_in' => false, 'include' => ['MY_CUSTOM_PI']]``.
  73. .. code-block:: diff
  74. --- Original
  75. +++ New
  76. -<?php var_dump(PHP_VERSION, M_PI, MY_CUSTOM_PI);
  77. +<?php var_dump(PHP_VERSION, M_PI, \MY_CUSTOM_PI);
  78. Example #5
  79. ~~~~~~~~~~
  80. With configuration: ``['exclude' => ['M_PI']]``.
  81. .. code-block:: diff
  82. --- Original
  83. +++ New
  84. -<?php var_dump(PHP_VERSION, M_PI, MY_CUSTOM_PI);
  85. +<?php var_dump(\PHP_VERSION, M_PI, MY_CUSTOM_PI);
  86. Rule sets
  87. ---------
  88. The rule is part of the following rule sets:
  89. @PhpCsFixer:risky
  90. Using the `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_ rule set will enable the ``native_constant_invocation`` rule with the config below:
  91. ``['fix_built_in' => false, 'include' => ['DIRECTORY_SEPARATOR', 'PHP_INT_SIZE', 'PHP_SAPI', 'PHP_VERSION_ID'], 'scope' => 'namespaced', 'strict' => true]``
  92. @Symfony:risky
  93. Using the `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_ rule set will enable the ``native_constant_invocation`` rule with the default config.