ternary_to_elvis_operator.rst 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. <?php
  15. -$foo = $foo ? $foo : 1;
  16. +$foo = $foo ? : 1;
  17. Example #2
  18. ~~~~~~~~~~
  19. .. code-block:: diff
  20. --- Original
  21. +++ New
  22. -<?php $foo = $bar[a()] ? $bar[a()] : 1; # "risky" sample, "a()" only gets called once after fixing
  23. +<?php $foo = $bar[a()] ? : 1; # "risky" sample, "a()" only gets called once after fixing
  24. Rule sets
  25. ---------
  26. The rule is part of the following rule sets:
  27. @PhpCsFixer:risky
  28. Using the `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_ rule set will enable the ``ternary_to_elvis_operator`` rule.
  29. @Symfony:risky
  30. Using the `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_ rule set will enable the ``ternary_to_elvis_operator`` rule.