numeric_literal_separator.rst 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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: ``'no_separator'``
  17. Examples
  18. --------
  19. Example #1
  20. ~~~~~~~~~~
  21. *Default* configuration.
  22. .. code-block:: diff
  23. --- Original
  24. +++ New
  25. <?php
  26. -$integer = 1234_5678;
  27. -$octal = 01_234_56;
  28. -$binary = 0b00_10_01_00;
  29. -$hexadecimal = 0x3D45_8F4F;
  30. +$integer = 12345678;
  31. +$octal = 0123456;
  32. +$binary = 0b00100100;
  33. +$hexadecimal = 0x3D458F4F;
  34. Example #2
  35. ~~~~~~~~~~
  36. With configuration: ``['strategy' => 'use_separator']``.
  37. .. code-block:: diff
  38. --- Original
  39. +++ New
  40. <?php
  41. -$integer = 12345678;
  42. -$octal = 0123456;
  43. -$binary = 0b0010010011011010;
  44. -$hexadecimal = 0x3D458F4F;
  45. +$integer = 12_345_678;
  46. +$octal = 0123_456;
  47. +$binary = 0b00100100_11011010;
  48. +$hexadecimal = 0x3D_45_8F_4F;
  49. Example #3
  50. ~~~~~~~~~~
  51. With configuration: ``['override_existing' => true]``.
  52. .. code-block:: diff
  53. --- Original
  54. +++ New
  55. -<?php $var = 24_40_21;
  56. +<?php $var = 244021;
  57. References
  58. ----------
  59. - Fixer class: `PhpCsFixer\\Fixer\\Basic\\NumericLiteralSeparatorFixer <./../../../src/Fixer/Basic/NumericLiteralSeparatorFixer.php>`_
  60. - Test class: `PhpCsFixer\\Tests\\Fixer\\Basic\\NumericLiteralSeparatorFixerTest <./../../../tests/Fixer/Basic/NumericLiteralSeparatorFixerTest.php>`_
  61. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.