DisallowWritingIntoStaticProperties
info off by defaultGroup: Code style · PhpStorm name: DisallowWritingIntoStaticPropertiesInspection
Static properties are global mutable state. This opt-in rule flags assignments to static properties: by default only writes performed outside the class that declares the property; optionally every write.
Example
Default option (ALLOW_WRITE_FROM_SOURCE_CLASS = true):
php
<?php
namespace Shop;
use Shop\Cache as Store;
class Cache
{
public static $hits = 0;
public static $ttl;
public function touch()
{
self::$hits += 1;
static::$ttl = 30;
Store::$ttl = 60;
array_map(function ($v) {
Cache::$hits = $v;
}, []);
}
}
class DiskCache extends Cache
{
public static $ttl = 5;
public function reset()
{
DiskCache::$ttl = 0;
parent::$hits = 0;
DiskCache::$hits = 0;
Unknown::$whatever = 1;
Cache::$hits++;
}
}
function warmUp()
{
Cache::$ttl = 10;
}
Nowhere::$thing .= 'x';
echo Cache::$hits;Reported:
- line 19: Modify this static property only from the class that declares it.
- line 31: Modify this static property only from the class that declares it.
- line 32: Modify this static property only from the class that declares it.
- line 40: Modify this static property only from the class that declares it.
- line 43: Modify this static property only from the class that declares it.
Options
| Option | Type | Default | Effect |
|---|---|---|---|
| ALLOW_WRITE_FROM_SOURCE_CLASS | bool | true | true: only writes from outside the declaring class are reported (D3). false: every static property assignment is reported (D2). |
Configure
In custos.json:
json
{
"rules": {
"DisallowWritingIntoStaticProperties": {
"enabled": true,
"options": {
"ALLOW_WRITE_FROM_SOURCE_CLASS": true
}
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore DisallowWritingIntoStaticProperties
/**
* @noinspection DisallowWritingIntoStaticPropertiesInspection
*/