string_implicit_backslashes.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. ====================================
  2. Rule ``string_implicit_backslashes``
  3. ====================================
  4. Handles implicit backslashes in strings and heredocs. Depending on the chosen
  5. strategy, it can escape implicit backslashes to ease the understanding of which
  6. are special chars interpreted by PHP and which not (``escape``), or it can
  7. remove these additional backslashes if you find them superfluous (``unescape``).
  8. You can also leave them as-is using ``ignore`` strategy.
  9. Description
  10. -----------
  11. In PHP double-quoted strings and heredocs some chars like ``n``, ``$`` or ``u``
  12. have special meanings if preceded by a backslash (and some are special only if
  13. followed by other special chars), while a backslash preceding other chars are
  14. interpreted like a plain backslash. The precise list of those special chars is
  15. hard to remember and to identify quickly: this fixer escapes backslashes that do
  16. not start a special interpretation with the char after them.
  17. It is possible to fix also single-quoted strings: in this case there is no
  18. special chars apart from single-quote and backslash itself, so the fixer simply
  19. ensure that all backslashes are escaped. Both single and double backslashes are
  20. allowed in single-quoted strings, so the purpose in this context is mainly to
  21. have a uniformed way to have them written all over the codebase.
  22. Configuration
  23. -------------
  24. ``double_quoted``
  25. ~~~~~~~~~~~~~~~~~
  26. Whether to escape backslashes in double-quoted strings.
  27. Allowed values: ``'escape'``, ``'ignore'`` and ``'unescape'``
  28. Default value: ``'escape'``
  29. ``heredoc``
  30. ~~~~~~~~~~~
  31. Whether to escape backslashes in heredoc syntax.
  32. Allowed values: ``'escape'``, ``'ignore'`` and ``'unescape'``
  33. Default value: ``'escape'``
  34. ``single_quoted``
  35. ~~~~~~~~~~~~~~~~~
  36. Whether to escape backslashes in single-quoted strings.
  37. Allowed values: ``'escape'``, ``'ignore'`` and ``'unescape'``
  38. Default value: ``'unescape'``
  39. Examples
  40. --------
  41. Example #1
  42. ~~~~~~~~~~
  43. *Default* configuration.
  44. .. code-block:: diff
  45. --- Original
  46. +++ New
  47. <?php
  48. $singleQuoted = 'String with \" and My\Prefix\\';
  49. -$doubleQuoted = "Interpret my \n but not my \a";
  50. +$doubleQuoted = "Interpret my \n but not my \\a";
  51. $hereDoc = <<<HEREDOC
  52. -Interpret my \100 but not my \999
  53. +Interpret my \100 but not my \\999
  54. HEREDOC;
  55. Example #2
  56. ~~~~~~~~~~
  57. With configuration: ``['single_quoted' => 'escape']``.
  58. .. code-block:: diff
  59. --- Original
  60. +++ New
  61. <?php
  62. -$singleQuoted = 'String with \" and My\Prefix\\';
  63. +$singleQuoted = 'String with \\" and My\\Prefix\\';
  64. -$doubleQuoted = "Interpret my \n but not my \a";
  65. +$doubleQuoted = "Interpret my \n but not my \\a";
  66. $hereDoc = <<<HEREDOC
  67. -Interpret my \100 but not my \999
  68. +Interpret my \100 but not my \\999
  69. HEREDOC;
  70. Example #3
  71. ~~~~~~~~~~
  72. With configuration: ``['double_quoted' => 'unescape']``.
  73. .. code-block:: diff
  74. --- Original
  75. +++ New
  76. <?php
  77. $singleQuoted = 'String with \" and My\Prefix\\';
  78. $doubleQuoted = "Interpret my \n but not my \a";
  79. $hereDoc = <<<HEREDOC
  80. -Interpret my \100 but not my \999
  81. +Interpret my \100 but not my \\999
  82. HEREDOC;
  83. Example #4
  84. ~~~~~~~~~~
  85. With configuration: ``['heredoc' => 'unescape']``.
  86. .. code-block:: diff
  87. --- Original
  88. +++ New
  89. <?php
  90. $singleQuoted = 'String with \" and My\Prefix\\';
  91. -$doubleQuoted = "Interpret my \n but not my \a";
  92. +$doubleQuoted = "Interpret my \n but not my \\a";
  93. $hereDoc = <<<HEREDOC
  94. Interpret my \100 but not my \999
  95. HEREDOC;
  96. Rule sets
  97. ---------
  98. The rule is part of the following rule set:
  99. - `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_
  100. References
  101. ----------
  102. - Fixer class: `PhpCsFixer\\Fixer\\StringNotation\\StringImplicitBackslashesFixer <./../../../src/Fixer/StringNotation/StringImplicitBackslashesFixer.php>`_
  103. - Test class: `PhpCsFixer\\Tests\\Fixer\\StringNotation\\StringImplicitBackslashesFixerTest <./../../../tests/Fixer/StringNotation/StringImplicitBackslashesFixerTest.php>`_
  104. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.