ternary_to_elvis_operator.rst 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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>`_
  32. References
  33. ----------
  34. - Fixer class: `PhpCsFixer\\Fixer\\Operator\\TernaryToElvisOperatorFixer <./../../../src/Fixer/Operator/TernaryToElvisOperatorFixer.php>`_
  35. - Test class: `PhpCsFixer\\Tests\\Fixer\\Operator\\TernaryToElvisOperatorFixerTest <./../../../tests/Fixer/Operator/TernaryToElvisOperatorFixerTest.php>`_
  36. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.