Skip to content

IsEmptyFunctionUsage ​

info on by default quick-fix

Group: 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 ​

OptionTypeDefaultEffect
REPORT_EMPTY_USAGEboolfalseEnables the generic report D3.
SUGGEST_TO_USE_COUNT_CHECKboolfalseEnables D1 (count comparison for arrays/Countable).
SUGGEST_TO_USE_NULL_COMPARISONbooltrueEnables D2 (null comparison) as a whole.
SUGGEST_NULL_COMPARISON_FOR_SCALARSbooltrueEnables 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
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.