Inline Temp Variable Refactoring
debt(d5/e1/b1/t5)
Closest to 'specialist tool catches it' (d5), because the detection_hints list Rector and PHPCS — both specialist static analysis/linting tools — with an automated pattern match for single-use temp variables. This is not a compiler or default linter catch, but it is automatable with the right tooling.
Closest to 'one-line patch or single-call swap' (e1), because the quick_fix is literally removing a single temp variable assignment and inlining its expression — a trivial, localised, one-line change per occurrence.
Closest to 'minimal commitment' (b1), because this is a localised readability refactoring with no architectural implications. It applies at the statement level and imposes no ongoing tax on future maintainers once applied.
Closest to 'notable trap' (t5), because the misconception field explicitly states the trap: developers assume inlining temp variables always improves code, but complex expressions actually benefit from a descriptive variable name. This is a documented gotcha that competent developers commonly fall into — over-inlining at the cost of readability.
TL;DR
Explanation
A temporary variable assigned once and used once can often be inlined if the expression is already self-explanatory. Especially common in boolean checks: $isEligible = $user->age >= 18; return $isEligible; → return $user->age >= 18; But: keep the temp if it (1) clarifies a complex expression with a meaningful name, (2) is used more than once, (3) makes a slow operation happen once. Inline Temp is the inverse of Extract Variable. It reduces line count but should only be done when clarity is preserved or improved.
Common Misconception
Why It Matters
Common Mistakes
- Inlining complex expressions that need a name to be understandable.
- Inlining variables used in multiple places — creates duplication.
- Not inlining obvious single-use variables that add no information.
Code Examples
$result = calculateTotal($items);
return $result; // $result adds nothing
return calculateTotal($items); // Direct, no noise
// But KEEP the temp when it adds meaning:
$isEligibleForDiscount = $user->isPremium() && $order->total > 100;
return $isEligibleForDiscount; // Name clarifies the condition