SelfClassReferencing
info on by default quick-fixGroup: Code style · PhpStorm name: SelfClassReferencingInspection
Inside a class's own methods, refer to the class consistently: by default with self (and __CLASS__ instead of Name::class), which survives renames; or, when PREFER_CLASS_NAMES is on, with the explicit class name instead of self / __CLASS__.
Example
Default mode:
php
<?php
namespace Shop;
class Basket
{
public function merge(Basket $other): ?Basket
{
$copy = new \Shop\Basket();
$max = Basket::LIMIT;
if ($other instanceof Basket) {}
log_event(Basket::class);
$make = fn() => new Basket(); // inside arrow function: skipped
$anon = new class() extends Basket {}; // extends clause: skipped
return self::empty();
}
}
trait Countable2
{
public function who() { return Countable2::class; } // trait: skipped
}php
<?php
namespace Shop;
class Basket
{
public function merge(self $other): ?self
{
$copy = new self();
$max = self::LIMIT;
if ($other instanceof self) {}
log_event(__CLASS__);
$make = fn() => new Basket(); // inside arrow function: skipped
$anon = new class() extends Basket {}; // extends clause: skipped
return self::empty();
}
}
trait Countable2
{
public function who() { return Countable2::class; } // trait: skipped
}Reported:
- line 6: Refer to the class as 'self' instead of 'Basket'.
- line 8: Refer to the class as 'self' instead of 'Basket'.
- line 9: Refer to the class as 'self' instead of 'Basket'.
- line 10: Refer to the class as 'self' instead of 'Basket'.
- line 11: Refer to the class as '__CLASS__' instead of 'Basket::class'.
Options
| Option | Type | Default | Effect |
|---|---|---|---|
| PREFER_CLASS_NAMES | bool | false | Switches the preferred style: false = use self/__CLASS__; true = use the explicit class name / Name::class. |
Configure
In custos.json:
json
{
"rules": {
"SelfClassReferencing": {
"enabled": false,
"options": {
"PREFER_CLASS_NAMES": false
}
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore SelfClassReferencing
/**
* @noinspection SelfClassReferencingInspection
*/