0

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.

2
  • Already asked: stackoverflow.com/questions/7026690/… Commented Sep 8, 2019 at 15:25
  • @Luuk it doesn't refactor variable name/parameter content, just took the pure code of function. But maybe it's the nearest possible option. Thanks anyway, I will wait for any other answer (maybe someone knew IDE way to do it) for a little bit longer. Commented Sep 8, 2019 at 15:52

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Man, you rock! That's exactly what I'm looking for. And it's so close to "extract" ))) Just didn't knew what "inline" do. Thanks!
Please test the resulting code if your functions have "some text {$param}" kind of strings: for me it produced a bit wrong code (youtrack.jetbrains.com/issue/WI-48596)
Yes, when you have variable inside the quotes, like "some text {$param}" then issue is reproducing. In my example echo "Some text:" . $text; replacement goes fine (obviously).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.