binary_operator_spaces.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. ===============================
  2. Rule ``binary_operator_spaces``
  3. ===============================
  4. Binary operators should be surrounded by space as configured.
  5. Configuration
  6. -------------
  7. ``default``
  8. ~~~~~~~~~~~
  9. Default fix strategy.
  10. Allowed values: ``'align'``, ``'align_by_scope'``, ``'align_single_space'``, ``'align_single_space_by_scope'``, ``'align_single_space_minimal'``, ``'align_single_space_minimal_by_scope'``, ``'at_least_single_space'``, ``'no_space'``, ``'single_space'`` and ``null``
  11. Default value: ``'single_space'``
  12. ``operators``
  13. ~~~~~~~~~~~~~
  14. Dictionary of ``binary operator`` => ``fix strategy`` values that differ from
  15. the default strategy. Supported are: ``=``, ``*``, ``/``, ``%``, ``<``, ``>``,
  16. ``|``, ``^``, ``+``, ``-``, ``&``, ``&=``, ``&&``, ``||``, ``.=``, ``/=``,
  17. ``=>``, ``==``, ``>=``, ``===``, ``!=``, ``<>``, ``!==``, ``<=``, ``and``,
  18. ``or``, ``xor``, ``-=``, ``%=``, ``*=``, ``|=``, ``+=``, ``<<``, ``<<=``,
  19. ``>>``, ``>>=``, ``^=``, ``**``, ``**=``, ``<=>``, ``??`` and ``??=``.
  20. Allowed types: ``array<string, ?string>``
  21. Default value: ``[]``
  22. Examples
  23. --------
  24. Example #1
  25. ~~~~~~~~~~
  26. *Default* configuration.
  27. .. code-block:: diff
  28. --- Original
  29. +++ New
  30. <?php
  31. -$a= 1 + $b^ $d !== $e or $f;
  32. +$a = 1 + $b ^ $d !== $e or $f;
  33. Example #2
  34. ~~~~~~~~~~
  35. With configuration: ``['operators' => ['=' => 'align', 'xor' => null]]``.
  36. .. code-block:: diff
  37. --- Original
  38. +++ New
  39. <?php
  40. $aa= 1;
  41. -$b=2;
  42. +$b =2;
  43. $c = $d xor $e;
  44. -$f -= 1;
  45. +$f -= 1;
  46. Example #3
  47. ~~~~~~~~~~
  48. With configuration: ``['operators' => ['+=' => 'align_single_space']]``.
  49. .. code-block:: diff
  50. --- Original
  51. +++ New
  52. <?php
  53. -$a = $b +=$c;
  54. -$d = $ee+=$f;
  55. +$a = $b += $c;
  56. +$d = $ee += $f;
  57. -$g = $b +=$c;
  58. -$h = $ee+=$f;
  59. +$g = $b += $c;
  60. +$h = $ee += $f;
  61. Example #4
  62. ~~~~~~~~~~
  63. With configuration: ``['operators' => ['===' => 'align_single_space_minimal']]``.
  64. .. code-block:: diff
  65. --- Original
  66. +++ New
  67. <?php
  68. -$a = $b===$c;
  69. -$d = $f === $g;
  70. -$h = $i=== $j;
  71. +$a = $b === $c;
  72. +$d = $f === $g;
  73. +$h = $i === $j;
  74. Example #5
  75. ~~~~~~~~~~
  76. With configuration: ``['operators' => ['|' => 'no_space']]``.
  77. .. code-block:: diff
  78. --- Original
  79. +++ New
  80. <?php
  81. -$foo = \json_encode($bar, JSON_PRESERVE_ZERO_FRACTION | JSON_PRETTY_PRINT);
  82. +$foo = \json_encode($bar, JSON_PRESERVE_ZERO_FRACTION|JSON_PRETTY_PRINT);
  83. Example #6
  84. ~~~~~~~~~~
  85. With configuration: ``['operators' => ['=>' => 'single_space']]``.
  86. .. code-block:: diff
  87. --- Original
  88. +++ New
  89. <?php
  90. $array = [
  91. - "foo" => 1,
  92. - "baaaaaaaaaaar" => 11,
  93. + "foo" => 1,
  94. + "baaaaaaaaaaar" => 11,
  95. ];
  96. Example #7
  97. ~~~~~~~~~~
  98. With configuration: ``['operators' => ['=>' => 'align']]``.
  99. .. code-block:: diff
  100. --- Original
  101. +++ New
  102. <?php
  103. $array = [
  104. - "foo" => 12,
  105. + "foo" => 12,
  106. "baaaaaaaaaaar" => 13,
  107. "baz" => 1,
  108. ];
  109. Example #8
  110. ~~~~~~~~~~
  111. With configuration: ``['operators' => ['=>' => 'align_by_scope']]``.
  112. .. code-block:: diff
  113. --- Original
  114. +++ New
  115. <?php
  116. $array = [
  117. - "foo" => 12,
  118. + "foo" => 12,
  119. "baaaaaaaaaaar" => 13,
  120. - "baz" => 1,
  121. + "baz" => 1,
  122. ];
  123. Example #9
  124. ~~~~~~~~~~
  125. With configuration: ``['operators' => ['=>' => 'align_single_space']]``.
  126. .. code-block:: diff
  127. --- Original
  128. +++ New
  129. <?php
  130. $array = [
  131. - "foo" => 12,
  132. + "foo" => 12,
  133. "baaaaaaaaaaar" => 13,
  134. "baz" => 1,
  135. ];
  136. Example #10
  137. ~~~~~~~~~~~
  138. With configuration: ``['operators' => ['=>' => 'align_single_space_by_scope']]``.
  139. .. code-block:: diff
  140. --- Original
  141. +++ New
  142. <?php
  143. $array = [
  144. - "foo" => 12,
  145. + "foo" => 12,
  146. "baaaaaaaaaaar" => 13,
  147. - "baz" => 1,
  148. + "baz" => 1,
  149. ];
  150. Example #11
  151. ~~~~~~~~~~~
  152. With configuration: ``['operators' => ['=>' => 'align_single_space_minimal']]``.
  153. .. code-block:: diff
  154. --- Original
  155. +++ New
  156. <?php
  157. $array = [
  158. - "foo" => 12,
  159. - "baaaaaaaaaaar" => 13,
  160. + "foo" => 12,
  161. + "baaaaaaaaaaar" => 13,
  162. "baz" => 1,
  163. ];
  164. Example #12
  165. ~~~~~~~~~~~
  166. With configuration: ``['operators' => ['=>' => 'align_single_space_minimal_by_scope']]``.
  167. .. code-block:: diff
  168. --- Original
  169. +++ New
  170. <?php
  171. $array = [
  172. - "foo" => 12,
  173. - "baaaaaaaaaaar" => 13,
  174. + "foo" => 12,
  175. + "baaaaaaaaaaar" => 13,
  176. - "baz" => 1,
  177. + "baz" => 1,
  178. ];
  179. Rule sets
  180. ---------
  181. The rule is part of the following rule sets:
  182. - `@PER <./../../ruleSets/PER.rst>`_ with config:
  183. ``['default' => 'at_least_single_space']``
  184. - `@PER-CS <./../../ruleSets/PER-CS.rst>`_ with config:
  185. ``['default' => 'at_least_single_space']``
  186. - `@PER-CS1.0 <./../../ruleSets/PER-CS1.0.rst>`_ with config:
  187. ``['default' => 'at_least_single_space']``
  188. - `@PER-CS2.0 <./../../ruleSets/PER-CS2.0.rst>`_ with config:
  189. ``['default' => 'at_least_single_space']``
  190. - `@PSR12 <./../../ruleSets/PSR12.rst>`_ with config:
  191. ``['default' => 'at_least_single_space']``
  192. - `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_
  193. - `@Symfony <./../../ruleSets/Symfony.rst>`_
  194. References
  195. ----------
  196. - Fixer class: `PhpCsFixer\\Fixer\\Operator\\BinaryOperatorSpacesFixer <./../../../src/Fixer/Operator/BinaryOperatorSpacesFixer.php>`_
  197. - Test class: `PhpCsFixer\\Tests\\Fixer\\Operator\\BinaryOperatorSpacesFixerTest <./../../../tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php>`_
  198. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.