
PhpStorm: Refactoring With Inline Variable
Scott Keck-Warren • February 17, 2025
One of the code smells that drive me up the wall is when there's a variable that exists just to pass around a value. For example in the code below we're setting $value
to 42
and then returning $value
. The $value
variable isn't adding anything to this piece of code and it might actually be making it worse if there are several lines between the set and the return
because our brains have extra information they need to keep track of.
public function theAnswer(): int
{
$value = 42;
return $value;
}
In PhpStorm if I hover over this it will show me that it's an "Unnecessary local variable".
Thankfully, PhpStorm allows us to easily inline the variable by using option (⌥) and the enter key to bring up the contextual actions and quick fixes menu and then select "inline variable" and it will replace anywhere the variable is used with the value it's being set to.
public function theAnswer(): int
{
return 42;
}