StrEndsWithCanBeUsed
info on by default quick-fix PHP ≥ 8.0Group: Language level migration · PhpStorm name: StrEndsWithCanBeUsedInspection
Cutting the tail off a string with substr($h, -strlen($n)) and comparing it to $n is the pre-PHP 8 "ends with" idiom. PHP 8 has str_ends_with(), which is clearer and avoids building a temporary string.
Example
php
<?php
function isType(string $file, string $ext): array {
return [
substr($file, -strlen($ext)) === $ext,
'.gz' === mb_substr($file, - mb_strlen('.gz')),
substr(basename($file), -mb_strlen($ext)) !== $ext,
substr($file, -strlen($ext)) === '.txt',
substr($file, -strlen($ext), 2) === $ext,
substr($file, -3) === $ext,
mb_substr($file, -mb_strlen($ext, 'UTF-8')) === $ext,
];
}php
<?php
function isType(string $file, string $ext): array {
return [
str_ends_with($file, $ext),
str_ends_with($file, '.gz'),
!str_ends_with(basename($file), $ext),
substr($file, -strlen($ext)) === '.txt',
substr($file, -strlen($ext), 2) === $ext,
substr($file, -3) === $ext,
mb_substr($file, -mb_strlen($ext, 'UTF-8')) === $ext,
];
}Reported:
- line 4: Replace with 'str_ends_with($file, $ext)'.
- line 5: Replace with 'str_ends_with($file, '.gz')'.
- line 6: Replace with '!str_ends_with(basename($file), $ext)'.
Configure
In custos.json:
json
{
"rules": {
"StrEndsWithCanBeUsed": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore StrEndsWithCanBeUsed
/**
* @noinspection StrEndsWithCanBeUsedInspection
*/