PhpStorm 2018.1 Help

Make Static Dialogs

Refactor | Make Static


Use this refactoring to convert an instance method into a static one.

The dialog box opens if you currently use the existing object via this or access the properties of the class. If your method does not need any parameters, nor rely on accessing properties or methods of this object, you will get the static method made silently, without displaying any dialog boxes:

BeforeAfter
class MyClass { private function getFormattedDate() { $format = getSettings()['dateFormat']; return time($format); } }
class MyClass { static private function getFormattedDate() { $format = getSettings()[ 'dateFormat' ]; return time( $format ); } }

Make Method Static dialog

ItemDescription
Add object as a parameter with name Select this checkbox to pass in an instance of the object via a parameter. In the text box below, specify the name of the parameter to be generated. After the refactoring, the new parameter will be documented in the PHPDoc block.
class MyClass { private function getFormattedDate() { $format = $this->getSettings()['dateFormat']; return time($format); } }
class MyClass { /** * @param MyClass $instance * @return */ static private function getFormattedDate($instance) { $format = $instance->getSettings()['dateFormat']; return time($format); } }
Add parameters for fields Use this area to pass the value of the property as a parameter instead of accessing the object inside the newly created static method if you access the properties of the class.
  1. Select the Add parameters for fields checkbox.
  2. In the Parameters list, which shows all the possibly suggested parameters, select the checkboxes next to the ones that you want to pass the values in.
class MyClass { private function getFormattedDate() { $format = $this->timeFormat; return time($format); } }
class MyClass { /** * @param $timeFormat * @return int; */ static private function getFormattedDate($timeFormat) { $format = timeFormat; return time($format); } }
Last modified: 27 July 2018

See Also