phpintellij-ideaphpstormphp-7structural-search

IntelliJ Structural Search in PhpStorm to find specific function used within foreach block


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;
}

Solution

  • You can try this:

    foreach ($statement$) {
    $st1$
    $var$ = current($arr$);
    $st2$
    }
    

    enter image description here

    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:

    enter image description here

    NOTES:

    1. 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...

    2. 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$
    }