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, !mb_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
Yoda style:
php
<?php
function probe(string $name, ?string $alias, int $count, $raw, array $bag)
{
if (strlen($name)) {}
while (!mb_strlen($name, 'UTF-8')) {}
if (!(strlen($alias))) {}
$a = mb_strlen($raw) ? 'y' : 'n';
$b = $count > 2 and strlen(trim($raw));
$c = 0 === mb_strlen($name);
$d = strlen($count) <> 0;
$e = strlen($alias) == 0;
$f = mb_strlen($name) > 0;
$g = strlen($bag['k']) >= 1;
$h = strlen($name) < 1;
$i = 0 < strlen($name);
$j = strlen($name) > 1;
$k = (strlen($name)) > 0;
$l = strlen($name) ?: 5;
return strlen($name);
}
if (strlen($argv[1])) {}php
<?php
function probe(string $name, ?string $alias, int $count, $raw, array $bag)
{
if ('' !== $name) {}
while ('' === $name) {}
if (!('' !== (string)$alias)) {}
$a = '' !== (string)$raw ? 'y' : 'n';
$b = $count > 2 and '' !== trim($raw);
$c = '' === $name;
$d = '' !== (string)$count;
$e = '' === (string)$alias;
$f = '' !== $name;
$g = '' !== (string)$bag['k'];
$h = '' === $name;
$i = 0 < strlen($name);
$j = strlen($name) > 1;
$k = (strlen($name)) > 0;
$l = strlen($name) ?: 5;
return strlen($name);
}
if ('' !== (string)$argv[1]) {}Reported:
- line 4: Compare with an empty string instead: ''' !== $name'.
- line 5: Compare with an empty string instead: ''' === $name'.
- line 6: Compare with an empty string instead: ''' !== (string)$alias'.
- line 7: Compare with an empty string instead: ''' !== (string)$raw'.
- line 8: Compare with an empty string instead: ''' !== trim($raw)'.
- line 10: Compare with an empty string instead: ''' === $name'.
- line 11: Compare with an empty string instead: ''' !== (string)$count'.
- line 12: Compare with an empty string instead: ''' === (string)$alias'.
- line 13: Compare with an empty string instead: ''' !== $name'.
- line 14: Compare with an empty string instead: ''' !== (string)$bag['k']'.
- line 15: Compare with an empty string instead: ''' === $name'.
- line 24: Compare with an empty string instead: ''' !== (string)$argv[1]'.
Options
| Option | Type | Default | Effect |
|---|---|---|---|
| (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
*/