ternary_to_elvis_operator.rst 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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
  31. Using the `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_ rule set will enable the ``ternary_to_elvis_operator`` rule.
  32. @Symfony:risky
  33. Using the `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_ rule set will enable the ``ternary_to_elvis_operator`` rule.