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!

Comments below can no longer be edited.

7 Responses to Productivity hints: Writing assignments with ‘Introduce Variable’ refactoring

  1. Bryan Green says:

    April 1, 2011

    As always, me rikey.


  2. Johnny Woo says:

    April 1, 2011

    This is awesome! It definitely should be one of those productivity tips, if it’s not there yet.

  3. Vladimir says:

    April 6, 2011

    It’s work only inside class methods. But if you try

    $dec=new DecoratorFactory(); //for example

    then Ctrl+Alt+V (Win), etc…

    you receive other result.

  4. alinares says:

    April 11, 2011

    It is a great feature, but it would be better if the dialog didn’t popped up. Just leave the cursor ready to write the variable between the ‘$’ and ‘=’ characters, and when hitting tab move it to where it originally was 🙂

  5. Nikolay Matveev says:

    April 14, 2011

    Can’t reproduce. Please provide screenshot(you can create a new issue in our issue tracker and attach screenshot there).

    Please create a new feature request –

  6. Shaun says:

    June 7, 2011

    I don’t use this feature that often, but it definitely saved me some time today when I did need it.

  7. Nikolay Matveev says:

    June 10, 2011

    Thanks for feedback!