‘Extract Function/Method’ refactoring for PHP

Hello guys,

We are working hard on new features for PhpStorm 2.1, and the ‘Extract Function/Method’ refactoring for PHP is one of them. This feature has been available since the first PhpStorm 2.1 EAP(106.444), and we are looking forward for your feedback.

The main point of the refactoring is to convert a part of a larger function/method into a new function/method. This might be useful if you need to make your code easier to understand or reuse a specific code fragment.

Let’s try to put this refactoring into practice.
Select a code fragment (Picture 1a) or an expression (Picture. 1b).

Picture 1a: Select a code fragment

Picture 1b: Select an expression

Invoke the refactoring from the menu ‘Refactor -> Extract Method‘ or using  a hotkey. The default hotkey is Ctrl+Alt+M for Windows/Linux and Cmd+Opt+M for Mac.
Depending on the context of the selected code fragment, ‘Extract method’ (the  code fragment is inside a method) (Picture 2b) or ‘Extract Function’(the code fragment is inside a function or script) (Picture 2a).

Picture 2a: ‘Extract Function’ dialog

Picture 2b: ‘Extract Method’ dialog

Enter the name of the new function/method. Press OK.

Picture 3a:  The code fragment is replaced with a function call statement

Picture 3b: The expression is replaced with a method call expression

Picture 4a: Definition of the extracted function

Picture 4b: Definition of the extracted method

Develop with pleasure!

This blog is permanently closed.

For up-to-date information please follow to corresponding WebStorm blog or PhpStorm blog.

 
This entry was posted in Cool Feature, PhpStorm and tagged , , , . Bookmark the permalink.

14 Responses to ‘Extract Function/Method’ refactoring for PHP

  1. OZ says:

    It’s just toys. It can be done easy without any ‘new refactoring’, just copy+paste (and will be less errors). Please, create ‘Extract Interface’ refactoring, it will be much more useful.
    http://youtrack.jetbrains.net/issue/WI-5093

  2. Sam Dark says:

    This one is the the most useful refactoring type for me (after rename, of course). Tried it in 107.21 and it worked flawlessly.

  3. Erik Schierboom says:

    I once tweeted: PHPStorm is great, now all I need is Extract Method. Well, you have provided! Great work.

  4. Bryan Green says:

    Keep this up. Can’t wait to renew my license for another year.

  5. OZ says:

    Lol, my comment is being hidden.
    Well, I can’t understand why this method looks ‘great’ for you, but I respect your opinion – maybe it’s just rare operation for me.
    But, I can’t believe that nobody cares about interfaces. It’s so important part of OOP… And forgotten features for interfaces looks weird.

  6. Hi, Oz.

    No, it just was “under moderation” :) Don’t know why our spam-filter concerned that your message requires additional attention from moderators. As regards development plans our developers will answer better than I.

    Alexey Korsun,
    marketing manager

  7. OZ says:

    Hi, Alexey, thank you.

  8. Nils Luxton says:

    This is great and will save countless development hours. Perhaps OZ above is one of the lucky few who work mostly on new projects!

  9. Nikolay Matveev says:

    @OZ
    18 votes for ‘Extract Method’(WI-2779) versus 3 votes for ‘Extract Interface’(WI-5093);) ‘Extract Interface’ refactoring is great one, but we need to listen our community.

  10. John McBliss says:

    Will this also work for Javascript? If not, is it planned?

  11. Alex Andrienko says:

    Now that looks *VERY* promising! Can’t wait till to try it out.

  12. Nikolay Matveev says:

    @John
    Yes, the refactoring works for JavaScript also.

  13. Nikolay Matveev says:

    @Alex
    Your previous comment about the refactoring was the reason of big improvements. Thank you for constructive feedback!

  14. OZ says:

    Nils Luxton, this refactoring very useful for old code exactly, to replace type hinting and avoid high coupling.

Comments are closed.