123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- ===================
- Rule ``yoda_style``
- ===================
- Write conditions in Yoda style (``true``), non-Yoda style (``['equal' => false,
- 'identical' => false, 'less_and_greater' => false]``) or ignore those conditions
- (``null``) based on configuration.
- Configuration
- -------------
- ``always_move_variable``
- ~~~~~~~~~~~~~~~~~~~~~~~~
- Whether variables should always be on non assignable side when applying Yoda
- style.
- Allowed types: ``bool``
- Default value: ``false``
- ``equal``
- ~~~~~~~~~
- Style for equal (``==``, ``!=``) statements.
- Allowed types: ``bool`` and ``null``
- Default value: ``true``
- ``identical``
- ~~~~~~~~~~~~~
- Style for identical (``===``, ``!==``) statements.
- Allowed types: ``bool`` and ``null``
- Default value: ``true``
- ``less_and_greater``
- ~~~~~~~~~~~~~~~~~~~~
- Style for less and greater than (``<``, ``<=``, ``>``, ``>=``) statements.
- Allowed types: ``bool`` and ``null``
- Default value: ``null``
- Examples
- --------
- Example #1
- ~~~~~~~~~~
- *Default* configuration.
- .. code-block:: diff
- --- Original
- +++ New
- <?php
- - if ($a === null) {
- + if (null === $a) {
- echo "null";
- }
- Example #2
- ~~~~~~~~~~
- With configuration: ``['equal' => true, 'identical' => false, 'less_and_greater' => null]``.
- .. code-block:: diff
- --- Original
- +++ New
- <?php
- - $b = $c != 1; // equal
- - $a = 1 === $b; // identical
- + $b = 1 != $c; // equal
- + $a = $b === 1; // identical
- $c = $c > 3; // less than
- Example #3
- ~~~~~~~~~~
- With configuration: ``['always_move_variable' => true]``.
- .. code-block:: diff
- --- Original
- +++ New
- <?php
- -return $foo === count($bar);
- +return count($bar) === $foo;
- Example #4
- ~~~~~~~~~~
- With configuration: ``['equal' => false, 'identical' => false, 'less_and_greater' => false]``.
- .. code-block:: diff
- --- Original
- +++ New
- <?php
- // Enforce non-Yoda style.
- - if (null === $a) {
- + if ($a === null) {
- echo "null";
- }
- Rule sets
- ---------
- The rule is part of the following rule sets:
- - `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_
- - `@Symfony <./../../ruleSets/Symfony.rst>`_
- References
- ----------
- - Fixer class: `PhpCsFixer\\Fixer\\ControlStructure\\YodaStyleFixer <./../../../src/Fixer/ControlStructure/YodaStyleFixer.php>`_
- - Test class: `PhpCsFixer\\Tests\\Fixer\\ControlStructure\\YodaStyleFixerTest <./../../../tests/Fixer/ControlStructure/YodaStyleFixerTest.php>`_
- The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.
|