escape_implicit_backslashes.rst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. Configuration
  20. -------------
  21. ``single_quoted``
  22. ~~~~~~~~~~~~~~~~~
  23. Whether to fix single-quoted strings.
  24. Allowed types: ``bool``
  25. Default value: ``false``
  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. Examples
  37. --------
  38. Example #1
  39. ~~~~~~~~~~
  40. *Default* configuration.
  41. .. code-block:: diff
  42. --- Original
  43. +++ New
  44. <?php
  45. $singleQuoted = 'String with \" and My\Prefix\\';
  46. -$doubleQuoted = "Interpret my \n but not my \a";
  47. +$doubleQuoted = "Interpret my \n but not my \\a";
  48. $hereDoc = <<<HEREDOC
  49. -Interpret my \100 but not my \999
  50. +Interpret my \100 but not my \\999
  51. HEREDOC;
  52. Example #2
  53. ~~~~~~~~~~
  54. With configuration: ``['single_quoted' => true]``.
  55. .. code-block:: diff
  56. --- Original
  57. +++ New
  58. <?php
  59. -$singleQuoted = 'String with \" and My\Prefix\\';
  60. +$singleQuoted = 'String with \\" and My\\Prefix\\';
  61. -$doubleQuoted = "Interpret my \n but not my \a";
  62. +$doubleQuoted = "Interpret my \n but not my \\a";
  63. $hereDoc = <<<HEREDOC
  64. -Interpret my \100 but not my \999
  65. +Interpret my \100 but not my \\999
  66. HEREDOC;
  67. Example #3
  68. ~~~~~~~~~~
  69. With configuration: ``['double_quoted' => false]``.
  70. .. code-block:: diff
  71. --- Original
  72. +++ New
  73. <?php
  74. $singleQuoted = 'String with \" and My\Prefix\\';
  75. $doubleQuoted = "Interpret my \n but not my \a";
  76. $hereDoc = <<<HEREDOC
  77. -Interpret my \100 but not my \999
  78. +Interpret my \100 but not my \\999
  79. HEREDOC;
  80. Example #4
  81. ~~~~~~~~~~
  82. With configuration: ``['heredoc_syntax' => false]``.
  83. .. code-block:: diff
  84. --- Original
  85. +++ New
  86. <?php
  87. $singleQuoted = 'String with \" and My\Prefix\\';
  88. -$doubleQuoted = "Interpret my \n but not my \a";
  89. +$doubleQuoted = "Interpret my \n but not my \\a";
  90. $hereDoc = <<<HEREDOC
  91. Interpret my \100 but not my \999
  92. HEREDOC;
  93. Rule sets
  94. ---------
  95. The rule is part of the following rule set:
  96. - `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_