IsEmptyFunctionUsage
info on by default quick-fixGroup: Code style · PhpStorm name: IsEmptyFunctionUsageInspection
empty() treats many unrelated values (0, '0', '', [], null, false) as "empty". When the argument's type is known, a precise check is clearer: count(...) === 0 for countables, ... === null for nullable scalars/objects. Optionally, any other empty() use can be flagged.
Example
All four options on, regular style:
php
<?php
function tally(array $rows, \SplObjectStorage $seen)
{
if (empty($rows)) {}
if (!empty($seen)) {}
if (empty(($rows))) {}
}
/**
* @param float|null $ratio
* @param true|null $flag
* @param string|null $label
*/
function probe($ratio, $flag, $label, ?\DateTime $when, \stdClass $box, $map)
{
return [
empty($ratio),
!empty($flag),
empty($when),
empty($box),
empty($label),
!empty($label),
empty(0),
empty($map['key']),
];
}
class Node
{
/** @var Node|null */
public $next;
/** @var string */
public $name;
}
function walk(Node $n)
{
return [
empty($n->next),
empty($n->next->next),
empty($n->name),
];
}php
<?php
function tally(array $rows, \SplObjectStorage $seen)
{
if (count($rows) === 0) {}
if (count($seen) !== 0) {}
if (count($rows) === 0) {}
}
/**
* @param float|null $ratio
* @param true|null $flag
* @param string|null $label
*/
function probe($ratio, $flag, $label, ?\DateTime $when, \stdClass $box, $map)
{
return [
$ratio === null,
$flag !== null,
$when === null,
$box === null,
empty($label),
!empty($label),
empty(0),
empty($map['key']),
];
}
class Node
{
/** @var Node|null */
public $next;
/** @var string */
public $name;
}
function walk(Node $n)
{
return [
empty($n->next),
empty($n->next->next),
empty($n->name),
];
}Reported:
- line 5: Replace with 'count($rows) === 0'.
- line 6: Replace with 'count($seen) !== 0'.
- line 7: Replace with 'count($rows) === 0'.
- line 18: Replace with '$ratio === null'.
- line 19: Replace with '$flag !== null'.
- line 20: Replace with '$when === null'.
- line 21: Replace with '$box === null'.
- line 22: Prefer a type-specific check over empty().
- line 23: Prefer a type-specific check over empty().
- line 24: Prefer a type-specific check over empty().
- line 42: Prefer a type-specific check over empty().
Options
| Option | Type | Default | Effect |
|---|---|---|---|
| REPORT_EMPTY_USAGE | bool | false | Enables the generic report D3. |
| SUGGEST_TO_USE_COUNT_CHECK | bool | false | Enables D1 (count comparison for arrays/Countable). |
| SUGGEST_TO_USE_NULL_COMPARISON | bool | true | Enables D2 (null comparison) as a whole. |
| SUGGEST_NULL_COMPARISON_FOR_SCALARS | bool | true | Enables D2a (nullable int/float/bool/resource); D2b (objects) only needs the previous option. |
Global setting: comparison style (regular default / yoda) selects the operand order of F1/F2.
Configure
In custos.json:
json
{
"rules": {
"IsEmptyFunctionUsage": {
"enabled": false,
"options": {
"REPORT_EMPTY_USAGE": false,
"SUGGEST_TO_USE_COUNT_CHECK": false,
"SUGGEST_TO_USE_NULL_COMPARISON": true,
"SUGGEST_NULL_COMPARISON_FOR_SCALARS": true
}
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore IsEmptyFunctionUsage
/**
* @noinspection IsEmptyFunctionUsageInspection
*/