escape_implicit_backslashes.rst 3.7 KB

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