“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

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, PhpStorm and tagged , , . Bookmark the permalink.
  • https://www.michael.yoo.id.au/ Michael Yoo

    Wow, this feature is going to be insanely useful for me. Thanks PHPStorm!

  • Charly // Shaffe

    That’s a nice feature, that might be handy. It would be nice if instances properties could be make static as well. Again, it’s maybe not the best pratice, but it would be useful.