FixedTimeStartWith
warning off by default quick-fixGroup: Performance · PhpStorm name: FixedTimeStartWithInspection
strpos($s, 'lit') === 0 scans the whole haystack when the prefix is absent, so its cost grows with the length of $s. Comparing only the first N bytes with strncmp/strncasecmp answers the same "starts with" question in time bounded by the needle length.
Example
php
<?php
function prefixes($url, $head, $part)
{
return [
strpos($url, 'https') === 0,
0 !== stripos($head, 'Accept:'),
\strpos($url, 'C:\\') !== 0,
0 === strpos($url, 'it\'s'),
strpos($url, 'https') == 0,
strpos($url, 'https', 2) === 0,
strpos($url, $part) === 0,
strpos($url, "{$part}/") === 0,
(strpos($url, 'https')) === 0,
strpos($url, 'https') === false,
strpos($url, '') === 0,
];
}php
<?php
function prefixes($url, $head, $part)
{
return [
strncmp($url, 'https', 5) === 0,
0 !== strncasecmp($head, 'Accept:', 7),
\strncmp($url, 'C:\\', 3) !== 0,
0 === strncmp($url, 'it\'s', 4),
strpos($url, 'https') == 0,
strpos($url, 'https', 2) === 0,
strpos($url, $part) === 0,
strpos($url, "{$part}/") === 0,
(strpos($url, 'https')) === 0,
strpos($url, 'https') === false,
strpos($url, '') === 0,
];
}Reported:
- line 5: Use 'strncmp($url, 'https', 5)' for a length-independent prefix check.
- line 6: Use 'strncasecmp($head, 'Accept:', 7)' for a length-independent prefix check.
- line 7: Use '\strncmp($url, 'C:\\', 3)' for a length-independent prefix check.
- line 8: Use 'strncmp($url, 'it\'s', 4)' for a length-independent prefix check.
Configure
In custos.json:
json
{
"rules": {
"FixedTimeStartWith": {
"enabled": true
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore FixedTimeStartWith
/**
* @noinspection FixedTimeStartWithInspection
*/