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.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
krylov123
  • 739
  • 8
  • 15
  • Already asked: https://stackoverflow.com/questions/7026690/reconstruct-get-code-of-php-function – Luuk Sep 08 '19 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. – krylov123 Sep 08 '19 at 15:52

1 Answers1

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

LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • 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! – krylov123 Sep 08 '19 at 20:37
  • Please test the resulting code if your functions have `"some text {$param}"` kind of strings: for me it produced a bit wrong code (https://youtrack.jetbrains.com/issue/WI-48596) – LazyOne Sep 08 '19 at 20:52
  • 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). – krylov123 Sep 08 '19 at 21:25