1.2.1025. Recycled Variables¶
A recycled variable is a variable used for two distinct missions in a method. There is usually a first part, with its own initialization, then, later in the method, a second part with a new initialization and a distinct usage of the variable.
Recycled variables leads to confusion: with the new initialization, the variable changes its purpose. Yet, with the same name, and with a bit of lost context, it is easy to confuse it later.
<?php
function foo() {
$variable = "initial"; // first initialisation
$variable = goo($variable); // processing the variable
hoo($variable); // sending the variable to a final destination
$variable = "second" ; // second initialisation
hoo2($variable); // sending the variable to a different final destination
}
?>
See also Please Don’t Recycle Local Variables.
1.2.1025.1. Connex PHP features¶
1.2.1025.1.1. Suggestions¶
Use distinct names for each variable
Split the method into smaller methods and unique variable name usage
1.2.1025.1.2. Specs¶
Short name |
Variables/RecycledVariables |
Rulesets |
|
Exakat since |
2.3.3 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
High |
Available in |