date_time_immutable.rst 922 B

1234567891011121314151617181920212223242526272829303132333435
  1. ============================
  2. Rule ``date_time_immutable``
  3. ============================
  4. Class ``DateTimeImmutable`` should be used instead of ``DateTime``.
  5. Warning
  6. -------
  7. Using this rule is risky
  8. ~~~~~~~~~~~~~~~~~~~~~~~~
  9. Risky when the code relies on modifying ``DateTime`` objects or if any of the
  10. ``date_create*`` functions are overridden.
  11. Examples
  12. --------
  13. Example #1
  14. ~~~~~~~~~~
  15. .. code-block:: diff
  16. --- Original
  17. +++ New
  18. <?php
  19. -new DateTime();
  20. +new DateTimeImmutable();
  21. References
  22. ----------
  23. - Fixer class: `PhpCsFixer\\Fixer\\ClassUsage\\DateTimeImmutableFixer <./../../../src/Fixer/ClassUsage/DateTimeImmutableFixer.php>`_
  24. - Test class: `PhpCsFixer\\Tests\\Fixer\\ClassUsage\\DateTimeImmutableFixerTest <./../../../tests/Fixer/ClassUsage/DateTimeImmutableFixerTest.php>`_
  25. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.