Features

Productivity hints: Writing assignments with ‘Introduce Variable’ refactoring

Hello guys,

Recently after demoing the PhpStorm to users on a conference I discovered that our practice of writing assignments “backwards” via “Introduce variable” was really surprising to users… Lets discuss what exactly we are doing and why.

How do you normally write assignment?
First of all, you write the name of the variable, then comes the equals sign ‘=’, and then the right-hand side of the statement. While writing the right-hand side of a statement you often use completion, so at the beginning you do not know what exactly the right-hand side will look like. Nevertheless, you need to find a name for the variable before you have completed the right-hand side of the assignment.

Suppose, you want to write the following assignment statement:

Let’s start writing it from its right-hand side. Type ‘$this->’:

Select the required method from the list:

With the cursor at the end of the line, invoke the ‘Introduce Variable’ refactoring (Ctrl+Alt+V for WindowsLinux and Cmd+Opt+V for Mac).
The IDE tries to suggest a name for the variable based on its type or the called function, etc. Rather often, the suggested name is the one you would assign the variable yourself.

Click OK.

Develop with pleasure!