PhpStorm 2017.1 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.

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; } ?>

JavaScript Extract Function example

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

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 PHP function or method:

  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.

See Also

Last modified: 19 July 2017