Skip to content

UnqualifiedReference ​

info off by default quick-fix PHP ≥ 7.0

Group: Performance · PhpStorm name: UnqualifiedReferenceInspection

Inside a namespace, an unqualified call such as strlen($s) must be resolved at run time (namespace first, then global). Writing \strlen($s) lets the PHP 7+ compiler bind it at compile time and, for a set of special functions, replace the call by a dedicated opcode. The same holds for global constants and for function names passed as string callbacks.

Example ​

With REPORT_ALL_FUNCTIONS = true, REPORT_CONSTANTS = true, level 7.1:

php
<?php
namespace {
    function legacy_hook() {}
}

namespace Shop\Cart {
    use function array_sum;
    use const E_ALL;

    function money($v) { return $v; }
    const SCALE = 2;

    $n = count([1]);
    $u = ucfirst('x');
    $e = PHP_EOL;
    \array_map('legacy_hook', []);
    \array_walk($list, "legacy_hook");

    $ok = \count([1]) + money(1) + SCALE + array_sum([]) + E_ALL;
    $ok2 = true && null === __LINE__;
    \array_map('\legacy_hook', []);
    \array_map('Helper::run', []);
    \usort($list, 'legacy_hook');
}
php
<?php
namespace {
    function legacy_hook() {}
}

namespace Shop\Cart {
    use function array_sum;
    use const E_ALL;

    function money($v) { return $v; }
    const SCALE = 2;

    $n = \count([1]);
    $u = \ucfirst('x');
    $e = \PHP_EOL;
    \array_map('\legacy_hook', []);
    \array_walk($list, "\\legacy_hook");

    $ok = \count([1]) + money(1) + SCALE + array_sum([]) + E_ALL;
    $ok2 = true && null === __LINE__;
    \array_map('\legacy_hook', []);
    \array_map('Helper::run', []);
    \usort($list, 'legacy_hook');
}

Reported:

  • line 13: Write '\count(...)' to allow compile-time binding.
  • line 14: Write '\ucfirst(...)' to allow compile-time binding.
  • line 15: Write '\PHP_EOL' to allow compile-time binding.
  • line 16: Write '\legacy_hook' to allow compile-time binding.
  • line 17: Write '\legacy_hook' to allow compile-time binding.

Options ​

OptionTypeDefaultEffect
REPORT_ALL_FUNCTIONSboolfalseReport every unqualified global function call (and every callback name), not only those in the opcode list.
REPORT_CONSTANTSboolfalseAlso report unqualified references to global constants.

The rule is disabled by default.

Configure ​

In custos.json:

json
{
  "rules": {
    "UnqualifiedReference": {
      "enabled": true,
      "options": {
        "REPORT_ALL_FUNCTIONS": false,
        "REPORT_CONSTANTS": false
      }
    }
  }
}

Suppress ​

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

php
// @custos-ignore UnqualifiedReference

/**
 * @noinspection UnqualifiedReferenceInspection
 */

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