locate duplicate code -> build function

Hello!

In my file is double code.
I display this code.
Is there a tool that converts the code into a function?

Since I now have PhpSorm version 6.0 !!
:)

Sincerely yours

Stephan

3 comments

Hi there,

Please select such code and try "Refactor | Extract | Method..."

0

Hello!

Thanks for the information.
Unfortunately, the result is not as hoped.

I have a file.

--------------------------
function test1 () {
     $ aaa = "aaaaa";
     $ bbb = "bbbbb";
     $ ccc = "ccccc";
}

function test2 () {
     $ aaa = "aaaaa";
     $ bbb = "bbbbb";
     $ ccc = "ccccc";
}

function test3 () {
     $ aaa = "aaaaa";
     $ bbb = "bbbbb";
     $ ccc = "ccccc";
}

------------------------------

This file is duplicate code.
With code -> Locate Duplicates ..
can I find duplicate code.

If I use the duplicate code
Refactor -> Extract -> Method ...
alters I get the file.

-------------------------------
function test1 () {
     myDuplicateCode ();
}

myDuplicateCode function ()
{
     $ aaa = "aaaaa";
     $ bbb = "bbbbb";
     $ ccc = "ccccc";
}

function test2 () {
     $ aaa = "aaaaa";
     $ bbb = "bbbbb";
     $ ccc = "ccccc";
}

function test3 () {
     $ aaa = "aaaaa";
     $ bbb = "bbbbb";
     $ ccc = "ccccc";
}

-------------------

Why not in the other functions?

Sincerely yours

Stephan

0

Other functions/places you have to do manually -- you can do such "replacement" from the same screen actually:
screen01.png
So ... in your example, After you've executed Code | Refactor | Extract Method:

  1. Make duplicate #2 to be displayed on right side in diff viewer (it is by default)
  2. Click on 2) to copy call to extracted method to that duplicate #2
  3. Make duplicate #3 to be displayed on right side  in diff viewer (the icon in 1) should be green)
  4. Click on 2) to copy call to extracted method to that duplicate #3


After making these operations the result will be:

function test1 () {
    ABC();
}

function ABC()
{
    $aaa = "aaaaa";
    $bbb = "bbbbb";
    $ccc = "ccccc";
}

function test2 () {
    ABC();
}

function test3 () {
    ABC();
}
0

Please sign in to leave a comment.