PhpStorm 2021.2 Help

Extract field

The Extract Field refactoring lets you declare a new field and initialize it with the selected expression. The original expression is replaced with the usage of the field. The new field is created with the default visibility modifier, which is set on the Code Generation tab of the Code Style. PHP page of the Settings/Preferences dialog Ctrl+Alt+S.

Extract a field in place

  1. Position the caret within a piece of code you want to extract into a field.

  2. Press Ctrl+Alt+F or from the main menu, select Refactor | Extract Field.

  3. Select an expression you want to introduce as a field.

    Extract field
    If PhpStorm detects more than one occurrence in your code, it lets you specify which occurrences to replace.

    Extract multiple occurrences of a field

  4. Provide the name of the new field and choose where it will be initialized: in its declaration, in the current method, or in the class constructor. When you work with PHPUnit tests, PhpStorm also lets you initialize the field in the setUp method. For more details on working with PHPUnit in PhpStorm, see PHPUnit.

    Extract and initialize field

Extract a field using the dialog

If the In modal dialogs refactoring option is selected in the Refactorings area on the Code Editing page of the Settings/Preferences dialog Ctrl+Alt+S, the Extract Field refactoring is performed by means of the Extract Field Dialog.

enable Inplace Refactoring disabled
  1. In the editor, select the expression or variable to be replaced with a field, or just position the caret within such an expression or variable declaration.

  2. In the main menu, or the context menu of the selection, choose Refactor | Extract | Field, or press Ctrl+Alt+F.

  3. In the Extract Field Dialog dialog that opens, configure the refactoring:

    Extract Field Dialog
    1. Specify the name of the field.

    2. Specify where the new field should be initialized by selecting the necessary option under Initialize in. When you work with PHPUnit tests, PhpStorm also lets you initialize the field in the setUp method. For more details on working with PHPUnit in PhpStorm, see PHPUnit.

    3. In the Visibility area, select the visibility scope for the new field.

    4. To replace all the occurrences of the selected expression (if the selected expression is found more than once in the class), select the Replace all occurrences checkbox.

    5. Click OK.

Example

Let's extract the 'param_query' argument into a $query class property. As a result, PhpStorm declares the new public $query property and changes all 'param_query' occurrences to self::$query. The resulting code will look as follows depending on where you've chosen to initialize the property:

Before

After

public function find($params) { return execute($params['param_query']); } public function findAll($params) { return executeAll($params['param_query']); }
public $query = 'param_query'; public function find($params) { return execute($params[self::$query]); } public function findAll($params) { return executeAll($params[self::$query]); }

Before

After

public function find($params) { return execute($params['param_query']); } public function findAll($params) { return executeAll($params['param_query']); }
public $query; public function find($params) { self::$query = 'param_query'; return execute($params[self::$query]); } public function findAll($params) { return executeAll($params[self::$query]); }

Before

After

public function find($params) { return execute($params['param_query']); } public function findAll($params) { return executeAll($params['param_query']); }
public $query; public function __construct() { $this->query = 'param_query'; } public function find($params) { return execute($params[$this->query]); } public function findAll($params) { return executeAll($params[$this->query]); }

Before

After

public function find($params) { return execute($params['param_query']); } public function findAll($params) { return executeAll($params['param_query']); }
private $query; public function find($params) { return execute($params[$this->query]); } public function findAll($params) { return executeAll($params[$this->query]); } protected function setUp() { $this->query = 'param_query'; parent::setUp(); }
Last modified: 16 November 2021