Features PhpStorm

‘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!

Comments below can no longer be edited.

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

  1. Avatar

    OZ says:

    May 3, 2011

    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.

  2. Avatar

    Sam Dark says:

    May 3, 2011

    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. Avatar

    Erik Schierboom says:

    May 3, 2011

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

  4. Avatar

    Bryan Green says:

    May 3, 2011

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

  5. Avatar

    OZ says:

    May 3, 2011

    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. Avatar

    Alexey Korsun says:

    May 3, 2011

    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. Avatar

    OZ says:

    May 3, 2011

    Hi, Alexey, thank you.

  8. Avatar

    Nils Luxton says:

    May 3, 2011

    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. Avatar

    Nikolay Matveev says:

    May 3, 2011

    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. Avatar

    John McBliss says:

    May 3, 2011

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

  11. Avatar

    Alex Andrienko says:

    May 4, 2011

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

  12. Avatar

    Nikolay Matveev says:

    May 4, 2011

    Yes, the refactoring works for JavaScript also.

  13. Avatar

    Nikolay Matveev says:

    May 4, 2011

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

  14. Avatar

    OZ says:

    May 5, 2011

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

Discover more