Skip to content

GetClassUsage ​

warning on by default PHP ≥ 7.1

Group: Language level migration · PhpStorm name: GetClassUsageInspection

Since PHP 7.2, passing null to get_class() is an error-level misuse (it no longer silently falls back to the calling class). This rule flags get_class(x) calls whose single argument may be null and that are not preceded by any null check of that same expression in the enclosing function.

Example ​

Language level 7.4:

php
<?php

function describe($entity = null, $label = 'x', ?Order $order = null, Invoice $invoice = NULL) {
    \get_class(NULL);
    get_class($entity);
    get_class($order);

    get_class($label);
    get_class($entity, 1);

    if (empty($invoice)) {
        return;
    }
    get_class($invoice);

    while ($order instanceof Order) {
        get_class($order);
    }
}

function report(?Order $first, ?Order $second) {
    $tag = $first ? 'a' : 'b';
    echo get_class($first);
    echo get_class($second);
    if (null != $second) {}
}

$loose = null;
get_class($loose);

Reported:

  • line 4: get_class() rejects null on PHP 7.2+; guard the argument.
  • line 5: get_class() rejects null on PHP 7.2+; guard the argument.
  • line 6: get_class() rejects null on PHP 7.2+; guard the argument.
  • line 24: get_class() rejects null on PHP 7.2+; guard the argument.
  • line 29: get_class() rejects null on PHP 7.2+; guard the argument.

Configure ​

In custos.json:

json
{
  "rules": {
    "GetClassUsage": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore GetClassUsage

/**
 * @noinspection GetClassUsageInspection
 */

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