OneTimeUseVariables
warning on by default quick-fixGroup: Control flow · PhpStorm name: OneTimeUseVariablesInspection
A local variable that is assigned in one statement and consumed exactly once in the very next return, throw or destructuring statement adds a name without adding meaning. The value can be used directly at the consumption site.
Example
php
<?php
$total = 40 + 2;
return $total;
function makeUser() {
$user = new User();
return $user->name;
}
function failWith($why) {
$err = new DomainError($why);
throw $err;
}
function split2() {
$parts = explode(':', 'a:b');
[$head, $tail] = $parts;
return $head . $tail;
}
function pick($a, $b) {
$chosen = $a ?: $b;
return $chosen->label;
}
function copyOf($proto) {
$dup = clone $proto;
return $dup->fresh();
}
function &byRef() {
$slot = null;
return $slot;
}
function twice() {
$n = compute();
$n = $n * 2;
return $n;
}
function readAgain() {
$cfg = load();
[$k] = $cfg;
return $cfg ? $k : null;
}
function annotated() {
/** @var Gadget $g */
$g = factory();
return $g;
}
function staticAccess() {
$cls = whichClass();
return $cls::build();
}
function compound($v) {
$v .= '!';
return $v;
}
$fn = function () use (&$acc) {
$acc = 5;
return $acc;
};
function refParam(&$out) {
$out = 1;
return $out;
}php
<?php
return 40 + 2;
function makeUser() {
return (new User())->name;
}
function failWith($why) {
throw new DomainError($why);
}
function split2() {
[$head, $tail] = explode(':', 'a:b');
return $head . $tail;
}
function pick($a, $b) {
return ($a ?: $b)->label;
}
function copyOf($proto) {
return (clone $proto)->fresh();
}
function &byRef() {
$slot = null;
return $slot;
}
function twice() {
$n = compute();
$n = $n * 2;
return $n;
}
function readAgain() {
$cfg = load();
[$k] = $cfg;
return $cfg ? $k : null;
}
function annotated() {
/** @var Gadget $g */
$g = factory();
return $g;
}
function staticAccess() {
$cls = whichClass();
return $cls::build();
}
function compound($v) {
$v .= '!';
return $v;
}
$fn = function () use (&$acc) {
$acc = 5;
return $acc;
};
function refParam(&$out) {
$out = 1;
return $out;
}Reported:
- line 2: Variable $total is used only once; inline its value.
- line 6: Variable $user is used only once; inline its value.
- line 11: Variable $err is used only once; inline its value.
- line 16: Variable $parts is used only once; inline its value.
- line 22: Variable $chosen is used only once; inline its value.
- line 27: Variable $dup is used only once; inline its value.
Options
| Option | Type | Default | Effect |
|---|---|---|---|
ALLOW_LONG_STATEMENTS | bool | false | When false, assignments whose text exceeds 80 characters are left alone (a long expression may deserve a name). When true, length is ignored. |
ANALYZE_RETURN_STATEMENTS | bool | true | Inspect return consumers (D1). |
ANALYZE_THROW_STATEMENTS | bool | true | Inspect throw consumers (D2). |
ANALYZE_ARRAY_DESTRUCTURING | bool | true | Inspect list()/[] destructuring consumers (D3). |
Configure
In custos.json:
json
{
"rules": {
"OneTimeUseVariables": {
"enabled": false,
"options": {
"ALLOW_LONG_STATEMENTS": false,
"ANALYZE_RETURN_STATEMENTS": true,
"ANALYZE_THROW_STATEMENTS": true,
"ANALYZE_ARRAY_DESTRUCTURING": true
}
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore OneTimeUseVariables
/**
* @noinspection OneTimeUseVariablesInspection
*/