modernize_strpos.rst 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. =========================
  2. Rule ``modernize_strpos``
  3. =========================
  4. Replace ``strpos()`` and ``stripos()`` calls with ``str_starts_with()`` or
  5. ``str_contains()`` if possible.
  6. Warning
  7. -------
  8. Using this rule is risky
  9. ~~~~~~~~~~~~~~~~~~~~~~~~
  10. Risky if ``strpos``, ``stripos``, ``str_starts_with``, ``str_contains`` or
  11. ``strtolower`` functions are overridden.
  12. Configuration
  13. -------------
  14. ``modernize_stripos``
  15. ~~~~~~~~~~~~~~~~~~~~~
  16. Whether to modernize ``stripos`` calls as well.
  17. Allowed types: ``bool``
  18. Default value: ``false``
  19. Examples
  20. --------
  21. Example #1
  22. ~~~~~~~~~~
  23. *Default* configuration.
  24. .. code-block:: diff
  25. --- Original
  26. +++ New
  27. <?php
  28. -if (strpos($haystack, $needle) === 0) {}
  29. -if (strpos($haystack, $needle) !== 0) {}
  30. -if (strpos($haystack, $needle) !== false) {}
  31. -if (strpos($haystack, $needle) === false) {}
  32. +if (str_starts_with($haystack, $needle) ) {}
  33. +if (!str_starts_with($haystack, $needle) ) {}
  34. +if (str_contains($haystack, $needle) ) {}
  35. +if (!str_contains($haystack, $needle) ) {}
  36. Example #2
  37. ~~~~~~~~~~
  38. With configuration: ``['modernize_stripos' => true]``.
  39. .. code-block:: diff
  40. --- Original
  41. +++ New
  42. <?php
  43. -if (strpos($haystack, $needle) === 0) {}
  44. -if (strpos($haystack, $needle) !== 0) {}
  45. -if (strpos($haystack, $needle) !== false) {}
  46. -if (strpos($haystack, $needle) === false) {}
  47. -if (stripos($haystack, $needle) === 0) {}
  48. -if (stripos($haystack, $needle) !== 0) {}
  49. -if (stripos($haystack, $needle) !== false) {}
  50. -if (stripos($haystack, $needle) === false) {}
  51. +if (str_starts_with($haystack, $needle) ) {}
  52. +if (!str_starts_with($haystack, $needle) ) {}
  53. +if (str_contains($haystack, $needle) ) {}
  54. +if (!str_contains($haystack, $needle) ) {}
  55. +if (str_starts_with(strtolower($haystack), strtolower($needle)) ) {}
  56. +if (!str_starts_with(strtolower($haystack), strtolower($needle)) ) {}
  57. +if (str_contains(strtolower($haystack), strtolower($needle)) ) {}
  58. +if (!str_contains(strtolower($haystack), strtolower($needle)) ) {}
  59. Rule sets
  60. ---------
  61. The rule is part of the following rule sets:
  62. - `@PHP80Migration:risky <./../../ruleSets/PHP80MigrationRisky.rst>`_
  63. - `@PHP82Migration:risky <./../../ruleSets/PHP82MigrationRisky.rst>`_
  64. - `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_
  65. References
  66. ----------
  67. - Fixer class: `PhpCsFixer\\Fixer\\Alias\\ModernizeStrposFixer <./../../../src/Fixer/Alias/ModernizeStrposFixer.php>`_
  68. - Test class: `PhpCsFixer\\Tests\\Fixer\\Alias\\ModernizeStrposFixerTest <./../../../tests/Fixer/Alias/ModernizeStrposFixerTest.php>`_
  69. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.