PhpStorm 2022.3 Help

Extract method

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

  • If the selection is made inside a function or a script, the refactoring extracts a function.

Extract a PHP function or method

  1. In the editor, select a block of code to be transformed into a function or method.

    The selected code fragment does not need to be a set of statements. It may also be an expression used somewhere in the code.

  2. From the main menu or the selection context menu, choose Refactor | Extract Method or press Ctrl+Alt+M.

    The title of the dialog 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, click the Move up button and the Move down button. 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 checkbox.

  8. View and check the declaration of the function or method to be generated in the Signature preview read-only area.

PHP Extract Method example

Before

After

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

Before

After

if ('POST' != $_SERVER['REQUEST_METHOD']) { header('Allow: POST'); header('HTTP/1.1 405 Method Not Allowed'); header('Content-Type: text/plain'); exit; }
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; }
Last modified: 28 September 2022