StrlenInEmptyStringCheckContext
info on by default quick-fixGroup: Control flow · PhpStorm name: StrlenInEmptyStringCheckContextInspection
Measuring a string's length only to know whether it is empty (strlen($s) > 0, !strlen($s), if (strlen($s))) is indirect and slower than comparing with the empty string. Suggest an identity comparison with '' instead, casting to string when the value is not known to be a string.
Example
php
<?php
class Form
{
public function check(string $name, $id, int $n)
{
$a = strlen($name) > 0;
$b = !mb_strlen($id);
$c = strlen($n) != 0;
$d = $a || \strlen($name);
do {} while (strlen($name) == 0);
$f = fn($s) => strlen($s) !== 0;
return [$a, $b, $c, $d, $f];
}
}php
<?php
class Form
{
public function check(string $name, $id, int $n)
{
$a = $name !== '';
$b = !mb_strlen($id);
$c = (string)$n !== '';
$d = $a || $name !== '';
do {} while ($name === '');
$f = fn($s) => (string)$s !== '';
return [$a, $b, $c, $d, $f];
}
}Reported:
- line 6: Compare with an empty string instead: '$name !== '''.
- line 8: Compare with an empty string instead: '(string)$n !== '''.
- line 9: Compare with an empty string instead: '$name !== '''.
- line 10: Compare with an empty string instead: '$name === '''.
- line 11: Compare with an empty string instead: '(string)$s !== '''.
Options
None. Operand order follows the global comparison style (regular default / yoda).
Configure
In custos.json:
json
{
"rules": {
"StrlenInEmptyStringCheckContext": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore StrlenInEmptyStringCheckContext
/**
* @noinspection StrlenInEmptyStringCheckContextInspection
*/