binary_operator_spaces.rst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_single_space'``, ``'align_single_space_minimal'``, ``'no_space'``, ``'single_space'``, ``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. ``>>``, ``>>=``, ``^=``, ``**``, ``**=``, ``<=>``, ``??``, ``??=``
  20. Allowed types: ``array``
  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. ];
  108. Example #8
  109. ~~~~~~~~~~
  110. With configuration: ``['operators' => ['=>' => 'align_single_space']]``.
  111. .. code-block:: diff
  112. --- Original
  113. +++ New
  114. <?php
  115. $array = [
  116. - "foo" => 12,
  117. + "foo" => 12,
  118. "baaaaaaaaaaar" => 13,
  119. ];
  120. Example #9
  121. ~~~~~~~~~~
  122. With configuration: ``['operators' => ['=>' => 'align_single_space_minimal']]``.
  123. .. code-block:: diff
  124. --- Original
  125. +++ New
  126. <?php
  127. $array = [
  128. - "foo" => 12,
  129. - "baaaaaaaaaaar" => 13,
  130. + "foo" => 12,
  131. + "baaaaaaaaaaar" => 13,
  132. ];
  133. Rule sets
  134. ---------
  135. The rule is part of the following rule sets:
  136. @PhpCsFixer
  137. Using the `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ rule set will enable the ``binary_operator_spaces`` rule with the default config.
  138. @Symfony
  139. Using the `@Symfony <./../../ruleSets/Symfony.rst>`_ rule set will enable the ``binary_operator_spaces`` rule with the default config.