Reference in foreach should be unset after loop

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

Hello Stephen,

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

Regards

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.