Features Releases

“Make Static” Refactoring Tool in PhpStorm 11

When refactoring, sometimes you wish to change your methods to be static, while this isn’t the best practice in the world, it’s possible you’ll be in a position where pragmatism will take priority over great design. If you are 100% percent certain that you won’t need to mock something, then use the new Make Static refactoring tool in PhpStorm 11 to ruin your future `self::`’s day.

If you want to make a simple method static, it’s as easy as placing the caret over the function name, and selecting the Make static option from the Refactor drop down menu. For simple methods with no access to the object, you can just deselect the Add object as a parameter with name checkbox, and click Refactor.

If your function is simple enough (it doesn’t need any parameters, or rely on accessing properties or methods of this object), you’ll get the static method made with no interaction and no fuss.

For more complex refactors, where you currently make use of the existing object via `this`, you’ll need to pass in an instance of the object via a parameter, by selecting the Add object as a parameter with name checkbox option, and giving the parameter a name.

Make Static Complex

Where you access the properties of the class, you can use the  Add parameters for fields section to pass the value of the property in as a parameter instead of accessing the object inside the newly created static method.

make static property

Make static result

While I heartily discourage you using this functionality, it’s nice to know that if you do choose to go down this route, PhpStorm has got you covered.

– Gary and the PhpStorm Team

The Drive to Develop!
JetBrains

image description