ternary_to_elvis_operator.rst 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. ==================================
  2. Rule ``ternary_to_elvis_operator``
  3. ==================================
  4. Use the Elvis operator ``?:`` where possible.
  5. .. warning:: Using this rule is risky.
  6. Risky when relying on functions called on both sides of the ``?`` operator.
  7. Examples
  8. --------
  9. Example #1
  10. ~~~~~~~~~~
  11. .. code-block:: diff
  12. --- Original
  13. +++ New
  14. @@ -1,2 +1,2 @@
  15. <?php
  16. -$foo = $foo ? $foo : 1;
  17. +$foo = $foo ? : 1;
  18. Example #2
  19. ~~~~~~~~~~
  20. .. code-block:: diff
  21. --- Original
  22. +++ New
  23. @@ -1 +1 @@
  24. -<?php $foo = $bar[a()] ? $bar[a()] : 1; # "risky" sample, "a()" only gets called once after fixing
  25. +<?php $foo = $bar[a()] ? : 1; # "risky" sample, "a()" only gets called once after fixing
  26. Rule sets
  27. ---------
  28. The rule is part of the following rule sets:
  29. @PhpCsFixer:risky
  30. Using the `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_ rule set will enable the ``ternary_to_elvis_operator`` rule.
  31. @Symfony:risky
  32. Using the `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_ rule set will enable the ``ternary_to_elvis_operator`` rule.