I need to find all foreach
blocks wherever I have used current()
function inside of it. Below is sample code block, which should get find.
$x=12;
foreach ($array as &$val)
{
$cur=current($array);
$x=$x+$cur;
echo $cur;
}
You can try this:
foreach ($statement$) {
$st1$
$var$ = current($arr$);
$st2$
}
Test code:
<?php
$x=12;
foreach ($array as &$val)
{
$cur=current($array);
$x=$x+$cur;
echo $cur;
}
foreach ($array as &$val)
{
$cur=current($array);
}
foreach ($array as &$val) {
$x=$x+$cur;
$cur = current($array);
}
foreach ($array as &$val) {
$x=$x+$cur;
echo $cur;
echo $cur;
$cur = current($array);
}
foreach ($array as &$val) {
$x=$x+$cur;
echo $cur;
echo $cur;
}
Result:
NOTES:
You can change Search target
from "Complete match" to some specific placeholder/variable and it will highlight only that place instead of the whole foreach
block...
That rule will not find places where current()
is used without assignment or in a some different way (it has to be $someVar = current($someArray);
).
You may create a separate rule for that (I'm not sure right now how can they be merged together though), e.g.
foreach ($statement$) {
$st1$
current($arr$);
$st2$
}