no_unset_on_property.rst 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. =============================
  2. Rule ``no_unset_on_property``
  3. =============================
  4. Properties should be set to ``null`` instead of using ``unset``.
  5. Warning
  6. -------
  7. Using this rule is risky
  8. ~~~~~~~~~~~~~~~~~~~~~~~~
  9. Risky when relying on attributes to be removed using ``unset`` rather than be
  10. set to ``null``. Changing variables to ``null`` instead of unsetting means these
  11. still show up when looping over class variables and reference properties remain
  12. unbroken. With PHP 7.4, this rule might introduce ``null`` assignments to
  13. properties whose type declaration does not allow it.
  14. Examples
  15. --------
  16. Example #1
  17. ~~~~~~~~~~
  18. .. code-block:: diff
  19. --- Original
  20. +++ New
  21. <?php
  22. -unset($this->a);
  23. +$this->a = null;
  24. Rule sets
  25. ---------
  26. The rule is part of the following rule set:
  27. - `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_
  28. References
  29. ----------
  30. - Fixer class: `PhpCsFixer\\Fixer\\LanguageConstruct\\NoUnsetOnPropertyFixer <./../../../src/Fixer/LanguageConstruct/NoUnsetOnPropertyFixer.php>`_
  31. - Test class: `PhpCsFixer\\Tests\\Fixer\\LanguageConstruct\\NoUnsetOnPropertyFixerTest <./../../../tests/Fixer/LanguageConstruct/NoUnsetOnPropertyFixerTest.php>`_
  32. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.