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:
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:
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.
Develop with pleasure!
- JetBrains PhpStorm Team