Introducing Parameter Refactoring in PhpStorm 9

PhpStorm users got a great new refactoring tool in PhpStorm 9 – parameter refactoring. Here’s a quick explanation on how to use this cool new tool.

It’s easy to refactor a value that is being used inside a function so that it needs to be passed into the function as a parameter. Suppose you have a function that defines a value inline:

Refactor Parameter

Here, we’d like to pass the filename into the function as a second parameter, rather than have it generated inline in the file_put_contents call.

If we highlight the date(‘dYM’) . ‘.log’ string, and press CMD/CTRL + ALT + P (or CTRL + T plus select 6. for “Parameter..”), PhpStorm will ask what we want to name the new parameter. In this case I’d like to call it filename, so I enter that name into the prompt. Once we confirm the name, the function will have been refactored:

Refactor Parameter

Notice how PhpStorm has moved the generation of the filename string into the call to the writeToLogFile function, and added the $filename parameter to the function definition and to the first parameter in the file_put_contents call.

If you would like to only move part of a string into a parameter, then simply highlight the part you wish to move and again press CMD/CTRL + ALT + P. PhpStorm will now ask you what you want to name the new parameter, and whether you would like to the string and new parameter, or embed into the string using double quotes.

Keep up with the latest PhpStorm news on our blog and follow us on Twitter @phpstorm.

Develop with pleasure!
– JetBrains PhpStorm Team

About Gary Hockin

Gary Hockin has been creating code to power web applications for 15 years, the last three of them at an extraordinary level. He is handsome, talented, funny and articulate, and also extremely modest. Gary is a valued contributor to Zend Framework 2 and is a member of the community review team. He's married and has two wonderful children, and when he's not coding, writing about code, speaking about code or reading about code, he can usually be found in the pub playing pool.
This entry was posted in Cool Feature, Early Access Program and tagged , . Bookmark the permalink.