‘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 entry was posted in Cool Feature, PhpStorm and tagged , , , . Bookmark the permalink.
  • OZ

    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://rmcreative.ru/ Sam Dark

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

  • Erik Schierboom

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

  • Bryan Green

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

  • OZ

    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.

  • http://jetbrains.com Alexey Korsun

    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

  • OZ

    Hi, Alexey, thank you.

  • Nils Luxton

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

  • Nikolay Matveev

    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.

  • John McBliss

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

  • Alex Andrienko

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

  • Nikolay Matveev

    Yes, the refactoring works for JavaScript also.

  • Nikolay Matveev

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

  • OZ

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