Avoid Reassigning Parameters
ID |
php.avoid_reassigning_parameters |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Php |
Tags |
code_smell, reliability |
Description
Reports a statement that rebinds a function parameter inside the body of the function that
declares it — by plain assignment, compound assignment, or increment/decrement. By-reference
parameters (&$p) are exempt, since rebinding them is exactly how they return a value to the
caller.
Rationale
When a parameter is reassigned the body no longer matches the caller’s view of the call: the original argument value is silently lost partway through the function, debuggers and stack traces show a value the caller never passed, and a later reader has to scan the whole body to learn what the name currently holds. Copying the argument into a clearly named local keeps the parameter intact and makes the transformation explicit.
<?php
function normalize($name) {
$name = trim($name); // FLAW — the original argument is now unrecoverable
return strtolower($name);
}
function normalize($name) {
$clean = trim($name); // OK — original parameter preserved, intent explicit
return strtolower($clean);
}
Remediation
Introduce a local variable initialised from the parameter and operate on the local. Leave the parameter untouched so the original argument value remains available throughout the function.