function_to_constant.rst 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. =============================
  2. Rule ``function_to_constant``
  3. =============================
  4. Replace core functions calls returning constants with the constants.
  5. Warning
  6. -------
  7. Using this rule is risky
  8. ~~~~~~~~~~~~~~~~~~~~~~~~
  9. Risky when any of the configured functions to replace are overridden.
  10. Configuration
  11. -------------
  12. ``functions``
  13. ~~~~~~~~~~~~~
  14. List of function names to fix.
  15. Allowed values: a subset of ``['get_called_class', 'get_class', 'get_class_this', 'php_sapi_name', 'phpversion', 'pi']``
  16. Default value: ``['get_called_class', 'get_class', 'get_class_this', 'php_sapi_name', 'phpversion', 'pi']``
  17. Examples
  18. --------
  19. Example #1
  20. ~~~~~~~~~~
  21. *Default* configuration.
  22. .. code-block:: diff
  23. --- Original
  24. +++ New
  25. <?php
  26. -echo phpversion();
  27. -echo pi();
  28. -echo php_sapi_name();
  29. +echo PHP_VERSION;
  30. +echo M_PI;
  31. +echo PHP_SAPI;
  32. class Foo
  33. {
  34. public function Bar()
  35. {
  36. - echo get_class();
  37. - echo get_called_class();
  38. + echo self::class;
  39. + echo static::class;
  40. }
  41. }
  42. Example #2
  43. ~~~~~~~~~~
  44. With configuration: ``['functions' => ['get_called_class', 'get_class_this', 'phpversion']]``.
  45. .. code-block:: diff
  46. --- Original
  47. +++ New
  48. <?php
  49. -echo phpversion();
  50. +echo PHP_VERSION;
  51. echo pi();
  52. class Foo
  53. {
  54. public function Bar()
  55. {
  56. echo get_class();
  57. - get_class($this);
  58. - echo get_called_class();
  59. + static::class;
  60. + echo static::class;
  61. }
  62. }
  63. Rule sets
  64. ---------
  65. The rule is part of the following rule sets:
  66. - `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_
  67. - `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_
  68. References
  69. ----------
  70. - Fixer class: `PhpCsFixer\\Fixer\\LanguageConstruct\\FunctionToConstantFixer <./../../../src/Fixer/LanguageConstruct/FunctionToConstantFixer.php>`_
  71. - Test class: `PhpCsFixer\\Tests\\Fixer\\LanguageConstruct\\FunctionToConstantFixerTest <./../../../tests/Fixer/LanguageConstruct/FunctionToConstantFixerTest.php>`_
  72. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.