ternary_to_elvis_operator.rst 908 B

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