numeric_literal_separator.rst 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ==================================
  2. Rule ``numeric_literal_separator``
  3. ==================================
  4. Adds separators to numeric literals of any kind.
  5. Configuration
  6. -------------
  7. ``override_existing``
  8. ~~~~~~~~~~~~~~~~~~~~~
  9. Whether literals already containing underscores should be reformatted.
  10. Allowed types: ``bool``
  11. Default value: ``false``
  12. ``strategy``
  13. ~~~~~~~~~~~~
  14. Whether numeric literal should be separated by underscores or not.
  15. Allowed values: ``'no_separator'`` and ``'use_separator'``
  16. Default value: ``'use_separator'``
  17. Examples
  18. --------
  19. Example #1
  20. ~~~~~~~~~~
  21. *Default* configuration.
  22. .. code-block:: diff
  23. --- Original
  24. +++ New
  25. <?php
  26. -$integer = 1234567890;
  27. +$integer = 1_234_567_890;
  28. Example #2
  29. ~~~~~~~~~~
  30. With configuration: ``['strategy' => 'no_separator']``.
  31. .. code-block:: diff
  32. --- Original
  33. +++ New
  34. <?php
  35. -$integer = 1234_5678;
  36. -$octal = 01_234_56;
  37. -$binary = 0b00_10_01_00;
  38. -$hexadecimal = 0x3D45_8F4F;
  39. +$integer = 12345678;
  40. +$octal = 0123456;
  41. +$binary = 0b00100100;
  42. +$hexadecimal = 0x3D458F4F;
  43. Example #3
  44. ~~~~~~~~~~
  45. With configuration: ``['strategy' => 'use_separator']``.
  46. .. code-block:: diff
  47. --- Original
  48. +++ New
  49. <?php
  50. -$integer = 12345678;
  51. -$octal = 0123456;
  52. -$binary = 0b0010010011011010;
  53. -$hexadecimal = 0x3D458F4F;
  54. +$integer = 12_345_678;
  55. +$octal = 0123_456;
  56. +$binary = 0b00100100_11011010;
  57. +$hexadecimal = 0x3D_45_8F_4F;
  58. Example #4
  59. ~~~~~~~~~~
  60. With configuration: ``['override_existing' => true]``.
  61. .. code-block:: diff
  62. --- Original
  63. +++ New
  64. -<?php $var = 24_40_21;
  65. +<?php $var = 244_021;
  66. References
  67. ----------
  68. - Fixer class: `PhpCsFixer\\Fixer\\Basic\\NumericLiteralSeparatorFixer <./../../../src/Fixer/Basic/NumericLiteralSeparatorFixer.php>`_
  69. - Test class: `PhpCsFixer\\Tests\\Fixer\\Basic\\NumericLiteralSeparatorFixerTest <./../../../tests/Fixer/Basic/NumericLiteralSeparatorFixerTest.php>`_
  70. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.