1.2.534. Identical Consecutive Expression

Identical consecutive expressions might be double code. They are worth being checked.

They may be a copy/paste with unmodified content. When the content has to be duplicated, it is recommended to avoid executing the expression again, and just access the cached result.

<?php

$current  = $array[$i];
$next     = $array[$i + 1];
$nextnext = $array[$i + 1]; // OOps, nextnext is wrong.

// Initialization
$previous = foo($array[1]); // previous is initialized with the first value on purpose
$next     = foo($array[1]); // the second call to foo() with the same arguments should be avoided
// the above can be rewritten as :
$next     = $previous; // save the processing.

for($i = 1; $i < 200; ++$i) {
    $next = doSomething();
}
?>

1.2.534.1. Suggestions

  • Check if the expression needs to be used twice.

1.2.534.2. Specs

Short name

Structures/IdenticalConsecutive

Rulesets

All, Analyze

Exakat since

1.0.8

PHP Version

All

Severity

Minor

Time To Fix

Instant (5 mins)

Precision

Very high

Features

expression

Available in

Entreprise Edition, Exakat Cloud