PhpStorm 9.0.0 Help

Extract Method

Basics

When the Extract Method refactoring is invoked , PhpStorm analyses the selected block of code and detects variables that are the input for the selected code fragment and the variables that are output for it.

The detected output variable is used as a return value for the extracted function.

In the JavaScript context, this refactoring always results in a function.

In the PHP context, the result of applying the Extract Method refactoring depends on the location of the selected code fragment.

  • If the selection is made inside a method of a class, the refactoring extracts a method. This case is applicable when you are using PHP 5.0 and higher.
  • If the selection is made inside a function or a script, the refactoring extracts a function.

Examples

Javascript example

BeforeAfter
function multiplication(a,b) { c = a + b; d = c * c; return d; }
function sum(a,b); return a + b; function multiplication(a,b) { c = sum(a,b); d = c * c; return d; }

Php extract method example

BeforeAfter
public function init() { $this->_router = $this->getFrontController()->getRouter(); }
public function init() { $this->_router = $this->getRouter(); } public function getRouter() { return $this->getFrontController()->getRouter(); }

Php extract function example

BeforeAfter
<?php if ('POST' != $_SERVER['REQUEST_METHOD']) { header('Allow: POST'); header('HTTP/1.1 405 Method Not Allowed'); header('Content-Type: text/plain'); exit; } ?>
<?php function printEmptyHeader() { header('Allow: POST'); header('HTTP/1.1 405 Method Not Allowed'); header('Content-Type: text/plain'); } if ('POST' != $_SERVER['REQUEST_METHOD']) { printEmptyHeader(); exit; } ?>

Extracting a javascript or php function

To extract a function in the javascript context

  1. In the editor, select a block of code to be transformed into a function.
  2. On the main menu or on the context menu of the selection, choose Refactor | Extract Method or press Ctrl+Alt+M.
  3. In the Extract Function dialog box that opens, specify the name of the new function.
  4. To have the new function defined through a function expression, select the Declare functional expression check box.
  5. In the Parameters area, configure the set of variables to be passed to the new function as parameters. By default, all the variables from the specified scope are listed.
    • To have a variable included in the parameter set, select the check box next to it.
    • To change the order of parameters, use the Move Up and Move Down buttons.
  6. View and check the declaration of the function to be generated in the Signature preview read-only area.

To extract a function or method in the PHP context

  1. In the editor, select a block of code to be transformed into a function or method.
  2. On the main menu or on the context menu of the selection, choose Refactor | Extract Method or press Ctrl+Alt+M.

    The title of the dialog box that opens is either Extract Method or Extract Function depending on the intended refactoring output.

  3. Specify the name of the new function or method.
  4. If a method is to be extracted, choose the relevant access modifier for it in the Visibility area:
    • Public
    • Protected
    • Private
  5. Configure the output of the new method or function in the Return output variable(s) through area.

    The Output variable(s) read-only field displays all the variables that make up the output of the selected code fragment. Specify the way in which the new method or function will return these variables to the callee.

    • To have the output variables returned by value, select the Return statement option. The result of this choice depends on the number of detected output variables. If there is exactly one output variable, it will be used as the return value. If the selection outputs several variables, these variables will be returned as an array.
    • To have the output variables returned by reference, select the Parameter(s) passed by reference option. In this case, no return statement will be generated. Instead, all the detected output variables will be added to the list of input parameters. Their names will be prepended with an ampersand & both in the Parameters list and in the declaration of the new method/function, as shown in the Signature preview read-only area.
  6. In the Parameters area, configure the input for the new function or method.

    Originally, the list contains the variables that PhpStorm has detected as the input for the new method or function. However, if you have chosen to return the new method/function output by reference, the list also contains all the output variables.

    To change the order of parameters, use the Move Up and Move Down buttons. This order determines the order in which the parameters are listed in the declaration of the new method or function.

    If necessary, rename the desired parameters, by double-clicking the corresponding parameter lines and entering new names.

  7. Optionally, to have PhpStorm transform tail break or continue statements if the selection contains any, select the Replace tail "break/continue" statements with return statements check box.
  8. View and check the declaration of the function or method to be generated in the Signature preview read-only area.

See Also

Last modified: 2 July 2015