modernize_strpos.rst 1005 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. - `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_