Reference in foreach should be unset after loop

php

(Stephen Eisenhauer) #1

SonarPHP should flag code which uses a reference in a foreach loop without unsetting that reference immediately after.

Noncompliant code:

foreach ($entries as &$entry) {
    $entry += 1;
}

Compliant code:

foreach ($entries as &$entry) {
    $entry += 1;
}
unset($entry);

External references:

Type: Bug


(Alexandre Gigleux) #2

Hello Stephen,

Thanks for the suggestion. I created SONARPHP-822 to add it to SonarPHP.

Regards