MagicMethodsValidity
error on by defaultGroup: Probable bugs · PhpStorm name: MagicMethodsValidityInspection
PHP's magic methods (__get, __toString, __clone, …) have fixed contracts: static-ness, visibility, number of parameters, return value. Violations are fatal errors, silently ignored methods or subtle bugs. This rule validates each magic method declared in a class-like, flags __-prefixed methods that are not magic, and catches magic names typed with a single underscore.
Example
php
<?php
class Base
{
public function __construct($id = 0) {}
public function __clone() {}
}
class Point extends Base
{
public function __construct($id = 0) { parent::__construct($id); }
static public function __clone() { parent::__clone(); }
public function __get() {}
protected function __call($verb, $args) {}
public function __callStatic($verb, $args) {}
public function __fetchAll() {}
public function __soapCall() {}
}
class Line extends Base
{
public function __construct($id = 0) {}
#[\Override]
public function __clone() {}
public function __toString()
{
if (rand(0, 1)) {
return $this->label;
}
return 42;
}
public function __wakeup()
{
return true;
}
public function __sleep() {}
public function __debugInfo(): string { return ''; }
public function __isset(&$key) {}
public function __set($key, $value) {}
public function __unset($key) {}
}
class Shape
{
public static function __set_state($props) { return new static(); }
public function _invoke() {}
public function __autoload($cls) {}
}
interface Printable { public function __toString(); }Reported:
- line 11: __clone must not be static.
- line 12: __get must declare exactly 1 parameter(s).
- line 12: __get needs a companion __set method.
- line 13: __call must be declared public.
- line 14: __callStatic must be declared static.
- line 15: The '__' prefix is reserved for magic methods.
- line 21: __construct does not call Base::__construct().
- line 29: __toString must return string; got 'int'.
- line 33: __wakeup must not return a value.
- line 35: __sleep must return array; got ''.
- line 36: __debugInfo must return array|null; got 'string'.
- line 37: __isset must not take parameters by reference.
- line 38: __set needs a companion __get method.
- line 45: '_invoke' is not magic; did you mean '__invoke'?
- line 46: __autoload is deprecated since PHP 7.2; use spl_autoload_register().
Configure
In custos.json:
json
{
"rules": {
"MagicMethodsValidity": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore MagicMethodsValidity
/**
* @noinspection MagicMethodsValidityInspection
*/