Fork me on GitHub
a Sensio Labs Product

7th Gear (v3.59.3) edition

Rule modernize_strpos

Replace strpos() calls with str_starts_with() or str_contains() if possible.


Using this rule is risky

Risky if strpos, str_starts_with or str_contains functions are overridden.


Example #1

--- Original
+++ New
-if (strpos($haystack, $needle) === 0) {}
-if (strpos($haystack, $needle) !== 0) {}
-if (strpos($haystack, $needle) !== false) {}
-if (strpos($haystack, $needle) === false) {}
+if (str_starts_with($haystack, $needle)  ) {}
+if (!str_starts_with($haystack, $needle)  ) {}
+if (str_contains($haystack, $needle)  ) {}
+if (!str_contains($haystack, $needle)  ) {}

Rule sets

The rule is part of the following rule sets:


The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.