modernize_strpos.rst 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. =========================
  2. Rule ``modernize_strpos``
  3. =========================
  4. Replace ``strpos()`` calls with ``str_starts_with()`` or ``str_contains()`` if
  5. possible.
  6. Warning
  7. -------
  8. Using this rule is risky
  9. ~~~~~~~~~~~~~~~~~~~~~~~~
  10. Risky if ``strpos``, ``str_starts_with`` or ``str_contains`` functions are
  11. overridden.
  12. Examples
  13. --------
  14. Example #1
  15. ~~~~~~~~~~
  16. .. code-block:: diff
  17. --- Original
  18. +++ New
  19. <?php
  20. -if (strpos($haystack, $needle) === 0) {}
  21. -if (strpos($haystack, $needle) !== 0) {}
  22. -if (strpos($haystack, $needle) !== false) {}
  23. -if (strpos($haystack, $needle) === false) {}
  24. +if (str_starts_with($haystack, $needle) ) {}
  25. +if (!str_starts_with($haystack, $needle) ) {}
  26. +if (str_contains($haystack, $needle) ) {}
  27. +if (!str_contains($haystack, $needle) ) {}
  28. Rule sets
  29. ---------
  30. The rule is part of the following rule sets:
  31. - `@PHP80Migration:risky <./../../ruleSets/PHP80MigrationRisky.rst>`_
  32. - `@PHP82Migration:risky <./../../ruleSets/PHP82MigrationRisky.rst>`_
  33. - `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_
  34. References
  35. ----------
  36. - Fixer class: `PhpCsFixer\\Fixer\\Alias\\ModernizeStrposFixer <./../../../src/Fixer/Alias/ModernizeStrposFixer.php>`_
  37. - Test class: `PhpCsFixer\\Tests\\Fixer\\Alias\\ModernizeStrposFixerTest <./../../../tests/Fixer/Alias/ModernizeStrposFixerTest.php>`_
  38. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.