phpsa icon indicating copy to clipboard operation
phpsa copied to clipboard

[Analyzer] Foreach - possible to write in referenced variable

Open ovr opened this issue 7 years ago • 0 comments

Simple snippet:

$elements = [1,2,3,4,5];

foreach ($elements as $k => &$item) {
//....
}

$another = [1,2,3,4,5];
foreach ($another as $k => $item) {

}

https://3v4l.org/ndpr0 WIll be bug, $elements = [1,2,3,4,6]

Algoritm:

  • Register Analyzer on foreach, if key or value
  • Check if $key or $value exist in the SymbolTable
  • If variable is referenced

Probably need to support unset operator if it's not working

Notice:

Variable should be renamed to protect writing to exists referenced variable

ovr avatar Sep 06 '16 07:09 ovr