Skip to content

ClassConstantCanBeUsed ​

info on by default quick-fix PHP ≥ 5.5

Group: Language level migration · PhpStorm name: ClassConstantCanBeUsedInspection

Since PHP 5.5 a class name can be obtained with Name::class. Writing class names as string literals hides them from refactoring tools and static analysis, and get_called_class() / get_parent_class() without arguments have constant-expression equivalents (static::class, parent::class).

Example ​

Options: IMPORT_CLASSES_ON_QF=true, USE_RELATIVE_QF=true, LOOK_ROOT_NS_UP=true.

php
<?php

namespace Shop\Billing;

use Shop\Billing\Invoice as Bill;
use \ArrayObject as Bag;

class Invoice {}
class Ledger {}

namespace Shop\Billing\Tax;
class Rate {}

namespace Vendor\Kit;
class Ledger {}
class Meter {}

namespace Shop\Billing;

class Clerk {
    public function names() {
        return [
            get_called_class(),
            get_parent_class(),
            '\ArrayObject',
            'ArrayObject',
            'Shop\Billing\Invoice',
            "\\Shop\\Billing\\Ledger",
            __NAMESPACE__ . '\Ledger',
            'Shop\Billing\Tax\Rate',
            'Vendor\Kit\Ledger',
            'Vendor\Kit\Meter',
            '\Vendor\Kit\Meter',
        ];
    }

    public function untouched($suffix) {
        $s  = '';
        $s .= '\ArrayObject';
        class_alias($suffix, '\Shop\Billing\Ledger');
        return [
            get_called_class($this),
            'arrayobject',
            'Bag',
            'Invoice',
            'shop\billing\invoice',
            '\ArrayObject' . $suffix,
            "\\ArrayObject{$suffix}",
            $suffix == 'Shop\Billing\Ledger',
            'Shop\Billing\Missing',
            'Not a class',
        ];
    }
}
php
<?php

namespace Shop\Billing;

use Shop\Billing\Invoice as Bill;
use \ArrayObject as Bag;
use Vendor\Kit\Meter;

class Invoice {}
class Ledger {}

namespace Shop\Billing\Tax;
class Rate {}

namespace Vendor\Kit;
class Ledger {}
class Meter {}

namespace Shop\Billing;

class Clerk {
    public function names() {
        return [
            static::class,
            get_parent_class(),
            Bag::class,
            Bag::class,
            Bill::class,
            \Shop\Billing\Ledger::class,
            Ledger::class,
            Tax\Rate::class,
            \Vendor\Kit\Ledger::class,
            Meter::class,
            Meter::class,
        ];
    }

    public function untouched($suffix) {
        $s  = '';
        $s .= '\ArrayObject';
        class_alias($suffix, '\Shop\Billing\Ledger');
        return [
            get_called_class($this),
            'arrayobject',
            'Bag',
            'Invoice',
            'shop\billing\invoice',
            '\ArrayObject' . $suffix,
            "\\ArrayObject{$suffix}",
            $suffix == 'Shop\Billing\Ledger',
            'Shop\Billing\Missing',
            'Not a class',
        ];
    }
}

Reported:

  • line 23: Use static::class instead.
  • line 25: Use \ArrayObject::class instead of the class name string.
  • line 26: Use \ArrayObject::class instead of the class name string.
  • line 27: Use \Shop\Billing\Invoice::class instead of the class name string.
  • line 28: Use \Shop\Billing\Ledger::class instead of the class name string.
  • line 29: Use Ledger::class instead of the class name string.
  • line 30: Use \Shop\Billing\Tax\Rate::class instead of the class name string.
  • line 31: Use \Vendor\Kit\Ledger::class instead of the class name string.
  • line 32: Use \Vendor\Kit\Meter::class instead of the class name string.
  • line 33: Use \Vendor\Kit\Meter::class instead of the class name string.

Options ​

OptionTypeDefaultEffect
IMPORT_CLASSES_ON_QFbooltrueFix may add a use import for namespaced classes and reference them by short name.
USE_RELATIVE_QFboolfalseWhen importing is otherwise possible, a class below the file's (first) namespace is written as a namespace-relative name instead of being imported.
LOOK_ROOT_NS_UPboolfalseAlso consider unqualified strings (no backslash), looked up as root-namespace classes ('Thing' → \Thing).

Upstream fixtures run with IMPORT_CLASSES_ON_QF=true, USE_RELATIVE_QF=true, LOOK_ROOT_NS_UP=true, except the no-namespace configuration case (true, false, false). Some upstream cases rely on classes declared in a companion file (project index).

Configure ​

In custos.json:

json
{
  "rules": {
    "ClassConstantCanBeUsed": {
      "enabled": false,
      "options": {
        "IMPORT_CLASSES_ON_QF": true,
        "USE_RELATIVE_QF": false,
        "LOOK_ROOT_NS_UP": false
      }
    }
  }
}

Suppress ​

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

php
// @custos-ignore ClassConstantCanBeUsed

/**
 * @noinspection ClassConstantCanBeUsedInspection
 */

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