string_length_to_empty.rst 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. ===============================
  2. Rule ``string_length_to_empty``
  3. ===============================
  4. String tests for empty must be done against ``''``, not with ``strlen``.
  5. Warning
  6. -------
  7. Using this rule is risky
  8. ~~~~~~~~~~~~~~~~~~~~~~~~
  9. Risky when ``strlen`` is overridden, when called using a ``stringable`` object,
  10. also no longer triggers warning when called using non-string(able).
  11. Examples
  12. --------
  13. Example #1
  14. ~~~~~~~~~~
  15. .. code-block:: diff
  16. --- Original
  17. +++ New
  18. -<?php $a = 0 === strlen($b) || \STRLEN($c) < 1;
  19. +<?php $a = '' === $b || $c === '';
  20. Rule sets
  21. ---------
  22. The rule is part of the following rule sets:
  23. - `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_
  24. - `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_
  25. References
  26. ----------
  27. - Fixer class: `PhpCsFixer\\Fixer\\StringNotation\\StringLengthToEmptyFixer <./../../../src/Fixer/StringNotation/StringLengthToEmptyFixer.php>`_
  28. - Test class: `PhpCsFixer\\Tests\\Fixer\\StringNotation\\StringLengthToEmptyFixerTest <./../../../tests/Fixer/StringNotation/StringLengthToEmptyFixerTest.php>`_
  29. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.