123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- ===============================
- Rule ``binary_operator_spaces``
- ===============================
- Binary operators should be surrounded by space as configured.
- Configuration
- -------------
- ``default``
- ~~~~~~~~~~~
- Default fix strategy.
- 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``
- Default value: ``'single_space'``
- ``operators``
- ~~~~~~~~~~~~~
- Dictionary of ``binary operator`` => ``fix strategy`` values that differ from
- the default strategy. Supported are: ``=``, ``*``, ``/``, ``%``, ``<``, ``>``,
- ``|``, ``^``, ``+``, ``-``, ``&``, ``&=``, ``&&``, ``||``, ``.=``, ``/=``,
- ``=>``, ``==``, ``>=``, ``===``, ``!=``, ``<>``, ``!==``, ``<=``, ``and``,
- ``or``, ``xor``, ``-=``, ``%=``, ``*=``, ``|=``, ``+=``, ``<<``, ``<<=``,
- ``>>``, ``>>=``, ``^=``, ``**``, ``**=``, ``<=>``, ``??`` and ``??=``.
- Allowed types: ``array<string, ?string>``
- Default value: ``[]``
- Examples
- --------
- Example #1
- ~~~~~~~~~~
- *Default* configuration.
- .. code-block:: diff
- --- Original
- +++ New
- <?php
- -$a= 1 + $b^ $d !== $e or $f;
- +$a = 1 + $b ^ $d !== $e or $f;
- Example #2
- ~~~~~~~~~~
- With configuration: ``['operators' => ['=' => 'align', 'xor' => null]]``.
- .. code-block:: diff
- --- Original
- +++ New
- <?php
- $aa= 1;
- -$b=2;
- +$b =2;
- $c = $d xor $e;
- -$f -= 1;
- +$f -= 1;
- Example #3
- ~~~~~~~~~~
- With configuration: ``['operators' => ['+=' => 'align_single_space']]``.
- .. code-block:: diff
- --- Original
- +++ New
- <?php
- -$a = $b +=$c;
- -$d = $ee+=$f;
- +$a = $b += $c;
- +$d = $ee += $f;
- -$g = $b +=$c;
- -$h = $ee+=$f;
- +$g = $b += $c;
- +$h = $ee += $f;
- Example #4
- ~~~~~~~~~~
- With configuration: ``['operators' => ['===' => 'align_single_space_minimal']]``.
- .. code-block:: diff
- --- Original
- +++ New
- <?php
- -$a = $b===$c;
- -$d = $f === $g;
- -$h = $i=== $j;
- +$a = $b === $c;
- +$d = $f === $g;
- +$h = $i === $j;
- Example #5
- ~~~~~~~~~~
- With configuration: ``['operators' => ['|' => 'no_space']]``.
- .. code-block:: diff
- --- Original
- +++ New
- <?php
- -$foo = \json_encode($bar, JSON_PRESERVE_ZERO_FRACTION | JSON_PRETTY_PRINT);
- +$foo = \json_encode($bar, JSON_PRESERVE_ZERO_FRACTION|JSON_PRETTY_PRINT);
- Example #6
- ~~~~~~~~~~
- With configuration: ``['operators' => ['=>' => 'single_space']]``.
- .. code-block:: diff
- --- Original
- +++ New
- <?php
- $array = [
- - "foo" => 1,
- - "baaaaaaaaaaar" => 11,
- + "foo" => 1,
- + "baaaaaaaaaaar" => 11,
- ];
- Example #7
- ~~~~~~~~~~
- With configuration: ``['operators' => ['=>' => 'align']]``.
- .. code-block:: diff
- --- Original
- +++ New
- <?php
- $array = [
- - "foo" => 12,
- + "foo" => 12,
- "baaaaaaaaaaar" => 13,
- "baz" => 1,
- ];
- Example #8
- ~~~~~~~~~~
- With configuration: ``['operators' => ['=>' => 'align_by_scope']]``.
- .. code-block:: diff
- --- Original
- +++ New
- <?php
- $array = [
- - "foo" => 12,
- + "foo" => 12,
- "baaaaaaaaaaar" => 13,
- - "baz" => 1,
- + "baz" => 1,
- ];
- Example #9
- ~~~~~~~~~~
- With configuration: ``['operators' => ['=>' => 'align_single_space']]``.
- .. code-block:: diff
- --- Original
- +++ New
- <?php
- $array = [
- - "foo" => 12,
- + "foo" => 12,
- "baaaaaaaaaaar" => 13,
- "baz" => 1,
- ];
- Example #10
- ~~~~~~~~~~~
- With configuration: ``['operators' => ['=>' => 'align_single_space_by_scope']]``.
- .. code-block:: diff
- --- Original
- +++ New
- <?php
- $array = [
- - "foo" => 12,
- + "foo" => 12,
- "baaaaaaaaaaar" => 13,
- - "baz" => 1,
- + "baz" => 1,
- ];
- Example #11
- ~~~~~~~~~~~
- With configuration: ``['operators' => ['=>' => 'align_single_space_minimal']]``.
- .. code-block:: diff
- --- Original
- +++ New
- <?php
- $array = [
- - "foo" => 12,
- - "baaaaaaaaaaar" => 13,
- + "foo" => 12,
- + "baaaaaaaaaaar" => 13,
- "baz" => 1,
- ];
- Example #12
- ~~~~~~~~~~~
- With configuration: ``['operators' => ['=>' => 'align_single_space_minimal_by_scope']]``.
- .. code-block:: diff
- --- Original
- +++ New
- <?php
- $array = [
- - "foo" => 12,
- - "baaaaaaaaaaar" => 13,
- + "foo" => 12,
- + "baaaaaaaaaaar" => 13,
- - "baz" => 1,
- + "baz" => 1,
- ];
- Rule sets
- ---------
- The rule is part of the following rule sets:
- - `@PER <./../../ruleSets/PER.rst>`_ with config:
- ``['default' => 'at_least_single_space']``
- - `@PER-CS <./../../ruleSets/PER-CS.rst>`_ with config:
- ``['default' => 'at_least_single_space']``
- - `@PER-CS1.0 <./../../ruleSets/PER-CS1.0.rst>`_ with config:
- ``['default' => 'at_least_single_space']``
- - `@PER-CS2.0 <./../../ruleSets/PER-CS2.0.rst>`_ with config:
- ``['default' => 'at_least_single_space']``
- - `@PSR12 <./../../ruleSets/PSR12.rst>`_ with config:
- ``['default' => 'at_least_single_space']``
- - `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_
- - `@Symfony <./../../ruleSets/Symfony.rst>`_
- References
- ----------
- - Fixer class: `PhpCsFixer\\Fixer\\Operator\\BinaryOperatorSpacesFixer <./../../../src/Fixer/Operator/BinaryOperatorSpacesFixer.php>`_
- - Test class: `PhpCsFixer\\Tests\\Fixer\\Operator\\BinaryOperatorSpacesFixerTest <./../../../tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php>`_
- The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.
|