yoda_style.rst 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ===================
  2. Rule ``yoda_style``
  3. ===================
  4. Write conditions in Yoda style (``true``), non-Yoda style (``false``) or ignore
  5. those conditions (``null``) based on configuration.
  6. Configuration
  7. -------------
  8. ``equal``
  9. ~~~~~~~~~
  10. Style for equal (``==``, ``!=``) statements.
  11. Allowed types: ``bool``, ``null``
  12. Default value: ``true``
  13. ``identical``
  14. ~~~~~~~~~~~~~
  15. Style for identical (``===``, ``!==``) statements.
  16. Allowed types: ``bool``, ``null``
  17. Default value: ``true``
  18. ``less_and_greater``
  19. ~~~~~~~~~~~~~~~~~~~~
  20. Style for less and greater than (``<``, ``<=``, ``>``, ``>=``) statements.
  21. Allowed types: ``bool``, ``null``
  22. Default value: ``null``
  23. ``always_move_variable``
  24. ~~~~~~~~~~~~~~~~~~~~~~~~
  25. Whether variables should always be on non assignable side when applying Yoda
  26. style.
  27. Allowed types: ``bool``
  28. Default value: ``false``
  29. Examples
  30. --------
  31. Example #1
  32. ~~~~~~~~~~
  33. *Default* configuration.
  34. .. code-block:: diff
  35. --- Original
  36. +++ New
  37. @@ -1,4 +1,4 @@
  38. <?php
  39. - if ($a === null) {
  40. + if (null === $a) {
  41. echo "null";
  42. }
  43. Example #2
  44. ~~~~~~~~~~
  45. With configuration: ``['equal' => true, 'identical' => false, 'less_and_greater' => null]``.
  46. .. code-block:: diff
  47. --- Original
  48. +++ New
  49. @@ -1,4 +1,4 @@
  50. <?php
  51. - $b = $c != 1; // equal
  52. - $a = 1 === $b; // identical
  53. + $b = 1 != $c; // equal
  54. + $a = $b === 1; // identical
  55. $c = $c > 3; // less than
  56. Example #3
  57. ~~~~~~~~~~
  58. With configuration: ``['always_move_variable' => true]``.
  59. .. code-block:: diff
  60. --- Original
  61. +++ New
  62. @@ -1,2 +1,2 @@
  63. <?php
  64. -return $foo === count($bar);
  65. +return count($bar) === $foo;
  66. Rule sets
  67. ---------
  68. The rule is part of the following rule sets:
  69. @Symfony
  70. Using the ``@Symfony`` rule set will enable the ``yoda_style`` rule with the default config.
  71. @PhpCsFixer
  72. Using the ``@PhpCsFixer`` rule set will enable the ``yoda_style`` rule with the default config.