1.2.56. Assigned Twice

The same variable is assigned twice in the same function.

While this is possible and quite common, it is also a good practice to avoid changing a value from one literal to another. It is far better to assign the new value to

Incremental changes to a variables are not reported here.

<?php

function foo() {
    // incremental changes of $a;
    $a = 'a';
    $a++;
    $a = uppercase($a);

    $b = 1;
    $c = bar($b);
    // B changed its purpose. Why not call it $d?
    $b = array(1,2,3);

    // This is some forgotten debug
    $e = $config->getSomeList();
    $e = array('OneElement');
}

?>

1.2.56.1. Suggestions

  • Remove the first assignation

  • Remove the second assignation

  • Change the name of the variable in one or both cases

1.2.56.2. Specs

Short name

Variables/AssignedTwiceOrMore

Rulesets

All, Analyze

Exakat since

0.9.8

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

variable

Available in

Entreprise Edition, Exakat Cloud