Search code examples
phpphpstormautomated-refactoring

Refactoring: How to replace function call with function code


What I'm trying to achieve is like Code -> Refactor -> Extract -> Method functionality of PhpStorm but vice versa.

I want to take some particular function calls and replace it with the code of that functions.

For example I have:

function main()
{
    $test = "test";
    module1($test);
    module2($test);
}

function module1($text)
{
    echo "Some text:" . $text;
}

function module2($text)
{
    echo "Another text:" . $text;
}

Then I want to receive next result:

function main()
{
    $test = "test";
    echo "Some text:" . $test;
    echo "Another text:" . $test;
}

I don't need full recursion, like if module1() function contain another function call - I don't need to go deep inside of it. Just let it be on level 1.

It also doesn't matter for me how to do it, with PhpStorm, another IDE or with another script.


Solution

  • Use Refactor | Inline... in PhpStorm (invoke it on a function/method definition).

    <?php
    function aa($hello)
    {
        return "Hello $hello";
    }
    
    echo aa('Yo!');
    

    enter image description here

    Final result for that simple code:

    <?php
    
    echo "Hello 'Yo!'";
    

    As you may see it's a bit incorrect (the single quotes around the variable content), so make sure to check your code afterwards.


    Final result for your code sample (after using it on each function):

    enter image description here