PhpStorm 9.0.0 Help

Extract Parameter

The Extract Parameter refactoring is used to add a new parameter to a function declaration and to update the function calls accordingly.

See also Change Signature.

Extracting a Parameter in PHP

In the example below, a new parameter $c will be added to the Calculate() function to replace 10:

Before After
<?php class Class1 { public function Calculate($i){ while ( $i < 10 ) { $i = $i + 1; return $i; }; } public function DisplaySum(){ $a = 1; $result = $this -> Calculate($a); echo "The final result is " . $result; } }
<?php class Class1 { public function Calculate($i,$c){ while ( $i < $c ) { $i = $i + 1; return $i; }; } public function DisplaySum(){ $a = 1; $result = $this -> Calculate($a,10); echo "The final result is " . $result; } }
  1. In the editor, place the cursor within the expression to be replaced by a parameter.
  2. Do one of the following:
    • Press Ctrl+Alt+P.
    • Choose Refactor | Extract | Parameter in the main menu.
    • Choose Refactor | Extract | Parameter from the context menu.
  3. In the Introduce Parameter dialog box that opens, type the name of the new parameter.

When you click OK, the code is updated: the new parameter is added to the definition of the function and to its call.

JavaScript Example

BeforeAfter
A new parameter will be added to this function to replace the 1's:
function calculate_sum(i) { alert('Adding ' + 1 + ' to ' + i); return (1 + i); } function show_sum() { alert('Result: ' + calculate_sum(5)); }
The new parameter i2 has been added as an optional parameter:
function calculate_sum(i, i2) { i2 = i2 || 1; alert('Adding ' + i2 + ' to ' + i); return (i2 + i); } function show_sum() { // The function call has not changed: alert('Result: ' + calculate_sum(5)); }
A new parameter will be added to this function to replace the 1's. When adding a new parameter we'll specify that it should be a required one.
function calculate_sum(i) { alert('Adding ' + 1 + ' to ' + i); return (1 + i); } function show_sum() { alert('Result: ' + calculate_sum(5)); }
The new parameter i2 has been added as a required parameter:
function calculate_sum(i, i2) { alert('Adding ' + i2 + ' to ' + i); return (i2 + i); } function show_sum() { alert('Result: ' + calculate_sum(5, 1)); }

See Extract Parameter in JavaScript for details.

See Also

Last modified: 2 July 2015