function_to_constant.rst 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 __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>`_